Notes
All notes

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:

  1. Browser/OS cache — if recently resolved, done in microseconds.
  2. Recursive resolver (ISP or public) — asks on your behalf.
  3. Root servers — point to the TLD (.com).
  4. TLD servers — point to the authoritative nameserver for example.com.
  5. 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:

\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.

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:

Pitfalls

QQuestionSelf-check

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.