sagelearnai

The Request Path: DNS, CDN, Load Balancers and Stateless Services

Almost every high-level design is a version of one path: a client resolves a name, hits an edge, is balanced across stateless servers, which read a cache and a database and hand slow work to a queue. Know each hop, what it is for and how it scales, and you can draw most designs in five minutes.

Learning objectives

  • Explain the role of DNS, CDNs, load balancers, API gateways and reverse proxies.
  • Distinguish layer 4 from layer 7 load balancing and choose a balancing algorithm.
  • Explain why stateless services scale horizontally and where state should live.
  • Walk a request through the full path and say what happens when each hop fails.
Clientbrowser, app CDNstatic, edge Load balancerL7, TLS App serversstateless CacheRedis DNSname → IP Workersasync jobs Queuebuffer work Databaseprimary + replicas resolve API, misses on miss
Client / userNetwork / edgeService / computeData storeQueue / stream
The path of a request. Every web-scale design is a variation of this path. Name each hop and what it is responsible for, and you have half of most high-level designs.
  1. DNS: turns the hostname into an IP address, often a geo-aware answer pointing at the nearest region or CDN edge. Low TTLs let you fail over quickly.
  2. CDN: caches static assets and cacheable responses at edge locations near users. It cuts latency and absorbs most read traffic for media. Uncacheable API calls pass through.
  3. Load balancer: spreads requests across healthy app servers, terminates TLS and health-checks backends. Layer 7 balancers can route by path or header; an API gateway adds auth and rate limits.
  4. App servers: stateless, so any server can handle any request and you scale by adding servers. Session state lives in a cache or token, not in server memory.
  5. Cache: an in-memory store such as Redis holding hot data. A hit returns in well under a millisecond and spares the database.
  6. Database: the source of truth. A primary takes writes, replicas serve reads; partition (shard) it when one primary cannot keep up.
  7. Queue: work that does not need to finish inside the request (emails, thumbnails, fan-out) is enqueued so the request returns fast and spikes are absorbed.
  8. Workers: consume the queue at their own pace, retry failures, and scale independently of the web tier.

The hops

HopJobScales byFails how
DNSName → IP; can return the nearest region (geo DNS)Provider-managedLong TTLs slow failover
CDNCache static and cacheable content near usersProvider edge networkStale content; origin overload on mass cache miss
Load balancerSpread traffic across healthy servers; TLS terminationManaged LB, or several behind DNSItself a single point if not redundant
API gatewayAuth, rate limiting, routing, request transformationHorizontallyAdds latency; misconfiguration blocks everything
App serversBusiness logicAdd servers (if stateless)Crashes absorbed by the LB health checks
CacheHot data in memoryCluster and shardCold start sends load to the database
DatabaseSource of truthReplicas for reads, sharding for writesPrimary failure needs failover
Queue + workersAsync and spiky workAdd partitions and workersBacklog grows; needs retries and dead-letter queues

Layer 4 vs layer 7 load balancing

Layer 4 (transport)Layer 7 (application)
SeesIPs and portsHTTP paths, headers, cookies
CanForward TCP/UDP connections fastRoute /api/* vs /static/*, terminate TLS, retry, add headers
Use forVery high throughput, non-HTTP protocols, long-lived connectionsMost web APIs

Algorithms: round robin (simple), least connections (uneven request costs), weighted (mixed server sizes), consistent hashing on a key (sticky routing, for example WebSocket sessions or per-user caches).

Stateless services

A service is stateless if any instance can serve any request because nothing about the user lives in the instance's memory. Put sessions in a token (signed JWT) or a shared cache, files in object storage, and data in the database. Then scaling is "add instances behind the load balancer", and a crashed instance loses nothing.

Stateful exceptions you will meet: WebSocket servers holding connections (Module 5) and caches or databases (which you partition and replicate).

Example: a request for a product page

  1. DNS returns the nearest CDN edge.
  2. The edge serves images, CSS and JS from cache.
  3. The HTML/API call misses the CDN and reaches the regional load balancer, which terminates TLS and forwards to an app server.
  4. The app server reads the product from Redis (hit, ~1 ms) and the price from the database (indexed read, ~3 ms).
  5. "Recently viewed" is recorded by enqueueing an event; a worker writes it later.
  6. Response in ~40 ms server-side.

Self-check

Walk a request from the browser to the database and back.
  • DNS → CDN → load balancer (TLS) → stateless app → cache → database; async work to a queue; response. Mention failure handling at each hop.
When do you choose layer 4 over layer 7 load balancing?
  • Very high throughput, non-HTTP protocols or long-lived TCP connections; otherwise layer 7 for routing, TLS and retries.

Last reviewed 2026-11-05.