What is a Load Balancer in Networking?
Learn what a load balancer does, Layer 4 vs Layer 7 balancing, algorithms, and health checks — with networking interview questions.
Expected Interview Answer
A load balancer is a device or service that sits in front of a pool of backend servers and distributes incoming client requests across them using a chosen algorithm, so no single server is overwhelmed and the overall service stays available even if one backend fails.
Clients connect to a single virtual IP or hostname exposed by the load balancer instead of talking to any backend directly; the load balancer then picks a healthy backend using an algorithm such as round robin, least connections, or weighted/IP-hash, and forwards the request. Layer 4 load balancers work at the transport layer, distributing raw TCP/UDP connections without inspecting content, which is fast but less flexible; Layer 7 load balancers work at the application layer, inspecting HTTP headers, cookies, or paths to make smarter routing decisions (e.g., routing /api to one service and /images to another). Health checks continuously probe each backend, automatically removing unhealthy servers from rotation so traffic never gets routed to something that is down. This combination of even distribution and automatic failover is what allows applications to scale horizontally and stay resilient.
- Distributes traffic evenly to prevent any single server from overloading
- Removes unhealthy backends automatically via health checks
- Enables horizontal scaling by adding/removing backend servers transparently
- Layer 7 balancers enable content-aware routing (path, header, cookie)
AI Mentor Explanation
A load balancer is like the ground staff directing arriving spectators to different entry gates so no single gate gets a crushing queue while others sit empty. Staff constantly watch each gate's queue length, sending new arrivals to whichever gate is shortest, exactly like a least-connections algorithm. If a gate's scanner breaks down, staff stop sending people there until it is fixed, mirroring a health check removing a failed backend. This keeps the whole stadium entry flowing smoothly even during peak rush.
Step-by-Step Explanation
Step 1
Client request
A client connects to a single virtual IP or hostname exposed by the load balancer.
Step 2
Health-aware selection
The load balancer picks a healthy backend using an algorithm like round robin or least connections.
Step 3
Forwarding
The request is forwarded to the chosen backend, at Layer 4 (TCP/UDP) or Layer 7 (HTTP-aware).
Step 4
Continuous health checks
The balancer keeps probing backends and removes any that fail checks from rotation.
What Interviewer Expects
- Correct purpose: distributing traffic across backend servers
- Names at least one algorithm (round robin, least connections, IP hash)
- Distinguishes Layer 4 vs Layer 7 load balancing
- Explains the role of health checks in removing failed backends
Common Mistakes
- Thinking a load balancer only does simple round robin, ignoring content-aware Layer 7 routing
- Confusing a load balancer with a reverse proxy (a load balancer is a specialized form of one)
- Forgetting health checks are what enables automatic failover
- Not knowing DNS-based load balancing is also a real approach, distinct from a dedicated LB device
Best Answer (HR Friendly)
“A load balancer sits in front of a group of servers and spreads incoming traffic across them so no single server gets overwhelmed, and if one server goes down, it automatically stops sending traffic there and shifts it to the healthy ones. It is what lets a website handle a huge spike in visitors without crashing, by sharing the work across many machines instead of relying on just one.”
Code Example
# Send repeated requests through a load balancer and see which backend answered
for i in $(seq 1 5); do
curl -s https://api.example.com/whoami
done
# server-01
# server-02
# server-01
# server-03
# server-02
# Check the load balancer’s health endpoint for a specific backend
curl -sf https://api.example.com/health || echo "backend unhealthy"Follow-up Questions
- What is the difference between Layer 4 and Layer 7 load balancing?
- How does sticky session (session affinity) work with a load balancer?
- What happens during a rolling deployment behind a load balancer?
- How does DNS-based load balancing differ from a dedicated load balancer device?
MCQ Practice
1. What is the primary purpose of a load balancer?
A load balancer spreads client requests across a pool of backend servers to avoid overload.
2. What distinguishes Layer 7 load balancing from Layer 4?
Layer 7 balancers inspect application-layer data (HTTP headers, paths, cookies) to make routing decisions.
3. What happens when a backend server fails a load balancer's health check?
Failed health checks cause the load balancer to stop routing traffic to that backend until it recovers.
Flash Cards
What is a load balancer? — A device/service that distributes client requests across multiple backend servers.
Layer 4 vs Layer 7 LB? — Layer 4 balances raw TCP/UDP connections; Layer 7 routes based on HTTP content.
What do health checks do? — Continuously probe backends and remove unhealthy ones from rotation.
Name a load-balancing algorithm. — Round robin, least connections, or weighted/IP-hash.