Skip to main content

Load Balancing & Service Reliability

Comparative Architecture

To understand how a Load Balancer differs from a Reverse Proxy and an API Gateway, and how they coexist in a production network path, see the Reverse Proxy vs. Load Balancer vs. API Gateway Guide.


Load Balancing Algorithms

AlgorithmHowBest For
Round RobinRotate through servers in orderEqual capacity servers, stateless
Weighted Round RobinMore requests to higher-weight serversMixed capacity servers
Least ConnectionsRoute to server with fewest active connectionsVariable request durations
IP HashHash client IP β†’ same serverSession affinity, WebSocket
RandomRandom server selectionSimple, low overhead
Resource-basedRoute based on CPU/memoryCPU-intensive workloads

L4 vs L7 Load Balancing

L4 (Transport)L7 (Application)
Works atTCP/UDP levelHTTP/HTTPS level
Content awarenessNo (binary stream)Yes (URL, headers, cookies)
TLS terminationNoYes
Routing byIP:portURL path, headers, cookies
PerformanceHigherSlightly lower (parses headers)
ExamplesAWS NLB, HAProxy (L4 mode)AWS ALB, nginx, Envoy

Load Balancer Architecture

Load Balancer Architecture Path
Client (User)DNS ResolutionGlobal LB (Anycast)Regional LB (L7)Service Pools & Mesh

Global Load Balancer

Uses Anycast routing to advertise a single IP address globally. Directs packets over private fiber to the closest data center.

  • Layer 4 (TCP/UDP) routing via Border Gateway Protocol (BGP).
  • Absorbs massive distributed DDoS attacks at edge PoPs.
  • Passes traffic to local application balancers.

πŸ’‘ Click any diagram node to inspect details.


Health Checks

Types

TypeDescriptionUse
PassiveTrack errors/timeouts on live trafficReal traffic quality signal
ActivePeriodic synthetic request to health endpointProactive failure detection
HybridBoth passive + activeBest coverage
# nginx health check config
upstream backend {
server app1:8080;
server app2:8080;

check interval=3000 rise=2 fall=3 timeout=1000 type=http;
check_http_send "GET /actuator/health HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx;
}

Kubernetes Probes

livenessProbe: # Is the container alive? Restart if fails.
httpGet:
path: /actuator/health/liveness
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
failureThreshold: 3

readinessProbe: # Is the container ready to serve traffic? Remove from LB if fails.
httpGet:
path: /actuator/health/readiness
port: 8080
periodSeconds: 5
failureThreshold: 2

startupProbe: # Is the app done starting? Don't liveness-kill a slow start.
httpGet:
path: /actuator/health
port: 8080
failureThreshold: 30
periodSeconds: 10

The Deep Health Check Trap (Cascading Failure Anti-Pattern)

A frequent and catastrophic failure mode in distributed systems is configuring a load balancer or Kubernetes probe to perform a deep health check that queries downstream databases, caches, or third-party APIs:

[ Load Balancer Probe: GET /health/deep ] ──▢ [ App Pod ] ──▢ [ Database: SELECT 1 FROM orders ]
CASCADING COLLAPSE TIMELINE:
T=00s: Database experiences transient lock contention or unindexed query spike.
T=05s: Database query latency spikes from 2ms to 3,000ms.
T=10s: Load balancer health probe times out on App Pod 1; marks it UNHEALTHY.
T=15s: Health checks fail on App Pods 2 through 50 simultaneously.
T=20s: Load balancer evicts 100% of healthy application pods from the target group!
RESULT: A transient database slow-down causes a 100% catastrophic total application outage.

The Architectural Fix:

  • Liveness & Ingress Health Checks MUST be Shallow: Probe only local process viability (e.g. GET /health/live checking JVM thread availability and local HTTP server response in < 2ms). Never check downstream networked dependencies in an ingress health probe.
  • Deep Health Probes (/health/deep) are for Telemetry & Alerting Only: Expose deep dependency status to Prometheus/Datadog dashboards and SRE on-call pagers, but decouple them from automated traffic kill-switches.

Single Point of Failure (SPOF) Analysis Across Every Tier

