System Design / A beginner's guide
CDN, step by step.
Learn how a Content Delivery Network serves files from locations closer to users.
1. What is a CDN?
A Content Delivery Network is a distributed network of servers that stores and serves copies of content closer to users. Content can include images, videos, JavaScript, CSS, and downloadable files.
Instead of every user requesting a file from one origin server, a CDN can serve the file from an edge location near the user.
2. Why do we need one?
If users around the world all request files from one server, requests travel farther and the server has to handle every response. A CDN reduces the distance for many requests and takes repeated delivery work away from the origin.
This can improve load time, reduce origin bandwidth, and help a website handle traffic spikes.
3. How a request is served
User → CDN edge server → Origin server- A user requests an image or page through the website domain.
- DNS and the CDN direct the request to a nearby edge location.
- The edge checks whether it already has a usable copy.
- If it has the content, it responds directly. Otherwise, it fetches the content from the origin, stores a cache copy, and returns it to the user.
4. Cache hit and cache miss
Cache hit
A cache hit happens when the edge server already has a fresh copy. The response can be returned quickly without contacting the origin.
Cache miss
A cache miss happens when the content is not stored at that edge, or its cached copy has expired. The edge asks the origin for the content, may store the response, and then sends it to the user.
5. Origin server and invalidation
The origin is the source of truth, such as an object storage bucket or an application server. The CDN does not replace the origin; it keeps temporary or configured copies of content.
When content changes, the CDN may continue serving an older cached copy until its time-to-live expires. A publisher can also invalidate or purge a file so the next request fetches the new version.
6. Benefits and trade-offs
- Faster delivery: users often connect to a nearby edge location.
- Lower origin load: repeated content can be served from cache.
- Traffic absorption: the CDN can handle many requests for cacheable files.
- Cache design: stale content, invalidation, and cache-control rules must be planned.
- Dynamic requests: personalized or rapidly changing data may need to go directly to the application.