/Load-balancer

Simple load balancer that can distribute client requests/network to 3 servers according to Round-Robin algorithm

Primary LanguageTypeScript

How the project works ?


  • You have 3 "Express" servers and a "load balancer" sitting infront of them.

  • This command "curl http://localhost:80" calls the load balancer, then the load balancer will forward that request to servers according to RR (Round Robin) algorithm:

  • 1st request => 1st server

  • 2nd request => 2nd server

  • 3rd request => 3rd server


How to run the project on your machine ?

Follow these tips:

  • clone the repo
  • npm install
  • Generate ".env" file and allocate these variables to unused ports on your local machine:
    • SERVER_PORT=
    • LOAD_BALANCER_PORT=
    • SECOND_SERVER_PORT=
    • THIRD_SERVER_PORT=
  • npm run dev

How to use the project works?

  • Once you run the command "npm run dev".

  • When you send an HTTP request to the load balancer by:

  • It will forward the request to the active servers and you will get hello from server...
    according to RR algorithm.

  • This command is responsible for checking the health of server:

  • when you run the previous command you will get a response contains details abt you server.

  • Try to kill one of the servers(by uncommenting "throw new Error()" in "healthChecker.ts") and run this cmd:

  • curl http://localhost:PORT_NUMBER/health-check
    and the broken server will be out.

Steps to use Dockerfile for the application

docker build -t image-name:tag .
docker run -itd -p host_port:container_port --env-file .env image-name:tag

Steps to use docker compose

docker-compose up -d