True service reliability requires eliminating Single Points of Failure at every layer of the distributed request pipeline:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ SPOF MITIGATION BY LAYER β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ ARCHITECTURAL TIERβ”‚ SPOF VULNERABILITY β”‚ PRODUCTION MITIGATION β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1. DNS Layer β”‚ Single authoritative β”‚ Multi-provider Anycast DNS β”‚
β”‚ β”‚ DNS provider outage β”‚ (Route 53 + Cloudflare) β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 2. Load Balancer β”‚ Single proxy host crash β”‚ Keepalived / VRRP Floating IP β”‚
β”‚ β”‚ or interface failure β”‚ or ECMP BGP Active-Active β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 3. Application β”‚ Host crash or AZ outage β”‚ Multi-AZ stateless fleet with β”‚
β”‚ β”‚ taking down instances β”‚ pod anti-affinity rules β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 4. Cache Tier β”‚ In-memory node reboot β”‚ Redis Cluster with 3+ masters β”‚
β”‚ β”‚ drops all session state β”‚ and Sentinel auto-failover β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 5. Database Tier β”‚ Primary node disk crash β”‚ Semi-sync replication, Raft β”‚
β”‚ β”‚ or network split β”‚ quorum, automated promotion β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Session Persistence: Sticky Sessions vs Externalized Backplane

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ SESSION PERSISTENCE STYLES β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ COOKIE-BASED STICKY SESSIONS β”‚ EXTERNALIZED STATELESS SESSIONS β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β€’ LB injects tracking cookie β”‚ β€’ App instances are 100% stateless β”‚
β”‚ (e.g., AWSALB=Server-02) β”‚ β€’ Sessions stored in Redis Cluster β”‚
β”‚ β€’ Client pinned to same server node β”‚ β€’ Any pod can process any request β”‚
β”‚ β€’ Drawbacks: Uneven traffic skew, β”‚ β€’ Advantages: Uniform load balance, β”‚
β”‚ cache hot-spots, session loss β”‚ seamless autoscaling, zero data β”‚
β”‚ when pinned server is terminated β”‚ loss on pod restarts or deploys β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Failover Strategies

Failover Strategies Explorer
ClientLoad BalancerPrimary ServerACTIVE (100% Load)Standby ServerIDLE (Hot Standby)Heartbeat OK

Active-Passive Strategy

One active primary processes 100% of the traffic. A secondary (standby) is kept synchronized but idle.

Status Log:
🟒 Primary Server running. Standby synchronizing state.
  • RTO (Recovery Time): 1–5 minutes (DNS propagation / VIP sweep).
  • RPO (Data Loss): Depends on replication mode (Async vs Sync).
Multi-Region Active-Active Replication Failover
Region A (US East)Region B (EU West)US ClientEU ClientALB us-eastApp ServersDB us-eastALB eu-westApp ServersDB eu-west

Multi-Region Deployment

Normally, clients connect to their closest region. Databases replicate continuously across regions.

Traffic Status:
🟒 Region-aware routing functioning. Synchronizing cross-region databases.

Graceful Degradation

Design systems to provide reduced functionality rather than complete failure.

@CircuitBreaker(name = "recommendations", fallbackMethod = "defaultRecommendations")
public List<Product> getRecommendations(Long userId) {
return recommendationService.getPersonalized(userId);
}

// Fallback: show popular items instead of personalized
public List<Product> defaultRecommendations(Long userId, Exception ex) {
log.warn("Recommendation service unavailable, using popular items fallback");
return productService.getMostPopular(10);
}

Degradation Levels

Graceful Degradation Levels

Level 0: Full Functionality

All systems green. Normal latency and full database writes/reads.

FEATURE MATRIX STATUS:
Reads (Catalog, User profile)
AvailableDirectly from DB / local cache.
Writes (Orders, Checkouts)
AvailableTransactions executed synchronously.
AI Recommendations
AvailablePersonalized models executed.
ARCHITECTURE / IMPLEMENTATION PATTERN:
// Full latency path
public List<Product> getRecommendations(Long userId) {
    return recommendationService.getPersonalized(userId);
}

Chaos Engineering

Deliberately inject failures to find weaknesses before users do.

Principles

  1. Define steady state (normal behavior)
  2. Hypothesize what will happen during failure
  3. Introduce failure in controlled way
  4. Compare actual vs expected

Common Chaos Experiments

ExperimentWhat to Test
Kill random podService resilience, restart behavior
Introduce network latencyTimeout handling, circuit breakers
Drop packetsRetry logic, idempotency
Exhaust connection poolBackpressure, error handling
Spike CPU to 90%Autoscaling, latency under load
Kill a DB replicaFailover, replication handling

Chaos Monkey (Spring Boot)

// Chaos Monkey for Spring Boot (Netflix Chaos Monkey style)
@ChaosMonkey(
assaults = {LatencyAssault.class},
watcher = {ServiceWatcher.class}
)
@Service
public class OrderService { ... }

