Command Palette

Search for a command to run...

Hectal
PHASE 5Intermediate ~7 min· topic 1 of 4

Topic 5.1

Load Balancing: L4 vs L7, Algorithms & Health Checks

In one line

A load balancer spreads requests across healthy backends; L4 balancers forward connections, L7 balancers understand requests, and the algorithm plus health checks decide how evenly and safely traffic is spread.

0/4 · 0%

Think of it like this

A supermarket with many checkout counters and a staff member directing customers: 'next free counter' (least connections), 'take turns' (round robin), 'express lane for small baskets' (routing by content), and 'don't send anyone to the closed counter' (health checks).

Key ideas

  1. 01

    L4 balancers forward TCP/UDP connections by IP and port: fast and protocol-agnostic (AWS NLB, HAProxy TCP mode, kube-proxy). L7 balancers parse HTTP and can route by host, path, or header, retry, and add headers (ALB, Nginx, Envoy, ingress controllers).

  2. 02

    Algorithms: ROUND ROBIN (fine when requests cost about the same), LEAST CONNECTIONS (better when costs vary), WEIGHTED (canaries, mixed instance sizes), CONSISTENT HASHING (the same key goes to the same backend, which suits caches; the same idea as ring hashing in the System Design course).

  3. 03

    Health checks: ACTIVE probing of /healthz, and PASSIVE ejection of backends that keep failing (Envoy calls this outlier detection). Check the instance itself, not every dependency (AWS course, Topic 2.4).

  4. 04

    Connection draining lets in-flight requests finish before a backend is removed, which is essential for zero-downtime deploys (CI/CD course, Topic 6.2). Prefer stateless backends so any instance can serve any request.

  5. 05

    STICKY SESSIONS (session affinity) pin a client to one backend, via a load-balancer cookie (ALB target group stickiness, Nginx sticky/hash $cookie_...) or the client IP (ip_hash, Kubernetes sessionAffinity: ClientIP). They exist for apps that keep session state in memory, but they cause uneven load, lost sessions when that backend dies, and awkward deploys. The better fix is shared session storage (Topic 7.2).

  6. 06

    REVERSE vs FORWARD proxy: everything in this phase is a REVERSE proxy, standing in front of servers. A FORWARD proxy stands in front of clients and controls their outbound traffic (Topic 7.5).

Code & diagrams

LbLayersdiagram
Rendering diagram…

Explain it without notes

01

When is least-connections a better algorithm than round robin?

Practice

01

Your health check calls the database. The database has a 30-second hiccup. What happens to your service, and how would you change the check?

Trade-offs

  • ↔

    L7 balancing gives rich routing and observability at the cost of CPU and latency per request; L4 is faster and simpler but can't route by content.

Done when you can

  • I can explain L4 vs L7 load balancing.

  • I can choose between round robin, least connections, weighted, and hashing.

  • I know how to design safe health checks and use connection draining.