codequest / system design
Core concept 3 of 21

Content delivery networks (CDN)

Serve static files from servers close to the user so your own servers only handle dynamic work.

What a CDN does

A content delivery network is a set of servers spread around the world (edge locations) that keep copies of your files. A user in Mumbai downloads an image from an edge server in Mumbai instead of from your server in Virginia. Two wins at once: lower latency for the user, and far less traffic for your own servers.

It works best for content that doesn't change per user: images, video segments, JavaScript, CSS, fonts. For most consumer apps that is the majority of requests and almost all of the bandwidth.

How it works

  • Pull CDN: the first request at an edge is a miss; the edge fetches from your origin and caches it. Easiest to set up.
  • Push CDN: you upload content to the CDN ahead of time. Good for large files you know will be requested.
  • Cache headers (Cache-Control) say how long edges may keep a file. Use versioned file names (app.3f9c.js) so you can cache for a year and still ship updates instantly.

Try it

A media-heavy app where 60% of requests are images and scripts. The app servers are spending their time sending files. Turn on the CDN.

App servers overloaded
p95 latency (server)
281 ms
Error rate
52.0%
Est. cost / month
$410
excl. bandwidth
App servers
400 req/s each · $40/mo
6
Hit rate
DB read replicas
+1,250 reads/s each · also applies every write · $120/mo
0

Primary DB: 1,250 queries/s (1 query per request). How the numbers work

The simulator's costs exclude bandwidth. In reality, data transfer is often the biggest bill for media apps, and a CDN usually transfers data more cheaply than your cloud's own egress.

In the interview

  • Any design with images or video should put them in object storage (like S3) behind a CDN, not on the app servers.
  • Mention cache headers and versioned file names for invalidation.
  • For video, add that files are split into small segments at several qualities (adaptive streaming), each cached at the edge.