Disaster Recovery

RTO & RPO Targets

TierRTORPOStrategy
Tier 1 (Critical)< 1 hour~0 (zero data loss)Active-active, synchronous replication
Tier 2 (Important)< 4 hours< 1 hourActive-passive, async replication
Tier 3 (Standard)< 24 hours< 24 hoursBackup + restore
Tier 4 (Low)< 72 hours< 72 hoursPeriodic backup

DR Runbook Checklist

  • Identify failed component(s)
  • Assess data loss window (check last replication timestamp)
  • Activate DR environment
  • Point DNS to DR
  • Verify functionality with smoke tests
  • Notify stakeholders
  • Document incident timeline
  • Post-mortem within 48 hours

Zero-Downtime Deployments

Zero-Downtime Deployment Strategies
User TrafficRouter / LBBlue Cluster (v1)100% TrafficGreen Cluster (v2)0% Traffic (Staging)

Blue-Green Deployment

Two identical physical environments are maintained. Traffic is switched atomically by updating the load balancer rules.

Operational Log:
πŸ”΅ Running version 1.0.0 in Blue environment. Green is idle and ready for deployment.
# Kubernetes canary with Argo Rollouts
apiVersion: argoproj.io/v1alpha1
kind: Rollout
spec:
strategy:
canary:
steps:
- setWeight: 5 # 5% to canary
- pause: {duration: 10m}
- setWeight: 25
- pause: {duration: 10m}
- setWeight: 100

Anycast

CDNs use anycast to route users to the nearest PoP automatically.

Anycast Routing Mechanics
US ClientEU ClientShared IP: 104.16.82.100 (Announced globally via BGP)US Edge PoPIP: 104.16.82.100EU Edge PoPIP: 104.16.82.100

Anycast Routing

In Anycast, multiple physically separate servers announce the exact same IP address to the internet using **BGP (Border Gateway Protocol)**.

πŸ‡ΊπŸ‡Έ Routing client in US:

Internet routers identify the US Edge PoP as the shortest topological path. Packets arrive at the US server instantly.

πŸ’‘ Click on either client node to see where their traffic is routed.

Used by: Cloudflare, Google (8.8.8.8), root DNS servers.


Session Persistence (Sticky Sessions)

When application state is stored in-server memory, all requests from a user must go to the same server.

Session Persistence & Affinity Models
Alice (Client)Sticky LBServer AActive (Session: Alice)Server BActive (Session: Empty)

Sticky Sessions

The Load Balancer uses cookies or IP hashing to pin a user's session to a specific backend server.

System Impact:
🟒 Active. LB uses cookie `SERVERID=server-a` to keep Alice pinned to Server A.
Best Practice

Avoid sticky sessions when possible. Store session data in a distributed cache (Redis) so any server can handle any request β€” true horizontal scaling.


nginx Load Balancer Configuration

upstream api_servers {
least_conn; # algorithm

server 10.0.0.1:8080 weight=3;
server 10.0.0.2:8080 weight=2;
server 10.0.0.3:8080 backup; # only used when others are down

keepalive 32; # keep N idle upstream connections open
}

server {
listen 80;
server_name api.example.com;

location /api/ {
proxy_pass http://api_servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

proxy_connect_timeout 5s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}

location /static/ {
root /var/www;
expires 30d;
add_header Cache-Control "public, immutable";
}
}

Global Server Load Balancing (GSLB)

Distributes traffic across multiple data centers globally using DNS:

Global Server Load Balancing (GSLB) Modes
US ClientEU ClientGSLB Server(Route 53 / DNS)Data Center AUS East (203.0.113.10)Data Center BEU West (198.51.100.20)

Latency-Based Routing

GSLB resolves the DNS query to the IP of the data center topologically nearest to the client, minimizing round-trip times.

DNS Resolution Log:
🌐 US user resolved to 203.0.113.10 (DC A).
🌐 EU user resolved to 198.51.100.20 (DC B).

Interview Questions

Q: What load balancing algorithm would you use for WebSocket connections? Why?

A: Use least-connections (or weighted least-connections) because WebSockets are long-lived and uneven. It balances concurrent connection load better than round-robin.

Q: What is the difference between L4 and L7 load balancing?

A: L4 routes by IP/port and is fast/protocol-agnostic; L7 routes by HTTP attributes like path, host, or headers. L7 enables smarter routing and policy but adds more processing overhead.

Q: What is the difference between liveness and readiness probes in Kubernetes?

A: Liveness checks if container should be restarted; readiness checks if it can serve traffic now. Readiness protects rollout and dependency warm-up phases.

