What is a Load Balancer Algorithm?
Learn what a load balancer algorithm is — round robin, least connections, weighted, and IP hash — and when to use each.
Expected Interview Answer
A load balancer algorithm is the rule a load balancer uses to decide which backend server receives each incoming request — common examples include round robin, least connections, weighted round robin, and IP hash — chosen to balance load fairly while respecting server capacity and session needs.
Round robin cycles through servers in fixed order, sending each new request to the next server in the list, which works well when all backends have similar capacity and requests are roughly uniform in cost. Least connections instead tracks how many active connections each server currently holds and sends the new request to whichever has the fewest, which handles uneven request durations far better than round robin. Weighted variants of either algorithm let an operator assign heavier traffic shares to more powerful servers. IP hash computes a hash of the client’s source IP to consistently map that client to the same backend, which is useful for simple session affinity without a shared session store. The right algorithm choice depends on whether backends are homogeneous, whether requests vary widely in cost, and whether the application needs sticky sessions.
- Distributes load to prevent any single server from being overwhelmed
- Improves response times by avoiding queuing on busy servers
- Can be tuned for heterogeneous server capacity via weighting
- Some algorithms support session affinity for stateful applications
AI Mentor Explanation
A load balancer algorithm is like the system a nets coach uses to assign incoming batters to available bowling machines — round robin sends each batter to the next machine in a fixed rotation, while a smarter coach instead sends the next batter to whichever machine currently has the shortest queue. Choosing the shortest-queue machine mirrors the least-connections algorithm, since it accounts for batters who take longer sessions than others. Both approaches aim to keep every machine equally busy rather than letting one queue balloon.
Step-by-Step Explanation
Step 1
Request arrives
The load balancer receives a new incoming client request that needs to be routed to a backend.
Step 2
Apply the algorithm
It applies the configured rule — round robin, least connections, weighted, or IP hash — to pick a target server.
Step 3
Forward the request
The request is proxied to the chosen backend server, and its response is returned to the client.
Step 4
Update state
The load balancer updates its bookkeeping (e.g., active connection counts) to inform the next routing decision.
What Interviewer Expects
- Names and explains at least round robin and least connections correctly
- Knows weighted variants exist for heterogeneous server capacity
- Understands IP hash and its use for simple session affinity
- Can reason about which algorithm suits which workload pattern
Common Mistakes
- Thinking round robin accounts for current server load
- Confusing least connections with least response time
- Assuming one algorithm is universally best regardless of workload
- Forgetting that session affinity may require a specific algorithm choice
Best Answer (HR Friendly)
“A load balancer algorithm is simply the rule used to decide which server handles the next request — like taking turns in a fixed order, or always picking whichever server is least busy right now. Different rules suit different situations, so picking the right one keeps every server working evenly instead of some being overloaded while others sit idle.”
Code Example
from itertools import cycle
servers = ["10.0.0.1", "10.0.0.2", "10.0.0.3"]
round_robin_pool = cycle(servers)
def pick_round_robin():
return next(round_robin_pool)
active_connections = {s: 0 for s in servers}
def pick_least_connections():
target = min(active_connections, key=active_connections.get)
active_connections[target] += 1
return target
def release_connection(server):
active_connections[server] = max(0, active_connections[server] - 1)
print(pick_round_robin()) # 10.0.0.1
print(pick_least_connections()) # server with fewest active connectionsFollow-up Questions
- When would you choose least connections over round robin?
- How does weighted round robin handle heterogeneous server capacity?
- What is the tradeoff of using IP hash for session affinity?
- How does least response time differ from least connections?
MCQ Practice
1. Which algorithm sends each new request to the server with the fewest active connections?
Least connections routes each request to whichever backend currently has the fewest active connections.
2. What does IP hash-based load balancing primarily provide?
IP hash computes a hash of the client IP to consistently map that client to the same backend, aiding session affinity.
3. Why would an operator use weighted round robin?
Weighted round robin lets operators assign a larger traffic share to servers with more capacity.
Flash Cards
What is a load balancer algorithm? — The rule used to decide which backend server handles each incoming request.
Round robin vs least connections? — Round robin cycles servers in fixed order; least connections picks the server with fewest active connections.
What does weighting add? — Lets more powerful servers receive a proportionally larger share of traffic.
What does IP hash provide? — Consistent routing of a given client to the same backend, useful for simple session affinity.