toolrota

What is a load balancer: how it works, algorithms and high availability

When traffic grows one server isn't enough. We explain how a load balancer distributes requests, the algorithms and its role in high availability.

Serkan Akyürek··4 min read
What is a load balancer: how it works, algorithms and high availability – SaaS & Cloud

When a website becomes popular, a single server can't serve all requests; it slows or crashes. The solution is usually to run several servers and spread incoming traffic among them. The component that does this is the load balancer. This guide covers how it works, the basic algorithms and its contribution to high availability.

What is a load balancer?

A load balancer is the component that sits between users and servers and routes requests to a suitable server. The user connects to one address and doesn't know how many servers are behind it. For scaling logic see our cloud computing and CDN guides.

A request through a load balancer
  1. 1

    The user connects to the site address

  2. 2

    DNS points to the load balancer's address

  3. 3

    The load balancer identifies healthy servers

  4. 4

    It picks a server according to the algorithm

  5. 5

    The request goes to that server and the reply returns to the user

Why is it needed?

  • Performance: load is spread across servers.
  • High availability: if one server crashes traffic goes to the others.
  • Scalability: adding and removing servers gets easier.
  • Easier maintenance: you can update a server without downtime.
  • A security layer: SSL termination and filtering can be done (see our SSL/HTTPS guide).

Load-distribution algorithms

AlgorithmHow it works
Round robinDistributes requests to servers in turn
Weighted round robinGives more requests to stronger servers
Least connectionsSends to the one with the fewest active connections
IP hashAlways routes the same client to the same server
Least response timePicks the server answering fastest

L4 and L7 load balancing

TypeTrait
L4 (transport layer)Routes by IP and port; fast, doesn't look at content
L7 (application layer)Smart routing by URL, headers and cookies

L7 can, for example, send "/api" requests to one server group and images to another.

Health checks

The load balancer checks servers regularly (for example by requesting an address). A server that doesn't answer is removed from the pool and re-added once it recovers. This way users aren't routed to a faulty server. For monitoring see our uptime monitoring guide.

Session stickiness

In some apps the user's session is kept on one server; then the same user must always be sent to the same server. A better solution is to keep session data in a shared store and make the app stateless; this makes scaling easier.

High availability and redundancy

A single load balancer becomes a single point of failure if it fails itself. Managed load balancers in the cloud usually run redundantly across several zones. For disasters see our disaster recovery plan guide.

Load balancers in the cloud

Large cloud providers offer managed load balancing services, reducing setup and maintenance load (see our cloud providers guide). On your own server open source software (e.g. Nginx) can be used (see our VPS guide). In container environments Kubernetes also distributes traffic (see our Kubernetes guide).

Common mistakes

  1. Leaving the load balancer as a single point of failure.
  2. Not configuring health checks.
  3. Keeping session data on a local server and making scaling hard.
  4. Not planning capacity.
  5. Not managing the SSL certificate.
  6. Not setting up monitoring and alerts.

Frequently asked questions

Is it necessary for a small site? Generally no; a single server may be enough.

How does it differ from a CDN? A CDN distributes static content close to users; a load balancer distributes dynamic requests among servers.

Which algorithm is best? It depends on the workload; in many cases least connections is a good choice.

Summary

A load balancer improves performance and availability by distributing traffic across several servers. Plan the algorithm, health checks and redundancy properly.

Share
Published:
Author
Serkan Akyürek

Serkan Akyürek is the founder and author of toolrota; he writes step-by-step guides on Excel, AI, the cloud, personal finance and digital security.

Related guides

SaaS & Cloud
SaaS & Cloud4 min read

What are a CPU and a GPU: the difference and which matters for what

A computer's brain and its visual power are not the same thing. We explain what the CPU and GPU do, how they differ and how to choose by need.

SaaS & Cloud4 min read

What is Git: version control, commits, branches and merging for beginners

If you are tired of saving files as 'final_final2', Git is for you. We explain the core concepts and commands in plain language.

SaaS & Cloud7 min read

What is RAM and what does it do: how much RAM is enough, memory types and a buying guide

When a computer slows down, RAM is the first suspect. We explain what RAM does, how it differs from storage and how many GB to choose for your needs.

SaaS & Cloud7 min read

What is open source: open source software, licenses, benefits and a guide to contributing to projects

Much technology, from Linux to browsers, is open source. We explain what open source is, how licenses differ and how you can contribute to projects.

SaaS & Cloud8 min read

SSD vs HDD: what's the difference and which should you choose? Speed, lifespan, durability, capacity and cost compared

An SSD is fast and silent, while an HDD offers large capacity cheaply. We explain how each works, their differences, NVMe vs SATA and which use each suits.

Office & Productivity4 min read

What are mental models: thinking tools you can use to make better decisions

Decision-making gets easier with the right thinking tools. We explain the most useful mental models with everyday examples.