Load balancing splits incoming network traffic across multiple servers so no single server bears the full load
Load balancing is a networking technique that directs user requests to different servers based on current demand, server health, and configured rules. Instead of sending all traffic to one server until it slows down or fails, a load balancer acts as a traffic cop — receiving requests first, then forwarding them to whichever server has capacity to handle them quickly.
The practical result is faster response times for users, better use of your server hardware, and protection against outages. If one server goes down, the load balancer stops sending traffic to it and routes requests to the remaining servers instead. For anyone running a website, application, or service that receives more than one server can handle, load balancing is how you keep things running smoothly.
Key Takeaways
- A load balancer receives all incoming requests and forwards them to different backend servers based on their current capacity and health status.
- Common distribution methods include round-robin (alternating servers), least connections (sending traffic to the least busy server), and IP hash (routing the same user to the same server each time).
- Load balancers can operate at Layer 4 (transport layer, handling raw traffic speed) or Layer 7 (application layer, understanding HTTP requests and content type).
- Hardware load balancers are physical devices that handle very high traffic volumes, while software load balancers run on standard servers and cost less but use more CPU.
How a load balancer decides which server gets each request
Load balancers use different strategies to distribute traffic, and the right choice depends on what your servers are doing. Round-robin is the simplest: requests go to server 1, then server 2, then server 3, then back to server 1, cycling through in order. This works well when all servers are identical and requests take roughly the same time to process.
Least connections sends each new request to whichever server currently has the fewest active connections. This is better when some requests take much longer than others — a video upload might tie up a connection for minutes, while a text search finishes in milliseconds. The load balancer avoids piling new work onto a server that is already busy.
IP hash looks at the user's IP address and always routes that user to the same backend server. This matters when a user's session data (login information, shopping cart contents, form progress) is stored on that specific server rather than in a shared database. Without IP hash, a user might be routed to a different server on their next request and lose their session.
Weighted distribution lets you send more traffic to more powerful servers. If you have one server with twice the CPU and RAM, you can tell the load balancer to send it twice as many requests as the others.
Layer 4 versus Layer 7 load balancing
Load balancers operate at different levels of the network stack, and this affects what decisions they can make. Layer 4 load balancing (transport layer) works with raw TCP and UDP traffic. It sees only the source IP, destination IP, port numbers, and protocol type — not the content of the request itself. Layer 4 is very fast because it makes routing decisions with minimal information, but it cannot understand what the request actually is.
Layer 7 load balancing (application layer) reads the actual HTTP request: the URL path, hostname, headers, and request body. This lets you route requests based on content. For example, you could send all requests to /api/ to one set of servers and all requests to /images/ to a different set. You could route requests for mobile.example.com to servers optimized for mobile, and desktop.example.com to different servers. Layer 7 is more flexible but slower because the load balancer has to read and understand each request.
Most modern deployments use Layer 7 for web traffic because the performance difference is negligible for typical request volumes, and the routing flexibility is worth it. Layer 4 is still common for non-HTTP protocols or extremely high-traffic scenarios where every millisecond matters.
Hardware load balancers versus software load balancers
Hardware load balancers are physical devices that sit between your internet connection and your servers. They are built specifically for this job and can handle enormous traffic volumes — millions of requests per second. Products like F5 BIG-IP and Citrix NetScaler are industry standards. The trade-off is cost: hardware load balancers range from tens of thousands to hundreds of thousands of dollars, plus ongoing support contracts.
Software load balancers run on standard servers using open-source or commercial software. NGINX, HAProxy, and Apache are common choices. They cost far less — often free — but they consume CPU and memory on the server they run on. A software load balancer can typically handle thousands of requests per second, which is enough for most websites and applications. If you need to scale beyond that, you either add more software load balancers in front of each other (called cascading) or move to hardware.
Cloud providers like AWS, Google Cloud, and Azure offer managed load balancing services where you do not own or operate the hardware — you configure routing rules and the provider handles the infrastructure. This removes the operational burden and scales automatically with your traffic.
Health checks and failover
A load balancer does not just send traffic blindly to servers. It continuously checks whether each backend server is actually working by sending periodic health check requests — typically a simple HTTP request to a specific URL or a TCP connection attempt. If a server stops responding, the load balancer removes it from the rotation and stops sending traffic to it.
When a server comes back online and starts responding to health checks again, the load balancer adds it back to the pool. This happens automatically without any manual intervention. From the user's perspective, they never notice that a server went down — their requests simply go to a different server instead.
Health checks are configurable. You can set how often they run (every 5 seconds, every 30 seconds), how many failed checks trigger removal (fail twice and remove, or fail five times), and what timeout to use (wait 2 seconds for a response, or 10 seconds). Aggressive health checks catch failures faster but generate more traffic and CPU load on your servers.
Session persistence and sticky sessions
Some applications need a user to stay connected to the same server for the duration of their session. This is called session persistence or sticky sessions. The load balancer remembers which server a user was routed to and sends all their subsequent requests to that same server, even if other servers have less load.
Session persistence is necessary when session data is stored locally on each server rather than in a shared database or cache. A user logs in to server 1, which stores their login token in memory. If the load balancer then routes their next request to server 2, that server has no record of the login and the user gets logged out.
The better long-term solution is to store session data in a shared location — a database, Redis cache, or memcached cluster — so any server can handle any user. This lets you use simple round-robin or least-connections balancing without sticky sessions. But during a migration or for legacy applications, sticky sessions keep things working while you refactor.
Common load balancing algorithms at a glance
| Algorithm | How it works | Best for |
|---|---|---|
| Round-robin | Cycles through servers in order | Identical servers, uniform request times |
| Least connections | Routes to the server with fewest active connections | Variable request duration, long-lived connections |
| IP hash | Routes same user IP to same server always | Local session storage, sticky sessions needed |
| Weighted round-robin | Cycles through servers, sending more to powerful ones | Mixed server capacity, predictable load |
| Random | Picks a random server for each request | Testing, simple scenarios |
Frequently Asked Questions
Does a load balancer slow down my network?
A load balancer adds a small amount of latency — typically 1 to 5 milliseconds — because every request passes through it. This is negligible for most applications. The performance gain from distributing traffic across multiple servers far outweighs this small delay. Without load balancing, a single overloaded server would be much slower.
What happens if the load balancer itself fails?
This is why most deployments use multiple load balancers in an active-active or active-passive configuration. In active-passive, one load balancer handles traffic while a second one stands by. If the first fails, the second takes over automatically using a technology called failover. In active-active, both load balancers handle traffic simultaneously, so losing one just reduces capacity slightly rather than causing a complete outage.
Can I use load balancing for databases?
Yes, but it works differently than for web servers. Database load balancing typically routes read queries to replica servers while sending writes to the primary server. This spreads read load across multiple machines. Tools like ProxySQL and pgBouncer handle this for MySQL and PostgreSQL respectively. Write operations still go to one place, so you cannot scale writes the same way you scale reads.
Do I need load balancing if I use a cloud provider?
Cloud providers like AWS and Google Cloud include load balancing services as part of their platform. If you are running multiple instances of your application, you should use their load balancer rather than managing your own. It integrates with their auto-scaling, monitoring, and security features.