Back to blog

Why are your photos slow on the other side of the world?

Thắng Đoàn
Thắng Đoàn

FotoFlow has 5,000 users worldwide, but the servers live in Virginia. A user in London waits about 75 milliseconds per round trip, Sydney about 200, and a page needs many round trips, so Sydney waits well over a second. You cannot speed up light; the only way to cut distance latency is to serve users from somewhere closer.

Most bytes are the same for everyone

Look inside a FotoFlow page load: the HTML is personal, but the photos, scripts, and stylesheets are identical for every user on the planet. Static assets are often more than 90 percent of the bytes on a page. Identical, rarely-changing files are perfect for caching anywhere, including far from the origin.

Copies at the edge

A CDN runs edge servers in hundreds of cities. Point your static files at it, and each user is served by the nearest edge: London now gets photos in about 10 milliseconds, and only misses travel all the way to Virginia. You rent the edges from Cloudflare, Fastly, Akamai, or CloudFront; you do not run them.

Edges are pull-through caches: they start empty, the first request for a file misses and fills from the origin, and everyone after that is served locally. It is the same cache-aside idea from the last post, spread around the planet.

The new logo nobody sees

You replace logo.png on the origin, but edges cached the old one with a one-day TTL, so users see yesterday's logo for hours. Purging fixes it in seconds, but someone must remember, every time, and no purge reaches copies already saved in browsers. Long TTLs are what make a CDN fast, and what makes updates slow to appear.

The fix inverts the problem: put a fingerprint of the file contents in its name, logo.3f9a.png. A new logo gets a new name and can never be confused with the old one, so files can be cached for a year while the HTML, uncached, simply points at the latest names. Build tools like Vite and webpack add these content hashes for you.

The edge absorbs spikes

A launch goes viral and traffic jumps 20 times. Almost all of it is answered by edges spread around the world and the origin barely notices. A CDN is a cache, a global network, and a shield for your origin, all at once. The same spread-out capacity also absorbs floods of malicious traffic in DDoS attacks.

Two traps

First trap: a config change tells the CDN to cache every FotoFlow page for 5 minutes, Bob opens his cart, the edge stores it, and Alice a second later gets served Bob's cart. Pages that depend on who is asking must be marked Cache-Control: private, so edges always pass them through. Caching personal data publicly is a classic, very public incident.

Second trap: a new file goes viral and every one of hundreds of edges misses at the same moment, hammering your origin with identical requests. An origin shield, a mid-tier cache in front of the origin, means only the shield ever asks, so each file is fetched once. Most CDNs offer it as a setting.

What this does not solve

The CDN makes reads fast everywhere, but every hit still ends at a data decision: where the bytes and rows actually live, how they are shaped, and what FotoFlow trades away for each choice. The next chapter starts with the biggest one: SQL or NoSQL.

Which of your current assets could be cached for a year tomorrow with a content hash, and which genuinely cannot?
If your traffic multiplied by 20 next Monday, would your origin survive, or would the edges be the only reason it does?

Speed of light: 1. The workaround: copies.

Next in the series: SQL or NoSQL: which one fits a photo app?

Sources

Cloudflare: What is a CDN?

MDN Web Docs: HTTP caching

undefined

System Design Primer

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.