CodeWithYash
DSA
Spring Boot
System Design
Series
Articles
Leaderboard
Sign In

Subscribe to Newsletter

Get the latest articles, DSA sheets, and system design challenges delivered to your inbox.

<CODEWITHYASH/>

The ultimate sanctuary for software engineers seeking deep mastery of core technical principles.

Library

  • Articles
  • DSA Sheets
  • Roadmaps

Connect

  • GitHub
  • LinkedIn
  • About
  • Contact

Legal

  • Privacy
  • Terms
  • Support

© 2026 CodeWithYash Global Systems.

Status: OperationalVersion: 4.2.0
System Design
OverviewTheory lessonsHLDLLD
All SeriesSeries
Distributed Systems

System Design Series

0%

0 of 33 topics completed

Progress
Load Balancers & Reverse ProxiesTheory
25 min
API Gateways & Edge RoutingTheory
35 min
Caching Architecture & Critical PathologiesTheory
30 min
Consistent Hashing & Virtual NodesTheory
25 min
Content Delivery Networks (CDNs) & Anycast RoutingTheory
35 min
Storage Engines: B-Trees vs. LSM-TreesTheory
30 min
SQL vs. NoSQL & Data Model SelectionTheory
35 min
Database Scaling: Sharding, Partitioning & ReplicationTheory
30 min
Message Brokers, Event Streaming & Task QueuesTheory
30 min
Rate Limiters & Traffic ShapingTheory
25 min
System DesignCore Infrastructure Building BlocksLoad Balancers & Reverse Proxies

Component Architecture · Phase 2: Core Infrastructure

Load Balancers & Reverse Proxies

Deep dive into Layer 4 (TCP) vs Layer 7 (HTTP) traffic balancing, routing algorithms (Round Robin, Least Connections, P2C), and active vs passive health checks.

Primary specification: Maglev: A Fast and Reliable Software Network Load Balancer (Google)

Written & reviewed by Yash Patel 25 minBaseline 2026.1 Curated Blueprint· Verified 2026-09-14

Lesson 8 of 33

1d streak

The Architectural Question

Application instances experience uneven load distribution and degraded backends continue receiving customer traffic, multiplying response latency.

Core Engineering Objectives

  • Layer 4 transport forwarding (Maglev/IPVS) vs Layer 7 application proxying (Envoy/NGINX)
  • Balancing algorithms: Round Robin, Weighted Least Connections, Power of Two Random Choices (P2C)
  • Readiness probes vs liveness probes and zombie server eviction

Interactive system trace

Watch the architecture make its key decision

Switch scenarios, predict the next active component, and step through the request or failure path.

Normal flow

Load Balancing

GET /products arrives while all three servers are healthy.

ClientLoad balancerServer 1Server 2Server 3
Active:Client: Sends one request to a stable service address.Load balancer: Checks health and applies the routing policy.

Healthy backends

3 / 3

Chosen backend

Pending

Requests in this trace

No dispatch yet

Solid arrow: active transferDashed arrow: available route
Request entersrequest accepted

The client calls one public address instead of knowing every backend.

Why it matters: Backends can be added or replaced without changing the client.

1 / 5
Step 1 of 5

1. What is it & Why it Matters

A load balancer is a reverse proxy that accepts incoming client traffic and distributes it across a pool of healthy backend servers. It acts as the single stable front door for an application.

Why it matters in System Design

A single application server hits physical ceilings: CPU interrupts, open file descriptors, and network interface buffers. A load balancer decouples clients from backend server topology, enabling horizontal autoscaling, health-check failover, and zero-downtime rolling deployments.

2. Layer 4 (Transport) vs. Layer 7 (Application) Balancing

Load balancers operate at different layers of the OSI network model with massive implications for throughput and routing intelligence:

DimensionLayer 4 (L4) Balancer (AWS NLB, Katran, IPVS)Layer 7 (L7) Balancer (Envoy, NGINX, AWS ALB)
OSI LayerLayer 4 (TCP / UDP)Layer 7 (HTTP / HTTPS / gRPC)
Routing BasisIP address + TCP/UDP port (5-tuple hash)HTTP path, headers, cookies, query parameters
TLS HandlingPassthrough (passes encrypted packets to backends)Terminates TLS, decrypts payload, can re-encrypt
Throughput CeilingMassive: > 10,000,000 packets/sec per hostModerate: 50,000 – 150,000 requests/sec per host
Direct Server Return (DSR)Supported: backend replies directly to client, bypassing LBNot possible: proxy must mediate bidirectional HTTP stream
Hardware CostMinimal CPU / memory overheadHigh CPU cost for TLS decryption and header parsing
snippet.texttext
1
2
3
4
5
6
7
8
9
10
LAYER 4 (NLB / IPVS) - PACKET-LEVEL TCP BALANCING:
Client ----[TCP SYN/ACK + Encrypted Packets]----> L4 Balancer ----[Direct Forward]----> Backend Server
(No TLS decryption, 5-tuple hash [Src/Dst IP & Port], >10M pkts/sec, Direct Server Return supported)

