Module 3 · Serving More People, Faster · Lesson 13 of 35
What is a CDN? Serving files from a city near the user
12 minFree System design course · Beginner to intermediateLesson 3.6By Saket Bhatnagar
Researched and drafted with AI, reviewed and edited by Saket Bhatnagar.
In short
- What is a CDN? It is a network of cache servers in many cities that keep copies of your files close to users.
- The first user in a city waits while that city’s server fetches the file once. Everyone after gets the nearby copy.
- Each response says how long it may be kept, and a personal page must never sit in a shared copy.
Video lesson · coming soon
What is a CDN? Bringing the poster to Kochi
Lala, your AI mentor, will walk you through this lesson in a short animated video. Until then, everything is here to read and try.
- 01 Same servers, slower Kochi
- 02 A milk booth in every colony
- 03 Nearby copy, miss only
- 04 Miss, fill, then hit
- 05 New poster, new name
- 06 Recap, then sketch your edge servers
Meera Kulkarni’s support inbox is filling up, and every message comes from Kochi. “Posters and team logos take forever to show,” one says.
Arjun Nair checks everything. The four app servers in Mumbai are calm, Redis is answering, and the database is quiet. On Meera’s phone in Pune, the match 42 poster appears at once.
Then Anil, the fan in Kochi, sends a screen recording. His poster paints in slowly, line by line. Same servers, same code, same poster, but Kochi is slower. Time to test the distance guess.
What is a CDN?
A CDN is a network of cache servers in many cities that keep copies of your files close to users. You cannot move Mumbai closer to Kochi, so you move the poster to Kochi.
Arjun keeps the real poster in one place: Mumbai.
Kochi would get its own cache server, close to Anil.
Edge servers near users, one origin in Mumbai
Drag to explorePhones ask the edge server in their own city. An edge server calls the origin in Mumbai only on a miss, so most requests never travel far.
Diagram connections:
- Phone in Kochi to Kochi edge server (Nearby copy)
- Phone in Ludhiana to Ludhiana edge server (Nearby copy)
- Kochi edge server to Mumbai origin (Miss only)
- Ludhiana edge server to Mumbai origin (Miss only)
- Phone in Kochi: Anil asks for the poster.
- Phone in Ludhiana: Manjit asks for the same poster.
- Kochi edge server: Keeps copies of posters in Kochi.
- Ludhiana edge server: Keeps copies of posters in Ludhiana.
- Mumbai origin: The real copy of every poster and logo.
The one idea to keep: move the copy near the person, not the person to the copy.
- The origin holds the real file, in one place.
- Edge servers in many cities hold copies, close to users.
- A rule on each file says how long a copy stays fresh.
- A festival poster paints in slowly on your phone while a friend elsewhere sees it at once. Theirs came from a copy nearby.
- You refresh and still see a shop’s old logo hours after it changed. The copy near you had not run out.
- Pictures in a shopping app load at once, but your cart takes a moment. Pictures are shared; your cart comes from the real servers.
Why does distance matter? Before the first byte of the poster arrives, the phone and server trade messages a few times, the round trips from lesson 2. Each one crosses the whole way, Kochi to Mumbai and back.
What is an edge server, and how does a request reach one?
An edge server is the CDN cache server near the user, and the phone finds it through DNS. The first request there is a miss, filled from your origin.
SeatJaldi’s origin is not an app server. Arjun moves posters, logos and app files, the code the app downloads, into object storage.
How does a CDN know how long to keep a file?
The origin tells every cache with a Cache-Control header. For example, max-age=86400 means fresh for 86,400 seconds, 24 hours. It is the “use by” label.
| Rule | What it tells caches | SeatJaldi example |
|---|---|---|
| max-age=86400 | Fresh this many seconds, then ask the origin | Posters |
| public | Any cache may keep it, even where it normally could not | Team logos |
| private | Only the user’s own browser may keep it | My bookings |
| no-store | Keep no copy at all | Payment status |
| no-cache | Keep it, but ask the origin before reuse | Match schedule |
What just happened? Each rule is a different label. With no label, a cache may guess its own use-by date, so Arjun labels every file.
What do engineers put on a CDN, and what never?
Shared files go on a CDN with versioned names and long lives. Personal or fast-changing data stays off it.
What distance costs
On a new connection the first byte needs about three round trips: open the connection, set up TLS, send the request. DNS is answered nearby either way.
Assume a round trip of about 40 ms to Mumbai and 5 ms to a Kochi edge server. Mumbai: 3 x 40 = 120 ms. Kochi: 3 x 5 = 15 ms. That is 8 times less on a hit, 105 ms saved. A miss adds the edge server’s own trip to Mumbai.
Against the 0.8-second guide for time to first byte from lesson 2, 105 ms of travel is a real slice.
How edge servers fill and refresh
- Pull: the edge server fetches on the first request, as in Kochi. Push: you upload files in advance.
- Cache key: the key a copy is stored under, usually its URL. So v3 and v4 are two different copies.
- s-maxage: like max-age, but only for shared caches such as a CDN, where it wins over max-age.
- ETag and 304: an ETag tags one version of a file. The edge server sends a stale copy’s ETag to ask if it changed. If not, the origin replies 304 Not Modified with no file.
- Versioned names: put a version in the name and set a long max-age, such as a year (365 x 86,400 = 31,536,000 seconds). A purge, asking edge servers to drop a file, is the backup, and it does not clear browsers.
The origin is object storage, the plant’s cold store. Some object storage promises that a read right after an upload returns the new file.
That promise stops at the origin. An edge server’s copy stays old until max-age runs out, so change the name instead of overwriting.
Revathi never puts anyone’s private order on the open shelf. Payment status gets no-store. Cache the live seat map only if a few seconds of stale data is fine.
What does it save? Assume 1 lakh daily users each load one 500 KB poster. Without a CDN, 1,00,000 x 500 KB = 5,00,00,000 KB, about 50 GB a day, leaves Mumbai.
Assume 20 edge servers, each fetching it about once a day: 20 x 500 KB = 10 MB. That is 50,000 / 10 = 5,000 times less, for about ₹4,000 a month.
Try it yourself
Open one image’s headers
In your browser’s developer tools, open the Network tab, reload and click one image. Or run curl -I with the image’s address.
Find the label
Find the Cache-Control line, and the Age line if there is one.
Convert to hours
Divide max-age by 3,600. If Age is shown, subtract it from max-age for the seconds left.
Check your answer
max-age=86400 with Age: 3600 is 24 hours fresh, 82,800 seconds left. You are right if you list every rule found and say public, private or neither. No Cache-Control line? Say so.
So yes, it was partly distance. Every Kochi visit paid long round trips to Mumbai before the poster began. Mumbai cannot move, so Arjun brings a copy to Kochi, like milk at Revathi’s booth.
The booth keeps the poster as long as its label says, and a new poster gets a new name. The module 3 picture is complete: four app servers, a load balancer, Redis and a CDN. It serves about 1 lakh daily users on a ₹40,000 monthly cloud bill. That covers servers, the database machine, the load balancer pair, Redis, the CDN and storage.
Then a new worry lands. Reads are fast everywhere. Now the bookings table is huge, and a crash mid-booking left money taken but no ticket. Can the database be trusted?
Check yourself
Check yourself
The match 42 poster changed, but Anil still sees the old one, cached with max-age=86400. Best fix?
Pick an answer to see why it is right or wrong.
Check yourself
Anil is in Kochi and the servers are in Mumbai. Which change fixes the cause of the slow poster?
Pick an answer to see why it is right or wrong.
Check yourself
Which response is most dangerous to keep in a shared copy on an edge server?
Pick an answer to see why it is right or wrong.
Remember these three
- Move the copy near the person, not the person to the copy.
- The first request in a city fills the edge server from the origin. Cache-Control sets how long the copy stays.
- Put shared files on a CDN. Keep personal pages and payment status off shared copies.
Questions people ask
- What is a CDN?
- A CDN is a network of cache servers in many cities that keep copies of your files. Users get pictures and app files from a server near them instead of your faraway origin.
- What is an edge server?
- An edge server is one of a CDN’s cache servers, placed in a city close to users. It answers from its stored copy and asks the origin only on a miss.
- What does the Cache-Control header do?
- Cache-Control tells browsers and CDNs whether they may keep a response and for how long. For example, max-age=86400 means fresh for 24 hours.
- Does a small app need a CDN?
- A small app often gains from one for pictures and app files, because it is cheap and takes load off the origin. Personal pages should still stay off shared copies.