Load Balancing: Algorithms and Strategies
In modern applications, thousands of users may use a system at the same time. If every request goes to a single server, that server soon becomes slow, and if it crashes, the whole application goes down. Load balancing solves this problem by spreading incoming requests across several servers, so that no single server is overloaded and users always reach one that is healthy.
This tutorial explains what load balancing is, how a load balancer works, the most common algorithms, Layer 4 and Layer 7 load balancing, health checks, sticky sessions and the main types of load balancer. It also shows practical examples with Nginx and Spring Boot.
1. What Is Load Balancing?
Load balancing is the process of distributing network traffic across multiple servers to improve performance, reliability and availability. The component that does this is called a load balancer. It sits between the users and a group of servers, often called a server pool or backend pool, and decides which server should handle each request.
To the user, the whole pool looks like one application with one address. Behind that address, the load balancer quietly shares the work and sends traffic only to servers that are working correctly.
2. Why Load Balancing Is Important
- Better performance: work is shared, so each server stays fast and response times stay low, even at busy times.
- No overloaded servers: a sudden spike in traffic is spread out instead of crushing one machine.
- High availability: if one server crashes, the load balancer sends traffic to the others, and users may not notice anything.
- Easy scaling: you can add servers when traffic grows and remove them when it drops, without changing the address users connect to. This is called horizontal scaling.
- Zero-downtime updates: servers can be updated one at a time while the others keep serving traffic.
- A single place for common tasks: many load balancers also handle HTTPS, compression and basic security checks.
3. How a Load Balancer Works
Every request goes through the same simple steps:
- Receive: the user's request arrives at the load balancer's public address.
- Check health: the load balancer already knows which servers are healthy, because it checks them regularly.
- Choose: it uses a load balancing algorithm to pick one healthy server.
- Forward: it sends the request to that server.
- Return: the server's response travels back to the user, usually through the load balancer.
The choice in step 3 is where the algorithms below come in. Some ignore the current state of the servers (static algorithms), while others look at how busy each server is right now (dynamic algorithms).
4. Common Load Balancing Algorithms
4.1 Round Robin
Round Robin is the simplest algorithm. Requests are sent to the servers one after another in a fixed circular order. With three servers, A, B and C:
- Request 1 → Server A
- Request 2 → Server B
- Request 3 → Server C
- Request 4 → Server A again, and the cycle repeats.
It works well when all servers have the same capacity and requests take roughly the same time. It does not notice if one server is struggling.
4.2 Weighted Round Robin
Weighted Round Robin gives each server a weight based on its capacity. A server with a higher weight receives proportionally more requests.
- Server A, weight 3 (a large machine)
- Server B, weight 1 (a small machine)
Out of every four requests, Server A receives three and Server B receives one. This is useful when your servers are not all the same size, or when you want to send a small share of traffic to a new version for testing.
4.3 Least Connections
Least Connections sends each new request to the server with the fewest active connections at that moment. If Server A has 10 active connections, Server B has 5 and Server C has 8, the next request goes to Server B.
This is a good choice when requests take very different amounts of time, such as a mix of quick page loads and long file uploads, because busy servers naturally receive fewer new requests.
4.4 Least Response Time
Least Response Time combines two measurements: how many connections a server has and how quickly it has been answering. The request goes to the server that is likely to respond fastest. It adapts well to servers that slow down temporarily, but the load balancer has to keep measuring response times.
4.5 IP Hash
IP Hash turns the client's IP address into a number using a hash function and uses that number to pick a server. Because the same IP always produces the same result, a given client keeps reaching the same server, as long as the server list does not change.
It is useful when a server keeps some information about the user in memory, but it can spread traffic unevenly, for example when many users share one IP address behind a company network.
4.6 Consistent Hashing
Simple hashing has a problem: if a server is added or removed, almost every client may be moved to a different server. Consistent hashing places servers and keys on an imaginary ring, so adding or removing one server only moves a small share of the keys. It is widely used for distributed caches and databases, where moving keys is expensive.
4.7 Random and "Power of Two Choices"
The Random algorithm simply picks a server at random, which evens out over many requests. A clever improvement is the power of two choices: pick two servers at random and send the request to the less busy of the two. It is cheap to compute yet spreads load almost as well as checking every server.
5. Comparing the Algorithms
| Algorithm | Type | Best for | Watch out for |
|---|---|---|---|
| Round Robin | Static | Identical servers, similar requests | Ignores how busy servers are |
| Weighted Round Robin | Static | Servers of different sizes, gradual rollouts | Weights must be set and updated by hand |
| Least Connections | Dynamic | Requests with very different durations | Connection count is not the same as actual load |
| Least Response Time | Dynamic | Servers whose speed varies | Needs constant measurement |
| IP Hash | Static | Keeping a client on the same server | Uneven load when many users share an IP |
| Consistent Hashing | Static | Caches and data that live on specific servers | More complex to implement |
| Power of Two Choices | Dynamic | Very large pools of servers | Slightly less precise than checking every server |
If you are not sure where to start, Round Robin for identical, stateless servers and Least Connections for uneven workloads cover most applications.
6. Layer 4 vs Layer 7 Load Balancing
Load balancers are often described by the layer of the network model they work at:
| Aspect | Layer 4 (Transport) | Layer 7 (Application) |
|---|---|---|
| Decides using | IP addresses and ports (TCP or UDP) | HTTP details such as URL path, headers and cookies |
| Reads request content? | No | Yes |
| Speed | Very fast, very little processing | Slightly slower, but far more flexible |
| Example routing | All traffic on port 443 to the web pool | /api/* to API servers, /images/* to image servers |
| Extra features | Few | HTTPS termination, redirects, header changes, caching |
| Typical uses | Databases, game servers, very high traffic | Websites, REST APIs, microservices |
Many large systems use both: a Layer 4 load balancer at the front for raw speed, passing traffic to Layer 7 load balancers that make the detailed routing decisions.
7. Load Balancing Strategies
7.1 Server-Side Load Balancing
In server-side load balancing, a dedicated load balancer sits between clients and servers. Clients only know the load balancer's address; the load balancer chooses the server. This is the most common setup for public websites and APIs, because clients stay simple and the server list can change freely.
7.2 Client-Side Load Balancing
In client-side load balancing, the calling application gets a list of available server instances, usually from a service registry, and picks one itself. There is no extra network hop through a separate load balancer, which makes it popular for calls between microservices inside a private network.
7.3 DNS-Based Load Balancing
DNS-based load balancing returns different IP addresses for the same domain name, so different users connect to different servers. It is simple and needs no extra hardware, but DNS answers are cached by browsers and internet providers, so traffic can keep going to a failed server until the cache expires.
7.4 Global Server Load Balancing (GSLB)
Global server load balancing spreads traffic across data centres or cloud regions around the world. Users are sent to the nearest healthy region, which reduces delay, and if a whole region fails, its traffic moves to another. It is usually built on smart DNS or on anycast networking, where many locations share the same IP address.
8. Health Checks and Failover
A load balancer must never send users to a broken server, so it keeps checking the health of every server in the pool.
- Active health checks: the load balancer regularly calls a health endpoint, such as
/health, every few seconds. If a server fails several checks in a row, it is removed from the pool; when it passes again, it is added back. - Passive health checks: the load balancer watches real traffic. If requests to a server keep failing or timing out, it stops using that server for a while.
- Connection draining: before a server is removed for maintenance, the load balancer stops sending it new requests but lets current requests finish, so no user is cut off.
A good health endpoint checks that the application can really do its job, for example that it can reach its database, but stays fast and cheap so the checks themselves do not add load.
9. Session Persistence (Sticky Sessions)
Some applications store user data, such as a shopping cart, in the memory of one server. If the next request goes to another server, that data is missing. Sticky sessions fix this by sending all of a user's requests to the same server, usually by setting a cookie or by using IP Hash.
Sticky sessions are easy to switch on, but they have drawbacks: load can become uneven, and if that server fails, its users lose their session. The better long-term solution is to make servers stateless and keep session data in a shared store, such as Redis or a database, so any server can handle any request. Read more in Stateful vs Stateless Architecture.
10. Hardware, Software and Cloud Load Balancers
| Type | Examples | Advantages | Disadvantages |
|---|---|---|---|
| Hardware | Dedicated appliances, for example from F5 or Citrix | Very high performance, specialised features | Expensive, fixed capacity, harder to scale |
| Software | Nginx, HAProxy, Envoy, Traefik | Free or low cost, flexible, runs anywhere | You install, configure and run it yourself |
| Cloud (managed) | AWS Elastic Load Balancing, Google Cloud Load Balancing, Azure Load Balancer | No servers to manage, scales automatically, built-in health checks | Pay per use, tied to one cloud provider |
In container platforms, load balancing is often built in. For example, a Kubernetes Service spreads traffic across the pods behind it, and an Ingress controller adds Layer 7 routing.
11. Example: Load Balancing with Nginx
Nginx is a popular open-source web server that also works as a load balancer. This configuration spreads requests across three application servers using Least Connections, gives the larger server a higher weight, temporarily stops using a server that keeps failing, and keeps a spare server for emergencies:
http {
upstream shop_backend {
least_conn; # send to the least busy server
server 10.0.0.11:8080 weight=2; # bigger machine, gets more traffic
server 10.0.0.12:8080 max_fails=3 fail_timeout=30s;
server 10.0.0.13:8080 max_fails=3 fail_timeout=30s;
server 10.0.0.20:8080 backup; # used only if the others are down
}
server {
listen 80;
server_name shop.example.com;
location / {
proxy_pass http://shop_backend;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
Without least_conn, Nginx uses Round Robin by default. Other options include ip_hash to keep a client on the same server and hash $request_uri consistent for consistent hashing. The max_fails and fail_timeout settings are a form of passive health checking.
12. Example: Client-Side Load Balancing in Spring Boot
In a Spring Boot microservice, Spring Cloud LoadBalancer provides client-side load balancing. Add the spring-cloud-starter-loadbalancer dependency (or a discovery client such as Eureka, which includes it) and mark an HTTP client builder with @LoadBalanced:
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;
@Configuration
public class HttpClientConfig {
@Bean
@LoadBalanced // resolve service names to real instances
public RestClient.Builder restClientBuilder() {
return RestClient.builder();
}
}
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClient;
@Component
public class InventoryClient {
private final RestClient restClient;
public InventoryClient(RestClient.Builder builder) {
// "inventory-service" is a service name, not a host:
// each call goes to one of its running instances
this.restClient = builder.baseUrl("http://inventory-service").build();
}
public int stockFor(String sku) {
return restClient.get()
.uri("/api/stock/{sku}", sku)
.retrieve()
.body(Integer.class);
}
}
Each call is sent to a different instance of inventory-service. By default, Spring Cloud LoadBalancer uses Round Robin, and a Random strategy is also available. The list of instances usually comes from a service registry; see Service Discovery for how that works.
13. Avoiding a Single Point of Failure
A load balancer protects your servers, but what protects the load balancer? If you run only one and it fails, the whole application is unreachable, so the load balancer itself must be redundant:
- Active-passive pair: two load balancers share a virtual (floating) IP address. The standby watches the active one and takes over the address within seconds if it fails. Tools such as keepalived implement this.
- Active-active: several load balancers handle traffic at the same time, with DNS or anycast spreading users between them.
- Managed cloud load balancers: these are built from many machines across several data centres, so redundancy is handled for you.
14. A Simple Real-Life Example
Think of a busy supermarket with several checkout counters. If every customer queued at one counter, the line would be very long and the other cashiers would sit idle. Instead, a staff member at the front directs each customer to a free counter:
- The staff member is the load balancer.
- The checkout counters are the servers.
- Sending customers to the counter with the shortest queue is the Least Connections algorithm.
- Closing a counter whose till has broken, and sending its customers elsewhere, is a health check and failover.
- Opening extra counters at busy times is horizontal scaling.
Everyone is served faster, and one broken till does not stop the shop. Load balancing does the same for servers.
15. Best Practices
- Keep servers stateless and store sessions in a shared store, so any server can handle any request.
- Configure meaningful health checks that confirm the application really works, and keep them fast.
- Choose the algorithm to match the workload: Round Robin for similar requests, Least Connections for uneven ones, hashing when data lives on specific servers.
- Run at least two load balancers, or use a managed cloud load balancer, to avoid a single point of failure.
- Use connection draining during deployments so users are never cut off mid-request.
- Terminate HTTPS at the load balancer when it suits your security needs, so certificates are managed in one place.
- Set sensible timeouts so slow servers cannot hold connections forever.
- Monitor request rates, error rates, response times and the health of each server, and alert when the pool is shrinking.
- Combine with auto-scaling so servers are added automatically when traffic rises.
16. Common Mistakes to Avoid
| Mistake | What goes wrong | Better approach |
|---|---|---|
| Running a single load balancer | It becomes a single point of failure for the whole system | Use a redundant pair or a managed service |
| Health checks that only test "is the process running?" | Broken servers keep receiving traffic | Check real dependencies, such as the database |
| Storing sessions in server memory | Users are logged out when they move to another server | Use a shared session store or stateless tokens |
| Using Round Robin for very uneven requests | Some servers are overloaded while others are idle | Use Least Connections or Least Response Time |
| Long DNS cache times with DNS load balancing | Traffic keeps going to failed servers for minutes or hours | Use short TTLs, or a real load balancer for failover |
| Removing servers without draining | Requests in progress fail during deployments | Enable connection draining |
17. Summary
- Load balancing spreads traffic across several servers to improve performance, availability and scalability.
- A load balancer receives each request, picks a healthy server using an algorithm and forwards the request.
- Common algorithms include Round Robin, Weighted Round Robin, Least Connections, Least Response Time, IP Hash, Consistent Hashing and Power of Two Choices.
- Layer 4 load balancers route by IP and port; Layer 7 load balancers route by HTTP content such as paths and headers.
- Strategies include server-side, client-side, DNS-based and global server load balancing.
- Health checks and connection draining keep users away from broken or restarting servers.
- Stateless servers with shared session storage work better than sticky sessions in the long run.
- The load balancer itself must be redundant to avoid a single point of failure.
By choosing the right algorithm and strategy, and pairing load balancing with health checks and auto-scaling, applications can handle heavy traffic smoothly and give every user a fast, reliable experience. Open the FAQ tab for answers to common questions about load balancing.
Frequently Asked Questions (FAQ)
Q1. What is a load balancer in simple words?
A load balancer is like a traffic officer for servers. It receives every request sent to your application and passes each one to a server that is free and healthy, so no single server gets overloaded.
Users only ever see one address, while the load balancer quietly shares the work among many servers behind it.
Q2. What is the difference between load balancing and scaling?
Scaling means changing how much capacity you have, for example adding more servers. Load balancing means sharing traffic across the servers you already have.
They work together: horizontal scaling adds servers, and the load balancer makes sure the new servers actually receive traffic.
Q3. Which load balancing algorithm is the best?
There is no single best algorithm; it depends on your workload:
- Round Robin for identical servers and similar requests.
- Weighted Round Robin when servers have different capacities.
- Least Connections when requests take very different amounts of time.
- Consistent hashing when data lives on specific servers, such as caches.
Round Robin or Least Connections is a good default for most web applications.
Q4. What is the difference between static and dynamic algorithms?
Static algorithms, such as Round Robin, Weighted Round Robin and IP Hash, follow fixed rules and do not look at how busy the servers are right now.
Dynamic algorithms, such as Least Connections and Least Response Time, check the current state of each server before choosing. They adapt better to changing load but need more information.
Q5. What is the difference between Layer 4 and Layer 7 load balancing?
A Layer 4 load balancer decides using only IP addresses and ports. It is very fast but cannot see what is inside the request.
A Layer 7 load balancer understands HTTP, so it can route by URL path, headers or cookies, for example sending /api requests to API servers. It can also terminate HTTPS and modify requests.
Q6. What is the difference between a load balancer and a reverse proxy?
A reverse proxy sits in front of servers and forwards client requests to them, often adding features like HTTPS, caching and compression. A load balancer is a reverse proxy whose main job is to spread traffic across several servers.
Many tools do both: Nginx and HAProxy are often used as a reverse proxy and a load balancer at the same time.
Q7. What is the difference between a load balancer and an API gateway?
A load balancer mainly distributes traffic across copies of the same service. An API gateway is the single entry point for many different services and adds API features such as authentication, rate limiting, request routing and response aggregation.
They are often used together: a load balancer in front of several gateway instances, and the gateway routing to services. See the API Gateway Pattern for details.
Q8. What are health checks?
Health checks let the load balancer find out which servers are working.
- Active checks: the load balancer calls an endpoint such as
/healthevery few seconds. - Passive checks: it watches real requests and stops using a server that keeps failing.
A server that fails is removed from the pool and added back automatically once it is healthy again.
Q9. What happens when a server fails?
The load balancer detects the failure through its health checks, usually within a few seconds, and stops sending new requests to that server. Traffic is shared among the remaining healthy servers.
Requests that were already being processed by the failed server may fail, so clients and services should use sensible timeouts and, where safe, retries.
Q10. What are sticky sessions, and should I use them?
Sticky sessions send all requests from one user to the same server, usually using a cookie. They help when session data is stored in a server's memory.
However, they can cause uneven load and users lose their session if that server fails. It is usually better to make servers stateless and store sessions in a shared store such as Redis.
Q11. Can the load balancer become a single point of failure?
Yes. If you run only one load balancer and it fails, nobody can reach your application. To prevent this:
- Run two load balancers in an active-passive pair that share a floating IP address.
- Run several in an active-active setup behind DNS or anycast.
- Use a managed cloud load balancer, which is redundant by design.
Q12. What is the difference between client-side and server-side load balancing?
In server-side load balancing, a separate load balancer chooses the server, and clients only know its address. In client-side load balancing, the calling application gets a list of instances, usually from a service registry, and chooses one itself.
Server-side is common for public traffic; client-side is popular for calls between microservices because it avoids an extra network hop.
Q13. Is DNS load balancing enough on its own?
DNS load balancing is simple and cheap, but it has limits. Browsers and internet providers cache DNS answers, so after a server fails, some users may keep trying it until the cache expires. DNS also cannot see how busy each server is.
It works well for spreading users across regions, usually combined with real load balancers inside each region.
Q14. What is consistent hashing used for?
Consistent hashing assigns requests or data keys to servers so that adding or removing a server moves only a small share of the keys instead of nearly all of them.
It is ideal for distributed caches and databases, where moving data is expensive. With simple hashing, adding one cache server would make almost every cached item miss at once.
Q15. What is connection draining?
Connection draining, also called deregistration delay, lets a server finish its current requests before it is removed from the pool. The load balancer stops sending it new requests but waits, for example up to 30 seconds, for existing ones to complete.
It is essential for deployments and scaling down, so users are never cut off in the middle of a request.
Q16. Which load balancers are popular?
- Software: Nginx, HAProxy, Envoy and Traefik.
- Cloud: AWS Elastic Load Balancing (Application, Network and Gateway Load Balancers), Google Cloud Load Balancing and Azure Load Balancer.
- Hardware: dedicated appliances from vendors such as F5.
- Containers: Kubernetes Services and Ingress controllers.
Q17. Does a load balancer slow down requests?
A load balancer adds a very small delay, usually well under a millisecond for a Layer 4 load balancer and a little more for Layer 7. In practice, applications usually get faster overall, because no server is overloaded and requests are handled by servers with spare capacity.
Q18. Should HTTPS be handled at the load balancer?
Often, yes. TLS termination at the load balancer decrypts HTTPS once, which simplifies certificate management and lets the load balancer route by URL. Traffic to the servers can then use plain HTTP inside a trusted network.
If your security rules require encryption all the way, the load balancer can re-encrypt traffic to the servers, or pass encrypted traffic straight through.
Q19. How does load balancing work in Spring Boot microservices?
Spring Boot services commonly use Spring Cloud LoadBalancer for client-side load balancing. You mark a RestClient.Builder, WebClient.Builder or RestTemplate bean with @LoadBalanced and call services by name, such as http://inventory-service.
The service instances usually come from a registry such as Eureka or Consul, and the default algorithm is Round Robin.
Q20. How is load balancing asked about in system design interviews?
Interviewers expect you to place load balancers in your design and explain your choices. Be ready to discuss:
- Where load balancers sit, for example in front of web servers and between internal services.
- Which algorithm you would use and why.
- Layer 4 versus Layer 7.
- Health checks, sticky sessions versus stateless servers, and how you avoid a single point of failure.