Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

SD
System Design
0/13 completed
Beginner
What is system design?How the internet worksBack-of-the-envelope estimation
Intermediate
Scaling basicsLoad balancersCachingDatabases at scale
Advanced
Sharding and consistent hashingMessage queues and event streamingConsistency, CAP and distributed transactionsReliability and observability
Pro
Designing a URL shortenerDesigning a chat app
Tracks / System Design / Intermediate

Load balancers

A load balancer sits in front of a group of servers and spreads incoming requests across them. It's what makes horizontal scaling work: users talk to one address, and behind it you can run 3 servers or 300, add and remove them, and survive individual failures.

                     ┌─▶ server A  ✔
clients ──▶ load ────┼─▶ server B  ✔
            balancer ├─▶ server C  ✖ failed health check: no traffic
                     └─▶ server D  ✔

What a load balancer does

  • Distributes requests by an algorithm.
  • Health-checks servers (GET /healthz every few seconds) and stops sending traffic to failed ones.
  • Terminates TLS, so app servers don't each manage certificates.
  • Drains connections when a server is being removed, for zero-downtime deploys.
  • Often also: rate limiting, compression, request routing by path or host, and protection against some attacks.

Layer 4 vs layer 7

Layer 4 (transport) Layer 7 (application)
Sees IP addresses and ports (TCP/UDP) Full HTTP: URL, headers, cookies
Can route by Connection Path (/api → API servers, /images → image servers), host, header
Speed Very fast, simple More work per request, far more flexible
Examples AWS NLB, HAProxy (TCP mode), LVS AWS ALB, nginx, Envoy, HAProxy (HTTP), Traefik, Cloudflare

Balancing algorithms

Python · editable

When requests aren't equal: least connections

Round robin assumes every request costs the same. When some take 10 ms and others 2 seconds, a server can be swamped with slow requests while another sits idle. Least connections sends each request to the server with the fewest requests in progress:

Python · editable

Other smart strategies: least response time, and power of two choices (pick two servers at random and use the less busy one), which gets most of the benefit with very little coordination.

Health checks and failover

Python · editable

Two kinds of checks matter: liveness ("is the process alive?") and readiness ("can it serve traffic right now?", e.g. database connected, caches warmed). A server that's starting up or shutting down is alive but not ready.

The load balancer is a single point of failure, too

Load balancers are deployed in redundant pairs or clusters, often sharing a virtual IP that moves to the standby if the active one fails, or (in the cloud) as a managed service that's redundant across availability zones. At the very top, DNS spreads users across load balancers in different regions (GeoDNS, anycast).

Sessions and stickiness

Sticky sessions route a user to the same server every time (via a cookie or IP hash). They're sometimes needed for legacy apps or WebSocket connections, but they unbalance load and lose sessions when a server fails. Prefer stateless servers with sessions in a shared store, as in the scaling lesson.

Global load balancing

For worldwide products:

  • GeoDNS answers each user with the nearest region's address.
  • Anycast announces the same IP from many locations; the internet routes users to the closest one (how Cloudflare and Google's front ends work).
  • Each region has its own load balancers; if a region fails, DNS or anycast steers traffic to the next nearest.

Practice

  1. Add "least response time" to the simulation, tracking each server's average duration.
  2. Change the health check to mark a server healthy only after 2 consecutive successes. Why might that be wise?
  3. Design path-based routing: /api/* to API servers, /static/* to a CDN origin, everything else to web servers.
✎ Check your understanding

1.What does a layer 7 load balancer understand that a layer 4 one doesn't?

2.Why is least connections better than round robin when request durations vary a lot?

3.What happens when a server fails its health checks?

← Scaling basics