LAYER 7 (ALB / Envoy / NGINX) - APPLICATION-LEVEL PROXY:
Client ----[TLS Handshake + HTTP GET /checkout]----> L7 Balancer (Terminates TLS, inspects headers)
                                                           |
                                                           +----(Route /checkout)----> Payment Service
                                                           +----(Route /catalog)-----> Catalog Service
(Decrypts payload, evaluates cookies/JWTs/paths, 50k–150k req/sec)

3. Routing Algorithms & The Fast-Failure Trap

Round Robin

Routes requests sequentially across nodes. Simple and state-free, but slow requests accumulate on unlucky nodes.

Weighted Round Robin

Assigns traffic proportional to static hardware capacity (e.g., an 8-core server receives 2x the requests of a 4-core server).

Least Connections

Routes to the server with the fewest active open sockets. Effective for long-lived sessions, but vulnerable to fast-failure cascades.

Power of Two Choices (P2C)

Picks two healthy nodes at random and routes to the one with lower active connections or latency. Achieves near-optimal load distribution in O(1) time without centralized locks.

Architectural Boundary · Do Not Confuse

Concept A

Least Connections (Dynamic Socket Counting)

Concept B

Power of Two Choices (P2C) with EWMA Latency

Under Least Connections, if Node A crashes and begins instantly returning HTTP 500 in 1ms (instead of normal 100ms processing), its connection count drops to zero. The balancer interprets this as high capacity and diverts 100% of traffic into the dying instance (Fast-Failure Sinkhole). P2C with latency-based weighting prevents this trap.

4. Health Checks, Draining & High Availability

  1. 011. Active Health Checking: The balancer periodically probes backends (e.g., GET /healthz every 5 seconds). If a node fails 3 consecutive probes, it is marked unhealthy and removed from the active pool.
  2. 022. Passive Outlier Detection: The balancer monitors real user traffic in real time. If a node returns 5 consecutive HTTP 5xx errors to live clients, it is ejected immediately for 30 seconds without waiting for the next active probe.
  3. 033. Connection Draining (Graceful Shutdown): When a server is deregistered for deployment, the load balancer stops routing new requests to it while allowing in-flight connections to finish (e.g., 30-second drain window).
  4. 044. Slow Start (Warming Up): When a new node joins, the balancer gradually ramps traffic from 5% to 100% over 60 seconds to allow JIT compilation and local caches to warm up without getting overwhelmed.

Use it when

Deploy an L4 balancer (e.g., AWS NLB) at the outer edge to handle raw high-packet TCP ingestion, and place an L7 balancer (e.g., Envoy) behind it for path-based microservice routing.

Reconsider when

For simple applications under 20,000 QPS, a single managed L7 cloud load balancer (AWS ALB or Cloudflare) is sufficient and eliminates operational complexity.

5. Routing Algorithm Cheat Sheet

AlgorithmAlgorithmic ComplexityState OverheadMajor Weakness / Failure Mode
Round RobinO(1)None (single counter)Slow requests accumulate on unlucky nodes
Weighted Round RobinO(1)Low (static weight map)Does not adapt to runtime dynamic latency spikes
Least ConnectionsO(N)Tracks active socketsFast-Failure Sinkhole: floods crashing nodes
Consistent Hashing (IP)O(log N)Ring token structureNon-uniform traffic from corporate NAT gateways / proxy IPs
Power of Two Choices (P2C)O(1)EWMA latency historySlight variance in load, but avoids herd oscillation

6. Active Recall Checkpoint

Active Recall Checkpoint

What is the "Fast-Failure Sinkhole" pathology in Least Connections load balancing, and how do modern balancers prevent it?

Hint: Think about what happens to the active socket count when a server crashes and returns fast HTTP 500 errors.

Show answer & explanation

When a backend node experiences internal failure (e.g., database connection pool exhaustion) that returns errors faster than healthy nodes, its active connection count can look artificially low. A least-connections balancer may then send it disproportionate traffic Modern balancers prevent this using passive outlier detection (ejecting nodes that emit consecutive 5xx errors) and P2C with response latency tracking.

Hardware Bounds & Sizing Math

Topic sizing snapshot

The four numbers that materially affect this architecture. Use them as bounds, not as a generic calculator embedded in every lesson.

Peak QPS

120,000 RPS

5-Yr Storage

N/A

Bandwidth

