Lesson 19 / الدرس 19

CDNs: copies closer to the reader / شبكات توصيل المحتوى: نسخ أقرب إلى القارئ

Distance costs time, and no amount of tuning beats the speed of light. The answer the whole industry uses is not a faster server — it is a nearer one.

المسافة تكلّف وقتًا، ولا يتغلب أي ضبط على سرعة الضوء. والجواب الذي تستخدمه الصناعة كلها ليس خادمًا أسرع — بل خادمًا أقرب.

Your server is in one place. A reader in Cairo and a reader in São Paulo cannot both be near it, and every request they make crosses that distance twice — once going, once coming back. Even on a perfect connection that is a fixed cost, paid on every single request.

A content delivery network is a fleet of machines in many cities, each holding a copy of your files. A reader is served from the nearest one. The idea is entirely boring — it is caching from lesson 16, moved out of the reader's browser and into a machine in their city — and it is the single largest speed improvement most sites can make.

How the request finds the near copy

Without a CDN:
  reader in Cairo  ->  ~200 ms  ->  your server in Paris

With a CDN:
  reader in Cairo  ->  ~10 ms   ->  CDN machine in Cairo   (a hit — nothing further happens)
                                        |
                                        v  only on a miss
                                   your server in Paris
The mechanism is the DNS from lesson 4. The name resolves to a different address depending on where the question came from, so the reader connects to the near machine without knowing a CDN exists. Nothing in the URL changes.
TermMeansWhat you want
OriginYour actual serverTo be asked as rarely as possible
EdgeA CDN machine near the readerTo answer nearly everything
HitThe edge had a copyMost requests
MissThe edge had to ask the originOnly the first, per city per file
PurgeTelling every edge to forget a fileAfter deploying a change

The last row is the one that catches people. A CDN is another cache, so lesson 16's problem now exists in fifty cities at once.

What else arrives with it

Because every request passes through the edge before reaching you, a CDN is also where several other things are done: absorbing a flood of traffic that would otherwise reach your one machine, terminating the TLS from lesson 12 near the reader so the handshake is quick, and compressing or resizing images on the way past. Speed is the reason people adopt one; these are the reasons they keep it.

Check yourself / اختبر نفسك

1. What does a CDN actually change about a request?

2. How does the reader end up talking to the near machine?

3. Which page must a CDN never cache?