Skip to content

Cloud

CDN Cache with TTL

A cache lookup is O(1) — you just compare the current time to a stored expiry. The real win: on a HIT, you skip a slow trip to the origin server entirely.

The idea, in plain English

A CDN (content delivery network) is like a corner shop that keeps popular snacks nearby, instead of making every customer travel to a warehouse across town. The warehouse is the 'origin server' — the one true copy of your content. The corner shop is a 'cache' — a copy kept close to the user so it loads fast. But snacks go stale. So every copy on the shelf gets a TTL (time to live): a timer that says how long it can sit there before the shop must fetch a fresh one from the warehouse.

How it works

  1. 1The first time something is requested, there's nothing on the shelf yet. That's a cache MISS. Fetch it from the origin server and put a copy on the shelf, marked with an expiry time (now plus the TTL).
  2. 2Later requests, made before the expiry time, check the shelf and find the copy there. That's a cache HIT, and it's served instantly without bothering the origin server.
  3. 3Once the expiry time passes, the copy is stale. The next request is a MISS again. Fetch a fresh copy from the origin and reset the expiry.

When you'd use it

Use this to serve content that doesn't change every second — images, videos, articles, even API responses — to users spread across the world. Caching it near them, and refreshing it now and then with a TTL, makes pages load faster and takes load off the origin server.

Common beginner mistakes

  • Setting the TTL too long. Users then see stale content long after it's changed, like an old price on a product page.
  • Setting the TTL too short. You barely benefit from caching, and the origin server gets hit almost every time anyway.
  • Using the real current time in examples or tests instead of a fixed, passed-in time. That makes the behavior unpredictable and hard to check.

Try it — edit and run

Click the code to edit · press ⌘/Ctrl+↵ to run

Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.

Expected output — hit Run to try it
t=0: MISS (fetched from origin, cached until t=10)
t=5: HIT (served from cache, expires at t=10)
t=12: MISS (fetched from origin, cached until t=22)
t=20: HIT (served from cache, expires at t=22)
t=25: MISS (fetched from origin, cached until t=35)

Not sure this is the right topic? See the learning paths → or where this leads →