15 Gbps

Working Set RAM

32 GB connection state table

Production Postmortem & Verification

Production Incident Postmortem

  1. Trigger / Setup: A cluster uses basic Round Robin routing when one instance suffers an uncaught thread deadlock.
  2. Production Symptom: Every Nth request routed to the deadlocked instance hangs until client timeout, elevating system error rate to 20%.
  3. Root Cause & Fix: Health checks must evaluate deep application readiness, and routing algorithms should track active in-flight connections.

Acceptance Criteria & Outcomes

  • Contrast L4 packet forwarding speed with L7 content-aware routing capabilities.
  • Derive the mathematical advantage of Power of Two Random Choices (P2C) over round robin under heterogeneous workloads.
  • Define timeout, retry, and connection draining policies during rolling deployments.

End-of-Chapter Synthesis & Key Takeaway

A load balancer is an availability and failure-isolation boundary before it is a performance optimization. Always separate Layer 4 (TCP packet routing) from Layer 7 (HTTP header/cookie aware routing), rely on active application readiness probes rather than TCP process checks, and protect downstreams with connection pooling.

Architectural & Technical Interview Review

Core FAANG Interview Questions

3 Questions
Q01
When would you choose a Layer 4 (Transport) balancer over a Layer 7 (Application) balancer in a large-scale architecture?
Click to reveal model answer →Hide answer ↑Requirements & Traffic Distribution

Choose Layer 4 (e.g. AWS NLB, IPVS) when you require ultra-high packet throughput (>millions of packets/s), sub-millisecond wire latency, or protocol transparency (raw TCP/UDP/WebSockets) without TLS termination overhead. Choose Layer 7 (e.g. Nginx, Envoy, AWS ALB) when routing decisions depend on HTTP host headers, URL paths, cookie-based session pinning, gRPC method paths, or request body payload inspection.

Source section: Requirements & Traffic Distribution
Q02
Why should backend health checks verify deep dependency readiness rather than just shallow process liveliness?
Click to reveal model answer →Hide answer ↑Request & Failure Flows

A web process can be alive in memory (returning HTTP 200 on /ping) while its database connection pool is completely exhausted, its local disk is 100% full, or essential cache clusters are unreachable. Routing traffic to such an instance causes cascading 500 errors. Readiness checks verify that the instance can actually fulfill end-to-end work right now.

Source section: Request & Failure Flows
Q03
How do you prevent a single slow backend server from triggering a thundering herd failure across the entire fleet?
Click to reveal model answer →Hide answer ↑Architecture Trade-offs

Implement Least Connections or Peak EWMA routing algorithms rather than naive Round Robin so loaded nodes receive fewer incoming requests. Couple this with aggressive connect/read timeouts, circuit breakers that eject degraded instances after N consecutive timeouts, and bounded request queues with load shedding.

Source section: Architecture Trade-offs

Production Pitfalls & Anti-Patterns

Critical Failure Anti-Patterns

!

Sticky Sessions Memory Leaks and Imbalance

Pinning clients to specific servers via cookies prevents even load distribution, creates single-server memory hotspots, and breaks user sessions whenever that backend node restarts or autoscales.

Production Mitigation: Store session state externally in an in-memory Redis cluster or use signed stateless JWT tokens.

!

Health Check DDoS Amplification

Aggressive health checks configured to hit expensive database queries every 1 second from 10 redundant load balancers can generate thousands of queries per second, saturating the database during outages.

Production Mitigation: Keep health check endpoints lightweight and cache the internal health status for 2-3 seconds.

!

Single Load Balancer Single Point of Failure (SPOF)

Placing all application traffic behind a single balancer instance makes any network or hardware glitch a full site outage.

Production Mitigation: Deploy redundant balancers in an active-passive setup with floating Virtual IP (VRRP / Keepalived) or active-active via Anycast BGP routing.

Primary Documentation & Specifications

Maglev: A Fast and Reliable Software Network Load Balancer (Google)The Power of Two Choices in Randomized Load Balancing (Mitzenmacher)
Previous LessonCAP Theorem & PACELC FrameworkFoundationsNext LessonAPI Gateways & Edge RoutingCore Infrastructure

In this chapter

01Architectural Question02Interactive Model031. What is it & Why it Matters042. Layer 4 (Transport) vs. Layer 7 (Application) Balancing053. Routing Algorithms & The Fast-Failure Trap064. Health Checks, Draining & High Availability075. Routing Algorithm Cheat Sheet086. Active Recall Checkpoint09Sizing Snapshot10Production Verification11Synthesis & Review12Primary References

Chapter promise

Layer 4 transport forwarding (Maglev/IPVS) vs Layer 7 application proxying (Envoy/NGINX)