Skip to content

DevelopersCoffee/dynamic-load-balancer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dynamic Load Balancer

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).

Project Structure

.
├── README.md
├── common
│   └── common.go
├── go.mod
├── go.sum
├── main.go
├── servers
│   └── server.go
├── strategy
│   └── strategy.go
└── vendor
    ├── github.com
    │   └── google
    └── modules.txt

Features

  • 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.

How to Run

  1. Clone the repository:

    git clone <repo-url>
    cd dynamic-load-balancer
  2. 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
  3. Start the load balancer:

    go run main.go
  4. Test the load balancer by sending HTTP requests to `http://localhost:9090\`:

    curl http://localhost:9090

Simulating a Backend Node Failure

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.

Steps to Simulate Node Down

  1. 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 8083

    This 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.

  2. 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 59396

    This will stop the server on port 8083 without affecting other processes, including the load balancer.

  3. 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.

About

A lightweight load balancer written in Go, featuring multiple strategies like Round-Robin and Hashed balancing. Distribute incoming requests across backend servers efficiently. Perfect for learning and implementing custom load balancing algorithms in microservices and distributed systems.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors