This is my implemenataion of a load balancer (from Coding Challenges: https://codingchallenges.fyi/challenges/challenge-load-balancer) written in Java. A load balancer distributes incoming client requests across multiple server instances to ensure efficient resource utilization.
Components/Features:
- Server: represents individual server instances
- ServerManager: manages creation and retrival of server instances
- LoadBalancer: main component that recieves incoming client request and forwards them to the appropriate server based on specified strategy
- HealthCheck: periodically checks health of servers and maintains a list of healthy servers
- LoadBalancerStrategy: implements algorithms for load balancing & session persistence: i. Round-Robin ii. Weighted Round-Robin iii. Least Connections
Setup and Running:
- Clone repository
- Build using Maven: mvn clean package
- Run: run-load-balancer.bat This starts server and load balancer instances
Once the load balancer is up and running, you can send http requests to it: curl http://localhost:1221 The load balancer will distribute these requests among the available server instances
Round-Robin strategy:
Weighted Round-Robin strategy:
Configuration: The following can be configured in src/main/resources/config.properties:
- server.startPort
- noOfServers
- loadbalancer.strategy
- healthCheck.interval
- loadBalancer.port
- sessionTimeout