DEV Community

Cover image for What is a Load Balancer?
wassef ben ahmed
wassef ben ahmed

Posted on

What is a Load Balancer?

Why?


imagine that you've worked on a startup website or a web app for a client and it started to get 100 visits per month. one year in, and the number of visits drastically increased to the point that the client called you complaining about the app's performance, so being the great engineer you are, you decided to upgrade the server's specs hoping it will solve the response time problem and it worked!

But what if the server hits 3000 visits per minute and costumers started getting a "502 bad gateway" and upgrading the server specs isn't an option being too expensive or that you are already using the maxed out specs the hosting service could offer.

  • Hosting services list :
    1. Bluehost.
    2. HostGator.
    3. InMotion.

Upgrading the specs or what is called vertical scaling is sometimes accepted if used on a database server being that you need more storage or a better CPU to process data ...

But what if, we still run into errors even after the upgrade?
many factors may come into play even with a maxed-out server, the CPU handling the threads of different requests simultaneously will cause it to prioritize some of them in favor of others causing unwanted/unexpected behaviors...

Imagine being in a restaurant that has 10 clients and a waiter that can't handle all incoming requests, so we replaced him with a four-armed waiter but that also didn't solve the problem being that, he takes a lot of trips to the kitchen and a client may order 4 plates that will occupy all 4 hands.

Clearly the solution isn't surgically adding more hand to the waiter but in hiring another!
He'll take some load off the first and we'll be distributing the pressure on both.
That's what we're calling :

horizontal scaling :
adding more servers instead of maxing out the main server, which will consume a bit of the resources in each server making it easy to just add more, in a rush hour, and costing less than a gigantic server.

To apply horizontal scaling we need to deploy the code like we'ed normally do but on multiple servers (let's say 10)
Having the code duplicated on multiple places with NGINX as a load balancer, it will distribute the incoming requests on our 10 servers

leveraging 6 algorithms to decide which one to use :

Weighted/Dynamic Round Robin.
Least Connections.
IP Hash.
Generic Hash.
Least Time.
Random.

Top comments (0)