Q: What is blue-green deployment and how does it enable zero-downtime releases?

A: Run old and new stacks in parallel, then switch traffic atomically to the new stack. Rollback is fast by flipping traffic back to the old environment.

Q: What is canary deployment? How do you decide when to proceed vs rollback?

A: Canary sends a small traffic slice to new version first and expands gradually. Advance only if error/latency/business KPIs stay within guardrails; otherwise auto-rollback.

Q: What is chaos engineering and why is it important?

A: Chaos engineering injects controlled faults to validate resilience assumptions in production-like conditions. It exposes hidden coupling before real incidents do.

Q: What is RPO and RTO? How do you design a system to meet given targets?

A: RPO is acceptable data loss window; RTO is acceptable recovery time. Meet targets with replication frequency, backup strategy, failover automation, and regular disaster drills.

Q: How do you implement graceful degradation in a microservices system?

A: Prioritize core paths and shed optional features during overload/failure. Use timeouts, fallbacks, cached defaults, and feature flags to keep essential functionality alive.

Q: What is the difference between active-passive and active-active failover?

A: Active-passive keeps standby idle until failover, simplifying consistency but increasing failover delay. Active-active serves traffic in multiple sites continuously, improving availability but complicating conflict handling.

Q: How do you design a multi-region system that remains consistent during a regional outage?

A: Classify data by consistency need: strongly consistent writes via quorum/primary strategy, and eventually consistent data via async replication. Automate failover with clear write-routing and reconciliation plans.

CDN & Network Level Load Balancing Questions

Q1. What is the difference between L4 and L7 load balancing?

L4 (transport layer) load balancing operates on TCP/UDP β€” it sees source/destination IP and port but not application data. Fast, protocol-agnostic, but can't route by URL path or headers. L7 (application layer) operates on HTTP β€” it can route by URL, headers, cookies, method; terminate TLS; perform content-based routing; modify requests. More overhead but much more flexible.

Q2. What is the difference between round-robin and least-connections algorithms?

Round-robin cycles requests evenly across servers β€” good for stateless apps where each request takes similar time. Least-connections sends to the server with fewest active connections β€” better when request duration varies significantly (e.g., some requests take 100ms, others take 10s). Least-connections prevents piling requests onto a slow server that's busy with long operations.

Q3. What are sticky sessions and why are they problematic at scale?

Sticky sessions (session affinity) ensure all requests from one user go to the same backend server β€” needed when session state is in server memory. Problems: uneven load distribution, a server crash loses all its users' sessions, prevents true horizontal scaling. Solution: externalize session state to Redis (or a database), making all servers stateless and interchangeable.

Q4. How does anycast work and where is it used?

Anycast announces the same IP address from multiple geographic locations. BGP routing naturally sends packets to the topologically nearest location announcing that prefix. There's no DNS trick β€” the internet's routing infrastructure handles it. Used by: CDNs (serve from nearest PoP), public DNS resolvers (8.8.8.8 works from anywhere), root DNS servers, DDoS mitigation (absorb attacks at multiple locations).

Q5. What is the purpose of health checks in load balancing?

Health checks detect unhealthy backends before they cause user-facing errors. Active checks proactively send probes (HTTP GET to /health) and mark backends down before failures impact traffic. Passive checks detect failures from live traffic patterns. Without health checks, the LB would send requests to dead servers, causing errors for those users until the problem is noticed manually.

Q6. What is cache busting and why is it needed with CDNs?

When static assets (JS, CSS) are updated but clients have cached the old version, users see outdated code. Cache busting adds a content hash to filenames (app.a3f4b.js) so each new version has a unique URL β€” never cached as the old version. CDN serves app.a3f4b.js with very long TTL (immutable); after deployment, the HTML references app.c7d2a.js (new hash) β€” a fresh request.

Q7. How would you design a load balancer health check for a Spring Boot application?

Expose Spring Boot Actuator's /actuator/health endpoint. Configure it to check database connectivity, cache availability, and any critical dependencies. Return HTTP 200 when healthy, 503 when not. Configure the LB to send GET /actuator/health every 10–30s, mark unhealthy after 2–3 failures, mark healthy after 2 successes. Protect the endpoint β€” restrict access to LB's IP range or use a separate management port.

Q8. Explain CDN cache invalidation strategies.

