DNS and CDNs (System Design #7)
Before a single byte of your application is fetched, the user’s browser has to answer one question: which IP address is this domain? That question is resolved by DNS, and the answer can route them to a server on the other side of the planet — or to the edge node ten milliseconds away. DNS and CDNs are the first two levers of perceived performance, and neither is optional at scale.
This chapter follows the journey of a request: DNS resolution, then content delivery. You will learn how DNS can do geographic routing and failover, how CDNs cache bytes near users, and the difference between pulling content on demand and pushing it ahead of time.
DNS: the internet’s phone book
DNS maps a human name (api.example.com) to an IP address (203.0.113.7). Resolution walks a hierarchy, with caching at every level:
- Browser/OS cache — if recently resolved, done in microseconds.
- Recursive resolver (ISP or public) — asks on your behalf.
- Root servers — point to the TLD (
.com). - TLD servers — point to the authoritative nameserver for
example.com. - Authoritative nameserver — returns the actual record.
Each answer carries a TTL; until it expires, resolvers skip the whole walk. TTL is a trade-off: short TTLs let you change routing quickly (failover, migrations) but increase resolver traffic and lookup latency; long TTLs are efficient but make changes slow to propagate. Choose TTL by how fast you need to move.
\begin{tikzpicture}[font=\rmfamily, >=stealth,
b/.style={draw, thick, rounded corners=3pt, minimum width=1.7cm, minimum height=0.66cm, align=center, font=\rmfamily\small},
a/.style={->, thick}]
\node[b, fill=gray!10] (c) at (-4.4,0) {client};
\node[b, fill=blue!10] (r) at (-2.2,0) {resolver};
\node[b, fill=green!12] (root) at (0.2,1.3) {root};
\node[b, fill=green!12] (tld) at (0.2,0) {.com};
\node[b, fill=orange!16] (auth) at (0.2,-1.3) {authoritative};
\draw[a] (c) -- (r);
\draw[a] (r) -- (root); \draw[a] (r) -- (tld); \draw[a] (r) -- (auth);
\node[font=\rmfamily\small, text=black!60, anchor=north] at (0.2,-1.9) {answers cached with a TTL};
\end{tikzpicture}
Record types you will actually use:
| Type | Maps to | Use |
|---|---|---|
A |
IPv4 address | the server |
AAAA |
IPv6 address | the server, v6 |
CNAME |
another name | aliases (www → app.example.com) |
MX |
mail servers | email routing |
TXT |
arbitrary text | verification, SPF/DKIM |
NS |
nameservers | delegation |
DNS is also a global load balancer. Because it can return different answers per resolver location, it enables geo-routing (send Europeans to the EU region), weighted routing (canary 5% of traffic), and failover (health-check the primary and return a backup if it is down). This is cheap and powerful, but crude: DNS answers are cached, so failover is only as fast as your TTL, and some resolvers ignore low TTLs. For fast failover, combine DNS with an anycast or proxy layer that can move traffic in seconds.
CDNs: move bytes to the edge
A Content Delivery Network is a globally distributed cache for bytes. Instead of every user fetching an image from your origin in one region, they fetch it from an edge node near them. The win is latency (physics: light in fiber takes ~50–150 ms to cross a continent), bandwidth cost, and origin offload.
\[ \text{origin load} = \frac{\text{requests} \times (1 - \text{cache hit rate})}{\text{edge nodes}}, \qquad TTL \uparrow \Rightarrow \text{hit rate} \uparrow \]
A 95% cache hit rate cuts origin traffic 20×. Fast-loading pages also improve conversion, which is why CDNs are one of the highest-ROI infrastructure investments.
Two ways content reaches the edge:
- Pull CDN. The edge fetches from origin on first request, caches, and serves subsequent requests from cache. Lazy, always fresh-ish, and the default for most content. The first request per edge is slow; after that it is fast.
- Push CDN. You upload content to the CDN ahead of time. Predictable and fast from the first request, but you manage uploads and must push updates. Suits large, slowly changing assets (videos, releases).
\begin{tikzpicture}[font=\rmfamily, >=stealth,
b/.style={draw, thick, rounded corners=3pt, minimum width=1.7cm, minimum height=0.68cm, align=center, font=\rmfamily\small},
a/.style={->, thick}]
% pull
\node[font=\small] at (-3.0,1.5) {pull};
\node[b, fill=gray!10] (p1) at (-4.2,0.5) {user};
\node[b, fill=orange!16] (p2) at (-2.6,0.5) {edge};
\node[b, fill=blue!10] (p3) at (-1.0,0.5) {origin};
\draw[a] (p1) -- (p2); \draw[a, dashed] (p2) -- node[above, font=\scriptsize] {miss} (p3);
% push
\node[font=\small] at (2.6,1.5) {push};
\node[b, fill=blue!10] (q1) at (0.8,0.5) {origin};
\node[b, fill=orange!16] (q2) at (2.4,0.5) {edge};
\node[b, fill=gray!10] (q3) at (4.0,0.5) {user};
\draw[a] (q1) -- (q2) node[above, font=\scriptsize] {upload}; \draw[a] (q2) -- (q3);
\end{tikzpicture}
Caching rules at the edge
CDNs are caches, so all the cache questions from Chapter 12 apply, with a CDN-specific twist: you often do not control the storage, only the headers.
Cache-Controlgoverns freshness (max-age,s-maxagefor shared caches,stale-while-revalidate). This is your main dial.ETag/If-None-Matchenable revalidation: the edge asks the origin whether the cached copy is still valid, saving bandwidth when it is.- Immutable assets (hashed filenames like
app.3f9c.js) can be cached forever. This is why build tools add content hashes — it makes aggressive caching safe. - Invalidation is the hard part. You can purge by URL, by tag, or by versioning the URL. Prefer versioned URLs; purge is slow and error-prone at scale.
- Origin shield — a mid-tier cache that collapses many edge misses into one origin request, protecting the origin during a cache stampede.
Dynamic/API responses are harder to cache but not impossible: cache per user or per tenant with short TTLs, or cache read-heavy endpoints whose contents change slowly (product listings, public profiles).
Where DNS and CDN meet the design
In the skeleton from Chapter 1, they are the first two hops:
client → DNS (geo/failover) → CDN (bytes) → load balancer (routing) → app servers
The design decisions:
- Serve all static assets and media through the CDN; never let them hit your app servers.
- Use DNS for coarse geographic routing and disaster failover; use the load balancer (Chapter 8) for fine-grained, fast routing.
- Set TTLs deliberately: short for records you may need to change (failover), long for stable ones.
- Watch the cache hit rate as a first-class metric; a CDN with a 40% hit rate is mostly a slow proxy.
Pitfalls
- Forgetting TTLs during a migration. A long TTL on an
Arecord means your cutover can take hours to reach everyone. Lower it before the move. - Caching dynamic content too aggressively. Serving one user’s data to another via a shared cache is a security incident. Partition caches by user/tenant or set
private. - Unversioned asset URLs. Without content hashes you cannot cache “forever”, so you get either stale assets or low hit rates. Hash the filename.
- Treating DNS failover as instant. Resolvers cache; failover can lag by the TTL. Pair it with a proxy/anycast layer for fast failover.
- Pushing everything or pulling everything. Static, rarely changing media suits push; frequently updated content suits pull. Match the mode.
Why is DNS useful for geographic routing but weak for fast failover? What is the difference between a pull and a push CDN?
ASolution
DNS can return location-specific or health-based answers, but resolvers cache answers for the TTL, so a change propagates slowly. Pull CDNs fetch on first request and cache (lazy, default); push CDNs receive uploaded content ahead of time (predictable, suited to large slowly changing assets).
Next: Load Balancing (System Design #8) — spreading requests across a pool without dropping the ones in flight.