100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is a Load Balancer and How Does it Work?

Learn what a load balancer does, Layer 4 vs Layer 7 routing, health checks, and common algorithms — with a DevOps interview-ready answer.

mediumQ121 of 224 in DevOps Est. time: 6 minsLast updated:
Open Code Lab

Expected Interview Answer

A load balancer is a component that sits in front of a pool of backend servers and distributes incoming traffic across them using a chosen algorithm, so no single server is overwhelmed and the system stays available even if individual servers fail.

A load balancer continuously health-checks each backend, routing traffic only to instances that respond correctly, and automatically pulling unhealthy instances out of rotation until they recover. Layer 4 load balancers operate at the transport layer, distributing raw TCP/UDP connections based on IP and port with minimal overhead, while Layer 7 load balancers operate at the application layer and can route based on HTTP headers, paths, or cookies, enabling features like path-based routing and SSL termination. Common algorithms include round robin (cycling evenly through backends), least connections (favoring the least busy backend), and IP hash (pinning a client to the same backend for session affinity). In cloud environments, a load balancer also enables zero-downtime deployments by gradually shifting traffic to new instances while draining connections from old ones.

  • Prevents any single server from becoming a bottleneck
  • Removes unhealthy instances automatically via health checks
  • Enables zero-downtime deployments and rolling updates
  • Provides a single stable entry point despite backend churn

AI Mentor Explanation

A load balancer is like a team’s bowling coach deciding which of several fit bowlers gets the next over, rotating fairly so no single bowler is overworked into injury. If a bowler pulls up with a niggle during warm-up, the coach simply does not hand them the ball until they are checked and cleared, similar to a health check pulling an unhealthy server from rotation. The coach might favor whichever bowler has bowled the fewest overs that spell, similar to a least-connections algorithm. Batters at the crease never need to know which bowler is coming next — they just face whoever the coach sends, exactly like clients hitting a stable endpoint.

Step-by-Step Explanation

  1. Step 1

    Register backends

    Add a pool of backend servers/instances behind the load balancer’s stable endpoint.

  2. Step 2

    Run health checks

    Periodically probe each backend; remove failing ones from rotation automatically.

  3. Step 3

    Apply the routing algorithm

    Distribute requests using round robin, least connections, or IP hash based on the use case.

  4. Step 4

    Terminate or pass through

    Layer 7 balancers can terminate SSL and route by path/header; Layer 4 balancers forward raw connections.

What Interviewer Expects

  • Clear distinction between Layer 4 and Layer 7 load balancing
  • Knowledge of at least two load-balancing algorithms and when to use them
  • Understanding of health checks and automatic failover
  • Awareness of how load balancers enable zero-downtime deployments

Common Mistakes

  • Confusing a load balancer with a reverse proxy without discussing distribution logic
  • Not mentioning health checks as the failover mechanism
  • Assuming Layer 4 and Layer 7 balancing are interchangeable
  • Forgetting session affinity implications for stateful applications

Best Answer (HR Friendly)

A load balancer sits in front of our servers and spreads incoming traffic across them, so no single server gets overloaded and if one fails, traffic automatically shifts to the healthy ones. It is a key piece of how we keep the product available and responsive even during traffic spikes or deployments.

Code Example

Kubernetes Service as an internal load balancer
apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  type: LoadBalancer
  selector:
    app: myapp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
  sessionAffinity: ClientIP

Follow-up Questions

  • What is the difference between Layer 4 and Layer 7 load balancing?
  • How does a load balancer support zero-downtime deployments?
  • When would you use IP-hash session affinity over round robin?
  • How do health checks decide when to remove or re-add a backend?

MCQ Practice

1. What is the main purpose of a load balancer?

A load balancer spreads incoming requests across a pool of backends so no single server is overwhelmed.

2. Which OSI layer does a Layer 7 load balancer operate at?

Layer 7 load balancers inspect application-layer data like HTTP headers and paths to make routing decisions.

3. What does a load balancer health check accomplish?

Health checks continuously probe backends and pull failing ones out of rotation until they recover.

Flash Cards

What is a load balancer?A component distributing traffic across multiple backend servers.

Layer 4 vs Layer 7 load balancing?Layer 4 routes raw TCP/UDP by IP/port; Layer 7 routes by HTTP path/header/cookie.

What does a health check do?Detects unhealthy backends and removes them from rotation automatically.

What is IP-hash routing used for?Pinning a client to the same backend for session affinity.

1 / 4

Continue Learning