Options: (1) TTL expiry β€” wait for TTL to expire (simplest; acceptable for slow-changing content); (2) Versioned URLs β€” change the URL on update (hash in filename); cache old versions expire naturally; (3) API purge β€” call CDN's purge API to immediately invalidate specific URLs or patterns (Cloudflare, Fastly, CloudFront all support this); (4) Cache tags/surrogate keys β€” tag cached objects and purge all objects with a tag (e.g., purge all objects tagged product:42).


Deep Dive: Load Balancers Architecture (L4 vs. L7, DSR, Maglev, and HA Topologies)

Reference: Sujeet Pro β€” Load Balancers Architecture

In hyperscale production networks, load balancing is never a single applianceβ€”it is a multi-tiered packet and connection pipeline separating raw transport multiplexing (Layer 4) from application-aware policy enforcement (Layer 7).

1. L4 Transport Load Balancing Under the Hood (NAT, DSR, IPVS/eBPF, Maglev)

Layer 4 load balancers operate on TCP/UDP segments (5-tuple: source IP, source port, destination IP, destination port, protocol) without terminating the TCP state machine or parsing application payloads (though stateful L4 proxies track SYN/FIN/RST flags in a connection table).

L4 Forwarding ModePacket Path & MechanicsProsCons / Operational Gotchas
Stateful DNAT / Full NAT (SNAT + DNAT)LB rewrites destination IP to backend IP (DNAT) and often rewrites source IP to LB IP (SNAT) so return packets traverse the LB.Works across routed subnets and VPCs without L2 adjacency.LB sits on the egress return path (bandwidth bottleneck); backend loses original client IP unless Proxy Protocol v2 or TCP option headers are injected.
Direct Server Return (DSR / L3DSR)LB only rewrites the L2 destination MAC address (or encapsulates in IP-in-IP / GRE / GUE) and forwards to the backend, which owns the Virtual IP (VIP) on its loopback interface (lo, arp_ignore=1, arp_announce=2). Backend replies directly to the client over the internet, bypassing the LB on the return path.Asymmetric return bandwidth (ideal when egress responses are 10Γ—10\times–100Γ—100\times larger than ingress requests, e.g., video/CDN/blob reads).Requires L2 adjacency (or L3 IP-in-IP encapsulation like Katran/Maglev); cannot perform L7 TLS termination at the DSR tier.
Linux IPVS vs. eBPF/XDP (Katran / Cilium)Traditional iptables evaluates rules sequentially in O(N)O(N); IPVS uses Netfilter kernel hash tables in O(1)O(1) but still allocates an sk_buff per packet. eBPF + XDP processes packets directly in the NIC driver ring buffer before kernel sk_buff allocation.eBPF/XDP achieves 10M–40M+ packets/sec per core with near-zero context switch overhead.Requires modern Linux kernels (β‰₯5.4\ge 5.4) and NIC drivers supporting native XDP.

Google Maglev & ECMP BGP Anycast​

At the network edge, routers distribute packets across a fleet of L4 software load balancers using ECMP (Equal-Cost Multi-Path) routing via BGP Anycast. However, ECMP routers perform stateless per-flow hashing (hash(5-tuple) % N_LBs). If one L4 LB node joins or leaves the BGP pool, ECMP re-hashes flows across the remaining nodes, breaking active TCP connections if the new LB routes the packet to a different backend server.

Maglev Consistent Hashing solves this without inter-LB synchronization:

  1. Every Maglev LB builds an identical, deterministic lookup table of prime size MM (e.g., M=65537M = 65537) using two independent hash functions h1h_1 and h2h_2 to generate a permutation sequence for each backend: offset=h1(backend)β€Šmodβ€ŠM,skip=(h2(backend)β€Šmodβ€Š(Mβˆ’1))+1\text{offset} = h_1(\text{backend}) \bmod M, \quad \text{skip} = (h_2(\text{backend}) \bmod (M - 1)) + 1
  2. Backends take turns filling empty slots in the table of size MM. Lookup is a single O(1)O(1) array index read (table[hash(5-tuple) % M]), rather than an O(log⁑N)O(\log N) binary search on a consistent hash ring.
  3. Even if an ECMP router shifts an in-flight TCP flow from Maglev Node A to Maglev Node B during a route flap, both nodes compute the exact same backend server deterministically, preserving the TCP connection without cross-node state replication.

2. L7 Application Load Balancing (TLS Termination & Envoy/NGINX Mechanics)

