How a CDN serves files from a server near you
A CDN (content delivery network) is a group of servers in many places, each keeping copies of your files. The idea to keep: most requests are answered by a copy close to the visitor, and the original server is asked only when the copy is missing.
The short version
Distance adds delay, so a copy that sits near the visitor is faster than a trip to the original server, called the origin. If the nearby server holds a fresh copy, it answers at once. If it does not, it asks the origin, keeps the reply and uses it for the visitors who come next.
Step by step
- The browser asks for a file.
- The request is routed to a nearby CDN server; how that works depends on the CDN.
- A fresh copy there is a hit, and it is sent back without asking the origin.
- No copy, or a stale one, is a miss: the server asks the origin.
- The reply is stored and passed on, and later requests reuse it.
- A changed file shows up when the copy's
Cache-Controltime ends or the owner purges it.
| Hit | Miss | |
|---|---|---|
| Fresh copy nearby | yes | no |
| Origin asked | no | yes |
Why it matters
Less distance and less work for the origin make pages load faster. The costs are a third party your site depends on, extra DNS lookups and connections, and a copy that can be out of date until it expires or is purged. Caching basics are in How a cache makes things faster.
Sources
- CDN — MDN Glossary — servers in many places keep copies, the closest one answers, distance adds delay, the costs of using a CDN
- RFC 9111: HTTP Caching — a fresh stored response is reused without asking the origin (4.2), a miss goes to the origin (4.1), a stale response is checked first (4.3)
- HTTP caching — MDN — CDNs are managed caches controlled with
Cache-Control, and some allow purging