CDNs, edge caching and anycast
Bring the content close
Section titled “Bring the content close”The earlier lessons made one thing clear: a round trip costs time in proportion to the distance, and the speed of light sets a floor that no server can lower. A server in one data centre is far from most of its users. A content delivery network, a CDN, puts servers in many places, so that there is one near each user. These are edge servers, and the server that holds the original content is the origin.
An edge server is a cache. When a client asks for an object, the edge looks in its own store. If it has it, it answers at once: a hit. If it does not, it asks the origin, usually keeps a copy and answers: a miss. The next client who asks for the same object, near the same edge, gets a hit.
Mental model. A CDN does two things. It brings the content close, when the edge has it, and it brings the connection close, always. The first depends on the hit rate. The second does not.
What a hit and a miss cost
Section titled “What a hit and a miss cost”The lab below has three variants for one request. With the defaults, the round trip to the edge is 20 ms, from the edge to the origin 120 ms, and from the client to the origin directly, with no CDN, 150 ms. The origin takes 50 ms to produce the object. Step through each variant and compare the times.
A CDN edge: hit or miss
The edge has the object in its cache and answers itself. The client pays one round trip to a server that is close to it.
- Between the client and the edge10 msClient to CDN edge: GET /logo.png. Arrives at 10 ms.GET /logo.png
- 20 msCDN edge to Client: 200 OK, from cache. Arrives at 20 ms.200 OK, from cache
Time until the client has the response: 20 ms1 round trip on the wire
Model notes
Assumptions
- Each message takes half the round-trip time of the link it crosses. Messages are never lost, never queue and never overlap, and each leaves when the one before it has arrived, once its receiver has finished any processing.
Simplifications
- Connections between the client and the edge, and between the edge and the origin, are assumed to be open already. A new connection to the edge costs the handshakes of the first lesson, over the short link.
- An edge is assumed to answer at once from its cache. An entry that has gone stale is revalidated with the origin, which costs a trip to the origin, like a miss.
- The hit rate is a single number that you set. A real hit rate depends on how often each object is requested and how long it may be cached.
Parameter values are illustrative. They are not benchmarks of any real product.
An edge hit costs one round trip to a server that is close: 20 ms. An edge miss costs that round trip, plus the edge’s trip to the origin and the origin’s time: 20 + 120 + 50 = 190 ms. With no CDN the client goes to the origin itself: 150 + 50 = 200 ms, every time. A miss is a little cheaper than going direct here only because the edge’s path to the origin is shorter than the client’s, which is a property of the network and not a guarantee.
The row at the bottom is the average at the hit rate you set. At the default of 90%, it is 0.9 × 20 + 0.1 × 190 = 37 ms. Move Share of requests the edge has in its cache to see the whole range: at 50% the average is 105 ms, at 99% it is 21.7 ms, and at 0% it is 190 ms, which is not better than having no CDN at all. The hit rate decides everything.
What the origin sees
Section titled “What the origin sees”A hit rate also says how much of the traffic reaches the origin. If 90% of the requests are hits, only 10% get through: 10,000 requests per second at the edges become 1,000 per second at the origin, ten times fewer. At 99%, a hundred times fewer. A CDN is therefore a way to protect and shrink the origin as well as to speed up users, and the origin can be sized for the misses and not for the traffic.
What may be cached
Section titled “What may be cached”An edge cache is a shared cache, in the terms of RFC 9111: it stores responses for reuse by more than one user. The origin tells it what it may do with the response header Cache-Control.
| Directive | What it says to a shared cache |
|---|---|
max-age=N |
The response is fresh for N seconds from when it was generated |
s-maxage=N |
Like max-age, for shared caches only, and it overrides max-age |
private |
A shared cache must not store the response, only a user’s own browser may |
no-store |
No cache may store any part of the request or the response |
no-cache |
A cache may store it, but must revalidate it with the origin before each reuse |
The Age header tells the receiver how long the response has been in caches along the path. A response that has passed its freshness lifetime is stale, and a cache can revalidate it with a conditional request carrying the validator it was given, an ETag or a Last-Modified date. If the object has not changed, the origin answers 304 Not Modified without the body. Revalidation saves the body, but it still costs the trip to the origin, like a miss.
So what is safe to put at the edge? A logo that is the same for everyone, a script with a version in its name, a public page that changes once an hour. What is not: a page that shows the signed-in user’s name or basket, which must be marked private or no-store, or it could be served to another user.
What happens if the edge cache is emptied?
Section titled “What happens if the edge cache is emptied?”A deployment purges the CDN’s cache, or a new edge starts, or a popular object expires everywhere at once. For a while the hit rate falls toward zero. Every request is now a miss, and the origin receives all of the traffic, where it received a tenth: 10,000 requests per second instead of 1,000. If the origin was sized for the misses, it is now ten times overloaded, exactly when users are being served slowly, by 190 ms and not 20.
This is the cache stampede of the caching module, one level up. The remedies are the same in spirit: expire objects at different times, let the edge fetch an object once and answer the others from that one fetch, and keep an extra layer of caching between the edges and the origin.
Beyond caching: the short connection
Section titled “Beyond caching: the short connection”A CDN speeds up even a miss, because the connection to the edge is short. A client that opens a new connection to the origin 150 ms away, over TCP with TLS 1.3, spends two round trips on handshakes: 300 ms before the request can leave. To an edge 20 ms away, the same handshakes cost 40 ms. A CDN can keep connections open between its edges and the origin, so that no handshake is paid on the long path. This is the shorter-distance remedy of the lesson on TLS, and it works for content that cannot be cached at all.
What happens if a miss costs more than going direct?
Section titled “What happens if a miss costs more than going direct?”With the edge’s path to the origin at 160 ms, longer than the client’s own 150 ms, a miss costs 20 + 160 + 50 = 230 ms against 200 ms direct. The CDN is worse than no CDN on a miss. It breaks even at the hit rate where the average equals 200 ms: the hit share h satisfies h × 20 + (1 − h) × 230 = 200, so h = 30 ÷ 210, or about 14.3%. Below that hit rate, the CDN slows users down. A CDN pays for objects that many users ask for, and does little for objects that each user asks for once.
Anycast: how a client finds a nearby edge
Section titled “Anycast: how a client finds a nearby edge”Something has to send each client to a nearby edge, and one way is anycast. In ordinary routing, called unicast, an address belongs to one place. With anycast the same address is made available in several separate places, and RFC 4786 defines it as the practice of making a service address available in multiple, discrete locations, such that datagrams sent to it are routed to one of the available locations. Each site announces the address to the network, and routers send each packet along the best route they know, which leads to one of the sites, normally the one closest by routing, which is not always the closest by distance. The client does nothing special.
RFC 4786 lists what operators use it for: proximity, since a packet is delivered to a site near the sender; coarse load distribution; containing attacks, since traffic sent from one part of the network arrives at the site that serves that part; and a short list of servers, which suits DNS. The root servers show the scale: root-servers.org lists 13 root server names run by 12 organisations, served as of 5 October 2026 from 2,045 instances.
Proximity is worth a floor. Light in an optical fibre travels about 200 km per millisecond, as the lesson on constraints showed, so a round trip over d kilometres takes at least d ÷ 100 milliseconds. A client 8,000 km from a single site cannot have a round trip shorter than 80 ms, and with a site 1,000 km away the floor is 10 ms. Real round trips are longer, but nothing else closes the gap between the floors: no faster server, only a nearer one. The handshakes of the earlier lessons are paid at that shorter distance: a connection that costs three round trips costs 30 ms at a floor of 10 ms, against 240 ms at 80 ms.
Anycast is one of three ways to send a client to a nearby place, and they act at different levels. DNS steering gives different answers to different resolvers, and usually sees the resolver’s address, not the client’s. Anycast gives everyone the same address, and the network’s routing chooses, packet by packet. A load balancer chooses among servers behind one site, where anycast chooses among sites. They combine: a CDN can use anycast to reach the nearest edge and a load balancer to pick a server inside it.
What happens if the routing changes in the middle of a connection?
Section titled “What happens if the routing changes in the middle of a connection?”Each packet is routed on its own. If routing changes while a TCP connection is open, the client’s packets can start arriving at a different site that announces the same address. That site has never heard of the connection and cannot continue it: the connection breaks, and the client has to start another. RFC 4786 says that routing must be stable for substantially longer than the expected transaction time, and warns that for long-running flows there are failure modes more complex than a plain unreachable destination.
That is why anycast suits short exchanges, such as a DNS query, which is a single datagram in each direction, and why using it for long connections needs routing that is stable. It also shows how a site is taken out of service: it stops announcing the address, routing moves its traffic to the next nearest site, and until the routing has settled, some packets may be lost.
Check what you have learnt
Section titled “Check what you have learnt”Anycast: check your understanding
This quiz needs JavaScript to check your answers.
- A CDN puts cache servers, the edges, near users. The edge answers from its cache, a hit, or asks the origin and keeps a copy, a miss.
- With 20 ms to the edge, 120 ms from the edge to the origin and 50 ms of origin work, a hit takes 20 ms, a miss 190 ms, and no CDN 200 ms.
- The average is the hit rate’s weighted mix: 37 ms at 90%, 105 ms at 50%, 21.7 ms at 99%, and no better than 190 ms at 0%.
- The hit rate also sets the origin’s load: at 90%, ten times fewer requests reach it.
Cache-Controltells a shared cache what it may do:s-maxageandmax-ageset freshness,privateandno-storekeep responses out of shared caches, and a stale entry is revalidated with the origin.- An emptied edge sends all the traffic to the origin. A CDN also shortens the handshakes, whatever the hit rate: 40 ms to an edge 20 ms away, against 300 ms to an origin 150 ms away.
- If a miss costs more than going direct, the CDN only pays above a break-even hit rate, 14.3% in the example.
- Anycast makes one address available in several places, and routing delivers each packet to one of them. The least round trip over d kilometres is d ÷ 100 ms: 80 ms at 8,000 km, 10 ms at 1,000 km. If routing changes mid-connection the packets can reach a site that does not know the connection, so anycast suits short exchanges.
Next: the module recap and quiz, which covers all six lessons.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.