An L7 load balancer is a terminating proxy: it completes the three-way TCP handshake and TLS handshake with the client (Socket A), parses the HTTP/1.1, HTTP/2, or HTTP/3 frames, applies routing rules (Host, path, headers, cookies, gRPC method), and multiplexes requests onto a warm pool of persistent backend TCP connections (Socket B).

  • Connection Multiplexing & Keep-Alive Amortization: 100,000 concurrent mobile clients over high-RTT cellular networks terminate on the L7 proxy. The L7 proxy coalesces those requests onto just a few hundred pre-warmed, low-RTT HTTP/1.1 keep-alive or HTTP/2 multiplexed connections to backend pods, shielding application servers from connection descriptor exhaustion and TLS handshake CPU spikes.
  • TLS Termination vs. TLS Passthrough vs. mTLS Re-encryption:
    • Edge Termination: Decrypts at L7 LB; forwards plaintext HTTP over the isolated VPC. Fastest, enables L7 inspection/WAF, but violates zero-trust internal compliance if the VPC network is shared.
    • mTLS Re-encryption (Strict Zero-Trust): Terminates public TLS at L7 LB, inspects headers, and re-encrypts over internal mTLS (using short-lived SPIFFE/SPIRE certificates) to backend pods, reusing persistent upstream TLS sessions to amortize handshake latency.

3. Advanced Load Balancing Algorithms: P2C & Bounded Consistent Hashing

While static Round Robin and naive Least Connections work for small homogeneous pools, they break down under tail-latency skew, heterogeneous hardware, or fleet-wide non-synchronized L7 proxy deployments:

AlgorithmMathematical / System MechanismWhy It Wins at Scale
Power of Two Random Choices (P2C)Instead of scanning all NN backends to find the global minimum active connections (which causes herd behavior when 50 independent Envoy proxies simultaneously pile traffic onto the single lowest-loaded node), pick 2 backends at random and send the request to the one with fewer active requests (or lower EWMA latency).Reduces max queue length exponentially from O(log⁑Nlog⁑log⁑N)O(\frac{\log N}{\log \log N}) (pure random) down to O(log⁑log⁑N)O(\log \log N) with O(1)O(1) selection cost and zero coordination across distributed LBs.
Peak EWMA (Exponentially Weighted Moving Average)Weights active connection count by exponentially smoothed round-trip latency (Lewma=Ξ±β‹…RTTsample+(1βˆ’Ξ±)β‹…LprevL_{\text{ewma}} = \alpha \cdot \text{RTT}_{\text{sample}} + (1 - \alpha) \cdot L_{\text{prev}}).Immediately routes around a backend experiencing GC pauses or slow disk I/O before its active connection count even appears high.
Consistent Hashing with Bounded LoadsPlaces backends on a virtual hash ring (with VV virtual nodes per physical server) for cache locality, but caps each server's load at c=(1+Ο΅)Γ—TotalΒ LoadNc = (1 + \epsilon) \times \frac{\text{Total Load}}{N} (where Ο΅β‰ˆ0.25\epsilon \approx 0.25). If the target server exceeds capacity cc, the request spills over clockwise to the next non-full node.Prevents a single hot tenant/key from melting down its assigned cache node while preserving >80%>80\% cache hit affinity for normal keys.

4. Active vs. Passive Health Checks (Outlier Detection) & Graceful Connection Draining

Relying solely on active periodic probes (GET /healthz every 5s) leaves a 2.5s–15s blackhole window where a crashed or deadlocked pod continues receiving and failing live traffic.

  • Passive Health Checks (Envoy Outlier Detection): Monitors live production traffic responses in real time. If a backend returns KK consecutive 5xx errors, connection resets, or exceeds 3Γ—3\times the cluster median latency, the L7 proxy immediately ejects the host for a base ejection time (30sΓ—consecutiveΒ ejections30\text{s} \times \text{consecutive ejections}), capped by a Panic Threshold (e.g., never eject more than 50% of the pool; if >50%>50\% fail, fail open and route to all nodes to avoid cascading collapse).
  • Graceful Connection Draining (Zero-Downtime Rolling Deploys): When a pod receives SIGTERM in Kubernetes, kube-proxy / Envoy endpoint removal and the pod's SIGTERM handler happen asynchronously in parallel, not sequentially!
    1. Pod receives SIGTERM and immediately flips its /health/readiness endpoint to return HTTP 503.
    2. Pod does not close its listening socket yet (preStop: sleep 5–10s) because upstream LBs take several seconds to observe the readiness failure or receive endpoint removal via xDS/kube-proxy and stop sending new requests.
    3. LB enters DRAINING state: stops dispatching new requests to the pod while allowing in-flight requests (and HTTP/2 streams via GOAWAY frames) up to drain_timeout (e.g., 30s) to finish cleanly before SIGKILL.

