I genuinely believe that in order to understand a concept, you must try building it from scratch.
Here is a simple Golang implementation of a load balancer that was given as a homework exercise in The System Design Masterclass by Arpit Bhayani.
TODO
You can customize the load balancer's behavior by modifying config.json. The following options are available:
port
: Specifies the port on which the load balancer runs locally. You can change it as needed.
healthCheckInterval
: Defines the interval (in milliseconds) at which the load balancer continuously polls all registered servers to check their health.
lbAlgo
: Determines the load-balancing strategy. Available options:
round-robin
random
least-connections
servers
: Lists the backend servers that the load balancer can route traffic to.
maxRetries
: Specifies the number of retry attempts when a server fails to process a request. The request is forwarded to the next available server based on the selected load-balancing strategy.
# 1. Clone this repo
git clone https://github.com/thedatamonk/go-load-balancer.git
# 2. Go to the main directory
cd go-load-balancer
# 3. Start the HTTP servers
# This will also use config.json as input to get the name of the servers that need to be managed by the load balancer
python manage_servers.py
# 4. Start the load balancer
# The load balancer will be started on the localhost at the port number specified in the config.json
go run .
# 5. Send a request to the load balancer
# The grep command is used for extracting the value of "X-Forwarded-Server" from the server response.
curl -s -i http://localhost:8080 | grep "X-Forwarded-Server"
You can use Artillery to simulate high traffic and measure the performance of your load balancer.
Ensure you have Artillery installed. If not, install it globally using:
npm install -g artillery@latest
Execute the following command to run a load test using artillery.yaml
:
artillery run artillery.yaml --record --key ARTILLERY_API_KEY
- Use this as a base project to implement your own load balancer.
- Experiment with different load balancer settings to observe their impact.
- Gain a deeper understanding of how a load balancer works by interacting with its configurations and mechanisms.
-
Dynamic Server Scaling
- Add new servers dynamically via an API endpoint to handle increased traffic.
-
Automatic Removal of Unhealthy Servers
- Servers are monitored continuously.
- If a server fails to respond for a specified
threshold
duration, it is automatically removed from the pool.
-
Intelligent Retry Mechanism
- If a server fails to process a request, the load balancer retries the request with another available server.
-
Multiple Load Balancing Strategies
- Supports the following strategies:
- Round Robin
- Random
- Least Connections
- Supports the following strategies: