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.
- 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.
- 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.
- 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.
- 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.
- Cache: an in-memory store such as Redis holding hot data. A hit returns in well under a millisecond and spares the database.
- Database: the source of truth. A primary takes writes, replicas serve reads; partition (shard) it when one primary cannot keep up.
- 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.
- Workers: consume the queue at their own pace, retry failures, and scale independently of the web tier.
The hops
| Hop | Job | Scales by | Fails how |
|---|---|---|---|
| DNS | Name → IP; can return the nearest region (geo DNS) | Provider-managed | Long TTLs slow failover |
| CDN | Cache static and cacheable content near users | Provider edge network | Stale content; origin overload on mass cache miss |
| Load balancer | Spread traffic across healthy servers; TLS termination | Managed LB, or several behind DNS | Itself a single point if not redundant |
| API gateway | Auth, rate limiting, routing, request transformation | Horizontally | Adds latency; misconfiguration blocks everything |
| App servers | Business logic | Add servers (if stateless) | Crashes absorbed by the LB health checks |
| Cache | Hot data in memory | Cluster and shard | Cold start sends load to the database |
| Database | Source of truth | Replicas for reads, sharding for writes | Primary failure needs failover |
| Queue + workers | Async and spiky work | Add partitions and workers | Backlog grows; needs retries and dead-letter queues |
Layer 4 vs layer 7 load balancing
| Layer 4 (transport) | Layer 7 (application) | |
|---|---|---|
| Sees | IPs and ports | HTTP paths, headers, cookies |
| Can | Forward TCP/UDP connections fast | Route /api/* vs /static/*, terminate TLS, retry, add headers |
| Use for | Very high throughput, non-HTTP protocols, long-lived connections | Most 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
- DNS returns the nearest CDN edge.
- The edge serves images, CSS and JS from cache.
- The HTML/API call misses the CDN and reaches the regional load balancer, which terminates TLS and forwards to an app server.
- The app server reads the product from Redis (hit, ~1 ms) and the price from the database (indexed read, ~3 ms).
- "Recently viewed" is recorded by enqueueing an event; a worker writes it later.
- 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.