5. High Availability Topologies: Active-Passive (VRRP) vs. Active-Active (BGP ECMP)

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ ACTIVE-PASSIVE (VRRP / Keepalived) β”‚ ACTIVE-ACTIVE (BGP ECMP + Maglev) β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β€’ 2 LB nodes share a Virtual IP (VIP) β”‚ β€’ N LB nodes announce the same Anycast β”‚
β”‚ via VRRP multicast heartbeats (1s). β”‚ VIP to ToR (Top-of-Rack) routers β”‚
β”‚ β€’ Master holds VIP; Backup sits idle. β”‚ via BGP peering. β”‚
β”‚ β€’ On Master crash, Backup issues β”‚ β€’ ToR routers split flows across all N β”‚
β”‚ Gratuitous ARP (GARP) to claim VIP. β”‚ LBs via ECMP 5-tuple hashing. β”‚
β”‚ β€’ Bottleneck: Max throughput = 1 node; β”‚ β€’ Scales horizontally to Tbps; L4 Maglevβ”‚
β”‚ 3s failover stall; cold TCP state β”‚ tier feeds a horizontally scaled L7 β”‚
β”‚ unless conntrackd syncs state table. β”‚ Envoy/NGINX fleet. β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Senior/Staff Interview Playbook: How to Ace Load Balancing & Reliability Interviews

1. The Senior Framing Strategy (First 60 Seconds)

