DNS and CDNs
Read first: Networking for request paths
A resolver caches an authoritative answer for a TTL, and a CNAME chain adds lookups. Anycast follows routing, not the nearest city. A CDN cache key decides whether media is shared, a purge catches up later, and an origin shield keeps a miss storm off the database.
Most requests should never reach your database. A resolver answers the name from a cache, or an edge answers the bytes from a cache, and the origin sees what those two missed. Networking for request paths priced the round trip after you have an address. This page is how you get the address, and how you avoid moving the bytes across that round trip at all.
Core concepts
A resolver asks. An authoritative server answers. The user’s machine almost never walks the name tree itself. It asks a recursive resolver, usually one the network handed it. That resolver, on a miss, asks the server that is authoritative for the zone: the slice of the name tree those servers are allowed to speak for. The answer comes back with a lifetime. Until that lifetime ends, the resolver may reuse it and not bother the authoritative server again. The hierarchy exists so no one server holds every name. The cache exists so the hierarchy is not on the user path.
TTL is that lifetime, in seconds. A low TTL fails over faster and increases lookup load. If you cut the TTL from 300 seconds to 30, resolvers that honor it ask ten times as often, because 300 / 30 = 10. Failover is “up to one TTL,” not “the moment you edit the record.” A resolver that still holds the old answer keeps handing it out. A connection that is already open does not look at DNS again until it reconnects. Changing the record does not move an existing TCP or QUIC session. It changes the next lookup.
A CNAME is an alias to another name. That name can itself be a CNAME. Each step is another lookup when the resolver’s cache is cold. Chains add lookups. The last name in the chain is the one with an address. Operators like the chain because the first name can stay stable while the later names point at a new provider. Cold clients pay a round trip per uncached step. Warm clients pay nothing until the TTL.
Anycast advertises the same prefix from many sites. Routing delivers the packet to one of those announcements. “Nearby” means what the routing system currently prefers, not the city that is closest on a map. A short fiber path can lose to a higher preference. It is not a promise that the nearest city wins. When the announcement is wrong, the traffic does what you asked, at the scale of the prefix, which is why a bad advertisement is not a single-server bug.
Two published Cloudflare incidents are the shape, not a reason to recite a timeline in an interview. On 17 July 2020 a backbone change removed a prefix-list condition instead of disabling the term. Routers leaked routes into the backbone with a higher local preference, 200 against 100 for local server routes, so traffic from many cities was attracted to Atlanta and overwhelmed it. About 27 minutes. Traffic across the network dropped by about half. Not every city, because not every site depended on that backbone. On 21 June 2022 a change to prefix advertisement policy withdrew a critical subset of prefixes. It started at 06:27 UTC. Nineteen data centers, about 4 percent of locations and about half of requests, were affected. The last revert finished at 07:42 UTC, about 75 minutes later. Engineers had trouble reaching the broken locations because the control plane rode the data plane they had just broken. Cloudflare outages has the third incident and the shared lesson: a global network change without a blast radius.
A CDN caches at the edge. The cache key is usually the URL plus selected headers. Two requests that produce the same key share one object. Two requests that produce different keys never hit. A purge asks edges to forget a key, and the forget is eventual. Some edges drop the object quickly, some later, some after they would have expired it anyway. You design as if a purged object can still be served for a while. If that is unacceptable, the name has to change, or the response should not have been cached.
Immutable media is a good cache citizen. Personalized HTML is a poor one. Bytes of an image or a video, named by their content, are the same for every viewer. Cache them for a long TTL and do not depend on purge: the next version is a new name, and the old name can live until it expires. HTML that says hello to one person is a different document per person. Cache it on the URL alone and you will show one user’s page to another. Vary the key on the cookie and you store one entry per user, which does not reduce origin load. Leaving that HTML uncached is the honest choice. Caching is the same rule inside your own process: the key is the contract, and the TTL only bounds how wrong you are when invalidation fails.
An origin shield is an extra cache tier in front of the origin. Edges miss. Without a shield, every edge that misses fetches the origin, and a popular new object becomes one request per edge against the same database. The shield collapses those fills. The first miss populates the shield; the other edges read the shield. The database sees one fill, not one per edge. It is the stampede lock, moved out to HTTP.
The production split worth copying is How Netflix streams. Control and product services are one system. The bytes of video are another: appliances close to the user store encoded media and serve it over HTTP. They do not store viewing history, DRM state, or member data. A control plane steers the player to a nearby appliance that has the file and is healthy. Filling new titles happens off the peak. The idea to steal is a small control decision plus a large cache of immutable bytes. It is the architecture they wrote about, not a claim about this year’s org chart.
A worked example
An image host, so the lookup math and the byte math can sit next to each other. Assumptions, all of them labeled:
- 100 million daily users, the teaching scale, each fetching 10 images a day. A sketch, not a company.
- Mean image size 200 KB, decimal, so 200,000 bytes.
- The name the client looks up has a TTL you choose. We will compare 60 seconds and 5 seconds.
- 20,000 recursive resolvers honor the TTL and collapse their clients. The 20,000 is an assumption about the resolver population, not a census.
- 200 edges. Also an assumption.
- A brand-new image is cold on every edge at once.
- Personalized HTML is a separate response, 20 KB, fetched once per user per day. Assumption.
image fetches per day 100,000,000 * 10 = 1,000,000,000
average fetch QPS 1,000,000,000 / 86,400 = 11,574
shortcut 1,000,000,000 / 100,000 = 10,000
origin bytes if every view misses
1,000,000,000 * 200,000 = 2 * 10^14 bytes
= 200 TB/day
= 2.31 GB/s = about 18.5 Gbps
naive authoritative load
each client asks every 60 s 86,400 / 60 = 1,440 lookups/day
times 100,000,000 clients 1.44 * 10^11 lookups/day
per second 1,666,667
with 20,000 resolvers at 60 s 20,000 / 60 = 333 QPS
reduction 100,000,000 / 20,000 = 5,000 times
same resolvers at a 5 s TTL 20,000 / 5 = 4,000 QPS
60 / 5 = 12 times the lookup load
CNAME chain, 3 cold steps
at a 150 ms ocean RTT 3 * 150 = 450 ms before an address
one cold image, 200 edges
no shield 200 origin fills
shield collapses the object 1 origin fill
1,000 cold images, no shield 200,000 origin requests in the burst
HTML, uncached on purpose
100,000,000 * 20 KB 2 TB/day, about 1,157 requests/s
The 1.67 million queries per second is what the authoritative servers see if every client asks them directly every minute. Nobody builds DNS that way. The resolver cache is the product. At the assumed 20,000 resolvers and a 60 second TTL, the authoritative tier sees a few hundred queries per second. That is why a small set of name servers can front a hundred million clients, and why the assumption that matters is “resolvers cache for the whole TTL,” not the user count.
Cut the TTL to 5 seconds because you want a failover in 5 seconds instead of 60. Lookup load goes up 12 times, to about 4,000 per second in this sketch. That can be the right trade. It is not free, and it still does not move connections that are already open. Those stay on the old address until they reconnect. Quote the failover as “new lookups within 5 seconds,” not “the fleet moved in 5 seconds.”
A CNAME chain is invisible until a cache is cold. Three steps, each missing, each crossing a California-to-Netherlands round trip of about 150 ms, is 450 ms before the client has an address. That is a worst case you should be able to compute, not the common case. A cached chain costs zero until the TTL. The failure mode is a low TTL on every step of a long chain: cold mobile clients pay the sum, over and over. Each extra alias is a lookup. Add one only when the operational freedom is worth a round trip on the cold path.
The byte side is why the CDN exists. A billion fetches of a 200 KB object is 200 TB a day, about 18.5 Gbps average, if the origin serves all of it. Latency numbers that matter already told you a cross-country round trip is tens of milliseconds before the first byte. Serving every image from one region pays that delay and fills the origin pipe. The edge exists so the second viewer of an object, near that edge, does not.
The cache key decides whether “the second viewer” is real. Keyed only on the URL, a hot image is one entry and the 11,574 requests per second share it. Keyed on the URL plus a session cookie, two users never share an entry. You can store on the order of one variant per user and the origin still sees the traffic. You bought a cache that does not cache. Selected headers belong in the key when they change the bytes in a way many people share. A content encoding is that kind of header. An authorization token is not.
Purge does not make a delete synchronous. You remove an image, you send a purge, and an edge that has not applied it keeps serving the old bytes. If the law or the user cannot tolerate that window, do not cache that URL, or publish the image under a name that includes the content and never reuse the name. Immutable names turn purge from a correctness tool into a disk-reclaim tool. The old name expires because of the TTL. The new name is a different key.
The shield is the difference between 200 and 1 on a single new image, and between 200,000 and 1,000 if a thousand images go cold together. A thousand is an assumption: a deploy that changed every cache key, or a catalog release. Without a shield the origin sees one request per edge per object. With a shield that holds the object, the origin sees one fill per object and the edges read the shield. The database behind the origin sees that smaller number only if the shield actually hits. A shield whose key does not match the edge’s key misses every time and is just another hop.
The HTML line is the contrast. Two terabytes a day looks large until you put it next to 200 TB of images. It is also the traffic you should not try to share. One page per user, with that user’s name in the body, is a leak if the key ignores the user, and a useless cache if the key includes them. Serve it from the origin, or assemble it at the edge from a cached template plus a small private fragment. The private fragment is the part that must not be a shared key. Design video streaming is the same split at a larger byte size: immutable media at the edge, control and personal state somewhere that can be correct. Object storage is the durable copy the origin shield falls back to.
A video byte versus an API call
One play is two requests, and they do not share a cache policy. Assumptions: the segment is 1,000 KB and the URL names the title, the rung, and the segment, with no user id. The “what should this member watch next” API response is 2 KB and depends on the member. The player is 400 km of fiber from an edge and 4,000 km from the only origin. Server think time is left out so the distance and the serialization are visible.
Dean’s row is about 10 microseconds to send 1 KB over a 1 Gbps link. A 1,000 KB segment serializes in 1,000 * 10 microseconds, which is 10 ms. Fiber is 200 km per millisecond.
edge round trip 400 / 200 = 2 ms one way, 4 ms
origin round trip 4,000 / 200 = 20 ms one way, 40 ms
segment, last byte from the edge 4 ms + 10 ms = 14 ms
segment, last byte from the origin 40 ms + 10 ms = 50 ms
API, 2 KB on a 1 Gbps link 20 microseconds
On a hit, the video byte is a 14 ms transfer from a nearby edge, and the origin does not see it. A miss that goes to the far origin is 50 ms before the cache is filled, and the origin did the work. The API’s 2 KB is noise next to a round trip. If that call lives only at the far origin, every play pays about 40 ms before any personalization, plus a same-datacenter database hop of 0.5 ms once the request is there. The database is not the term. The distance is.
Caching the API under the URL alone would hand one member’s list to the next member. Caching it under the cookie would store one entry per member and almost never hit. Leave it uncached, and keep it small. The segment stays a shared key, so the 14 ms path is real and the shield can collapse a miss storm on that one URL. A video byte is a cache hit at the edge. An API call is a round trip you still owe.
flowchart LR
client[Client]
resolver[Recursive resolver]
auth[Authoritative server]
edge[CDN edge]
shield[Origin shield]
origin[Origin]
client --> resolver
resolver --> auth
client --> edge
edge --> shield
shield --> origin
The top path runs when the name is cold. The bottom path runs on every byte. A healthy system spends almost all of its requests on the bottom path and almost all of those on the edge box, not the origin.
Tradeoffs
| Choice | What you gain | What you spend |
|---|---|---|
| TTL of many minutes | Very little authoritative load | Failover waits out that TTL for new lookups |
| TTL of a few seconds | New lookups move quickly | Lookup load scales as one over the TTL |
| A CNAME to the CDN | You can retarget the provider without renaming the product | One more lookup on every cold resolver |
| Cache key is the URL alone | One shared object for every viewer | Wrong the moment the body depends on the user |
| Cache key includes the cookie | No cross-user leak | No sharing, so the origin still sees the traffic |
| Immutable name, long TTL | Correctness does not depend on purge | Old names sit in cache until they expire |
| Origin shield | A miss storm collapses to one fill per object | Another tier that can be stale, and a key you must get right |
Pitfalls
- Treating a DNS edit as a drain. Existing connections keep their old address. Only new lookups see the new one, and only after the TTL they already cached.
- A 30 second TTL on a three-step CNAME chain. Cold clients pay three lookups, and they pay them again every half minute.
- Believing anycast picked the geographically nearest site. It picked the announcement routing preferred. The Atlanta preference leak is the symptom: a higher local preference beat “local.”
- Putting a cookie in the cache key “to be safe.” You get safety and you lose the cache. The origin graph looks like you have no CDN.
- Caching a response that sets a session cookie, on a shared key. The next user receives the first user’s cookie.
- Using purge as the only way to unpublish. Purge is eventual. Someone will fetch the object after you declared it gone.
- An origin shield with a different key than the edge. Every edge miss becomes an origin miss. The extra tier adds latency and no collapse.
- One cache policy for personalized HTML and for media. The HTML leak and the media miss are different bugs, and one setting causes both.
- A global announcement with no blast radius. Withdrawing a prefix, or preferring the wrong one, moves half the traffic because anycast did what it is for.
In the interview
Open with four nouns: resolver, TTL, cache key, and what is immutable. “The client asks a resolver. I set the TTL to a minute so a failover is on the order of a minute and the lookup load stays small. Images are cached on the URL because they are the same for everyone. The HTML with the user’s name is not cached. An origin shield sits in front of the database so a cold image is one fill, not one per edge.” That is a full answer.
Skip BGP attribute encyclopedias and vendor product names. If you mention anycast, say in the same breath that routing, not distance, chooses the site.
The question that usually follows is “we just deleted it and a user still sees it” or “the origin just melted.” Deletion: purge is eventual, so the name should have been immutable or uncached. Melted origin: a miss storm, one request per edge, which a shield or a single-flight fill would have collapsed. The honest tradeoff is staleness. A long TTL and a long-lived edge object are how you got the load off the database, and they are why a change is not instant.
Check yourself
You drop the DNS TTL from 300 seconds to 30 seconds. What happens to lookups at the authoritative servers if resolvers cache for the full TTL?
They rise by a factor of ten, because each resolver must refresh ten times as often. Failover for a new lookup shrinks from up to five minutes to up to 30 seconds. Connections that are already open do not move until they reconnect. You paid lookup load for a faster change of the next lookup, not for an instant drain.
One billion image fetches a day. What is the average rate, and what is wrong with quoting the shortcut as exact?
1,000,000,000 / 86,400 is about 11,574 per second. The shortcut divides by 100,000 and gets 10,000. That 10,000 is 86,400 / 100,000 = 0.864 of the exact rate, about 14 percent low. The exact rate is about 16 percent higher than 10,000, because 100,000 / 86,400 = 1.16. Same gap, two bases. Say which divisor you used.
Two hundred edges miss the same new image at once. How many origin fills do you get with a shield, and without one?
Without a shield, 200 fills, one per edge, and the database behind the origin sees 200 if it does work per fill. With a shield that stores the object under the same key and collapses the burst, the origin sees one fill and the other edges read the shield. That assumes the shield holds the object. A shield that misses is not a shield.
Why is a purge not a delete, and what do you do instead for a photo that must disappear?
A purge asks caches to forget, and they forget on their own schedule. A user can still be served from an edge that has not applied it. If that window is unacceptable, do not cache the response, or publish under a name you will never reuse so you do not need the purge for correctness. The old name can expire by TTL. The new bytes live at the new name.
A user in a nearby city still lands on a far site after you anycast the prefix. Is the mechanism broken?
No. Anycast delivers the packet to whichever site is announcing the prefix and currently winning the route. It is not a promise that the nearest city wins. A higher local preference can pull traffic across the map, which is the failure mode in the July 2020 backbone leak: preference 200 beat preference 100 and pulled load to one city. Check the advertisement before you blame the distance.
Based on: Cloudflare postmortems; Netflix Open Connect; Designing Data-Intensive Applications, Kleppmann and Riccomini.