Kazi Rahamatullah
Kazi Rahamatullah
AboutProjectsBlogContact
UsesBooks
ResumeView CV
Kazi Rahamatullah

© Copyright 2026 Kazi Rahamatullah

AboutProjectsBlogBooksUses
Twitter/XGitHubProduct HuntCodeSandbox
Back to Blog
System DesignLoad BalancingHigh AvailabilityDevOpsScalability

System Design: Load Balancing

Traffic distribution, session stickiness, health checks, and failover — with an interactive load balancer playground for round robin, least connections, and sticky sessions.

Jul 8, 20263 min read

Introduction

Quote

When you know how to distribute traffic, the system scales.

Load balancing is how a system survives traffic and recovers from failure. Session stickiness, health checks, and failover policies are the reliability backbone — not optional extras at scale.

← Back to System Design overview

Why load balance?

A single app server has a ceiling — CPU, memory, connections. Multiple instances behind a load balancer give you:

  • Horizontal scale — add servers under load
  • High availability — route around unhealthy nodes
  • Zero-downtime deploys — drain connections from old instances

Architecture sketch

plaintext
                    ┌─────────────┐
  Clients ────────► │ Load        │
                    │ Balancer    │
                    └──────┬──────┘
           ┌───────────────┼───────────────┐
           ▼               ▼               ▼
        ┌──────┐       ┌──────┐       ┌──────┐
        │ API-1│       │ API-2│       │ API-3│
        └──────┘       └──────┘       └──────┘

Balancing algorithms

AlgorithmHow it worksBest for
Round robinRotate across healthy serversStateless APIs
Least connectionsSend to server with fewest active connectionsLong-lived connections, WebSockets
IP hash / stickySame client → same serverLegacy sessions in memory
WeightedMore traffic to stronger machinesMixed instance sizes

Warning

Session stickiness simplifies stateful apps but hides uneven load and complicates failover. Prefer external session storage (Redis) for new systems.

Interactive playground

Send requests, switch algorithms, and click a server to simulate a health-check failure.

Load Balancer Playground

Route traffic, test failover, and compare stickiness vs fair distribution.

Click a server card to simulate health-check failure and observe failover behavior.

Health checks

Load balancers probe backends periodically:

  • HTTP GET /health — returns 200 when app + dependencies are ready
  • TCP check — port open only; weaker signal
  • Graceful drain — stop new traffic, finish in-flight requests before shutdown
typescript
// Minimal health endpoint
export async function GET() {
  const dbOk = await pingDatabase()
  if (!dbOk) return Response.json({ status: 'degraded' }, { status: 503 })
  return Response.json({ status: 'ok' })
}

Failover behavior

When a server fails health checks:

  1. Balancer stops sending new requests
  2. In-flight requests complete or timeout
  3. Sticky clients may need re-login if sessions were local

Tip

Run at least two instances in every production tier. A load balancer with one backend is just an expensive proxy.

Interview Answer

Load balancing interview points: clarify stateless vs stateful → pick algorithm → explain health checks → mention L4 vs L7 (TCP vs HTTP routing) → discuss active-passive vs active-active for databases separately from app tier.

Back to index


Share this article

XLinkedInFacebook
Kazi Rahamatullah

Written by

Kazi Rahamatullah

FullStack Developer

X / TwitterGitHubLinkedIn

Subscribe to my newsletter

Stay up to date and get notified when I share new contents.

No spam ever, unsubscribe anytime