When asked to design or scale the ingress / load balancing tier of a distributed system, establish physical invariants and failure boundaries immediately:

  1. Separate L4 Multiplexing from L7 Policy: Never expose L7 Envoy/NGINX directly to the raw internet without an L4 tier (BGP ECMP + Maglev/NLB) in front to absorb volumetric SYN floods and distribute TCP connections horizontally.
  2. Back-of-the-Envelope Capacity Math (Little's Law & Ephemeral Ports):
    • By Little's Law (L=λ×WL = \lambda \times W), at Ξ»=200,000Β QPS\lambda = 200,000\text{ QPS} and average response latency W=50msW = 50\text{ms}, there are 10,00010,000 concurrent active requests, plus hundreds of thousands of idle keep-alive sockets.
    • Ephemeral Port Exhaustion: An L7 proxy connecting to a single backend IP:Port uses the 4-tuple (LB_IP, Ephemeral_Port, Backend_IP, Backend_Port). Because ephemeral ports are 16-bit (∼64,000\sim 64,000 max), a single L7 LB IP can only open ∼60k\sim 60\text{k} concurrent TCP connections per backend IP:Port. If TIME_WAIT (60s) accumulates or connections aren't pooled with HTTP keep-alive/HTTP/2, the LB runs out of ports (EADDRNOTAVAIL). Fix by enabling upstream connection pooling, SO_REUSEADDR/tcp_tw_reuse, and assigning multiple source Secondary IPs to the LB.
  3. Define Failure Domains & Blast Radius: Ensure shallow health checks, configure a 50% panic threshold on outlier detection so a downstream blip never evicts the entire fleet, and enforce multi-AZ cross-zone routing limits.
  4. Day-2 Operational Observability: Track LB HTTP 502 (backend reset/refused), HTTP 503 (no healthy upstream / surge queue overflow), HTTP 504 (upstream timeout), and TLS handshake CPU saturation separately from backend application metrics.

2. Classic Interviewer Traps & Principal Rebuttals

Trap 1: "We have 20 Envoy L7 load balancers using Least Connections, so traffic will never overload a single slow backend pod."​

  • Junior/Mid Answer: "Yes, Least Connections tracks active requests per pod and always routes to the least busy server, so slow pods won't get overloaded."
  • Principal Rebuttal: "In a distributed fleet of 20 uncoordinated L7 load balancers, deterministic Least Connections causes herd synchronization. Each Envoy instance only sees its local view of active connections, or if metrics are gossiped periodically, all 20 LBs see Pod X has 0 connections at the exact same millisecond and simultaneously dump a wave of requests onto it. Furthermore, a pod throwing immediate HTTP 500 errors in 0.5ms0.5\text{ms} has near-zero active connections, acting as a blackhole magnet that attracts 90% of cluster traffic under Least Connections! The production fix is Power of Two Random Choices (P2C) weighted by Peak EWMA latency coupled with Passive Outlier Detection to immediately eject fast-failing 5xx nodes."

Trap 2: "During Kubernetes rolling deployments, we still see a spike of HTTP 502 Bad Gateway errors even though we configured Readiness Probes."​

  • Junior/Mid Answer: "We should decrease the periodSeconds on the readiness probe so Kubernetes detects the pod is stopping faster."
  • Principal Rebuttal: "Lowering probe intervals does not fix the fundamental distributed race condition during pod termination. When Kubernetes deletes a Pod, the kubelet sends SIGTERM to the container at the exact same moment the Endpoints controller begins propagating the IP removal to kube-proxy (iptables/IPVS) and Ingress Envoy workers. If the Spring Boot/Node.js app closes its listening socket immediately on SIGTERM, in-flight packets from LBs whose xDS/iptables tables are 500ms stale hit a closed socket (RST β†’\rightarrow HTTP 502). A Principal architecture enforces a two-phase drain: (1) a preStop hook (sleep 5 or flipping readiness to 503 while keeping the HTTP listener open) so all LBs drain the endpoint first, and (2) server.shutdown=graceful with HTTP/2 GOAWAY signaling to drain active requests before socket closure."

Trap 3: "Let's turn on Direct Server Return (DSR) on our Load Balancer AND have the Load Balancer terminate TLS and route /api/v1 vs /api/v2."​

  • Junior/Mid Answer: "Sure, DSR will speed up the response path while the LB terminates TLS and routes by URL path."
  • Principal Rebuttal: "That is physically impossible at the network protocol layer. DSR is strictly a Layer 4 mechanism where the load balancer forwards raw packets to the backend without terminating the TCP or TLS state machine, allowing the backend to send return TCP packets directly to the client IP. If the LB terminates TLS or inspects HTTP Layer 7 paths, the LB must complete the TCP 3-way handshake and TLS key exchange with the client using its own sequence numbers and session keys. The backend would have no knowledge of the client's TCP sequence numbers or TLS encryption keys to encrypt and return packets directly. To combine both benefits, you deploy a two-tier topology: an L4 DSR/Maglev tier at the edge routing to a horizontal fleet of L7 Envoy proxies (which terminate TLS and route paths), or offload static large-byte responses to a CDN edge."

Trap 4: "To make sure our load balancer never sends traffic to a broken instance, our /health endpoint checks PostgreSQL, Redis, and our downstream Payment API."​

  • Junior/Mid Answer: "That's a great best practice because if the database is unreachable, the pod shouldn't receive traffic."
  • Principal Rebuttal: "Coupling ingress load balancer health checks to shared downstream dependencies is the #1 cause of total cascading fleet blackouts. If PostgreSQL experiences a 3-second lock spike or a Redis failover occurs, every pod fails its deep health check simultaneously, causing the load balancer to mark 100% of the fleet unhealthyβ€”unless a Panic Threshold (e.g., Envoy's default 50% threshold) overrides health status and forces fail-open routing. Ingress health checks must be strictly shallow (verifying local event-loop/thread-pool responsiveness and local warmup), while downstream dependency failures must be isolated per-route via L7 Circuit Breakers (Resilience4j) and graceful fallback responses."

3. Quick Reference β€” Mid-Level vs. Principal Candidate Cheat Sheet

Concept / ScenarioMid-Level Candidate SaysPrincipal Candidate Demonstrates
Ingress Topology"Put an NGINX or AWS ALB in front of the backend servers.""Use a two-tier architecture: BGP Anycast + L4 ECMP/Maglev (eBPF/XDP) for volumetric packet scaling, fronting a stateless fleet of L7 Envoy proxies for TLS termination and path routing."
Algorithm Choice"Use Round Robin for even servers, or Least Connections for uneven requests.""Use Power of Two Choices (P2C) with Peak EWMA latency for general RPCs to avoid herd synchronization, or Consistent Hashing with Bounded Loads when L1 cache affinity is required."
L4 Return Path Scaling"Scale up the load balancer bandwidth if response payloads are huge.""Enable L4 Direct Server Return (DSR) via IP-in-IP/GRE encapsulation so backends reply directly to clients, bypassing the LB on the egress path."
Connection Scaling"Increase ulimit -n to handle more connections.""Account for 4-tuple ephemeral port exhaustion (∼64k\sim 64\text{k} limit per upstream IP:Port) by pooling upstream HTTP/2 connections and attaching multiple source IPs to the L7 proxy."
Zero-Downtime Deploys"Add a Kubernetes readinessProbe and rolling update strategy.""Eliminate the endpoint-removal vs. SIGTERM race condition using a preStop delay, HTTP 503 readiness transition, HTTP/2 GOAWAY frames, and bounded connection draining."
πŸ“–
Track Page Progress0 / 635 Read
Knowledge Base Completion0%