This repository contains a Go implementation of a dynamic load balancer that distributes HTTP traffic across multiple backend servers. The load balancer can be used with a configurable load balancing strategy (e.g., Round Robin, Hashed).
.
├── README.md
├── common
│ └── common.go
├── go.mod
├── go.sum
├── main.go
├── servers
│ └── server.go
├── strategy
│ └── strategy.go
└── vendor
├── github.com
│ └── google
└── modules.txt
- Round Robin Balancing: Distributes traffic evenly across all backend servers.
- Hashed Balancing: Consistent hashing to balance requests based on request content.
- Multiple Backends: You can configure multiple backend servers and the load balancer will route traffic to them.
-
Clone the repository:
git clone <repo-url> cd dynamic-load-balancer
-
Run the backend servers:
go run servers/server.go -port=8081 go run servers/server.go -port=8082 go run servers/server.go -port=8083 go run servers/server.go -port=8084
-
Start the load balancer:
go run main.go
-
Test the load balancer by sending HTTP requests to `http://localhost:9090\`:
curl http://localhost:9090
In a real-world scenario, backend servers may go down due to various reasons such as network issues, hardware failures, or software crashes. To simulate a backend node going down during testing, you should specifically kill the process running on a particular port.
-
Identify the process running on the port of the backend server you want to simulate as down. You can use the following command to find the Process ID (PID) of the server:
ps aux | grep <port_number>
Replace `<port_number>` with the port number of the backend server you want to kill. For example, if you want to kill the server running on port 8083:
ps aux | grep 8083This command will output a list of processes. Look for the line that includes your `go run servers/server.go -port=8083` command. The second column in the output is the PID of the process.
-
Once you've identified the PID, you can kill that specific process using the `kill` command:
kill <PID>
For example, if the PID is `59396`, you would run:
kill 59396This will stop the server on port 8083 without affecting other processes, including the load balancer.
-
Observe how the load balancer handles the failure by checking the logs or sending new requests to the load balancer:
curl http://localhost:9090
The load balancer should automatically reroute traffic to the remaining healthy backend servers.