go-lb is a simple, custom load balancer written in Go
go-lb is a simple, custom load balancer written in Go. It uses a round-robin load balancing strategy to send requests into a set of servers with support for concurrent request handling. It also performs active and passive health checks of servers.
Clone the repository:
git clone https://github.com/kpraveenkumar19/go-lb.git
cd load-balancer
Build the project:
You can build the binaries for the load balancer and the mock server using the standard Go build command.
# Build the Load Balancer
go build -o bin/lb ./lb
# Build the Mock Server
go build -o bin/server ./server
You can run the project either using the provided automated script or by manually starting the components.
The easiest way to see the load balancer in action is to use the test_run.sh script. This script builds the binaries, spins up multiple backend servers, starts the load balancer, runs test requests, and then cleans up.
Make the script executable:
chmod +x test_run.sh
Run the script:
./test_run.sh
What happens:
lb or server.8081, 8082, and 8083.3030.curl requests to demonstrate load balancing.If you prefer to run components individually:
Start Backend Servers:
Open separate terminals or run in the background:
| Flag | Description |
|---|---|
--name |
Name of the server |
--port |
Port for the load balancer to listen on (default: 8080). |
./bin/server -port 8081 -name "Server-1" &
./bin/server -port 8082 -name "Server-2" &
./bin/server -port 8083 -name "Server-3" &
Start the Load Balancer:
Open separate terminal or run in the background:
| Flag | Description |
|---|---|
--backends |
Comma-separated list of backend URLs. |
--port |
Port for the load balancer to listen on (default: 3030). |
./bin/lb -port 3030 -backends "http://localhost:8081,http://localhost:8082,http://localhost:8083"
Send Requests:
Use curl to send traffic to the load balancer:
# "------------------------------------------------"
# "Test 1: Sequential Requests"
# "------------------------------------------------"
echo "Sending 8 sequential requests..."
for i in {1..8}; do
curl -s "http://localhost:3030"
done
# "------------------------------------------------"
# "Test 2: Concurrent Requests"
# "------------------------------------------------"
echo "Sending 100 concurrent requests..."
(
for i in {1..100}; do
curl -s "http://localhost:3030" &
done
wait
)
Check the output logs of the load balancer and servers to see the request distribution. Below is an example of how logs will look like :

Contributions are welcome! To propose changes: