VISUAL GUIDE · ONE-PAGE VERSION
Where a cache helps (and where it lies)
A short link goes viral and thousands of browsers ask for the same redirect every second. Every request reads the same row from the alias store, which starts to slow down. A cache will fix the load, but the first time someone changes or disables a link, the cache will keep serving the old answer. Follow a redirect through each case and decide how wrong the cache is allowed to be, and for how long.
By the end you can
- Trace a cache hit and a cache miss on a read path, including who fills the cache.
- Explain why a cached answer can be stale and what bounds how long it stays wrong.
- Order the steps of a change correctly: write the source of truth, then purge.
- Explain why a takedown needs a check that runs on every request, not just a purge.
- Pick a TTL by asking how long a wrong answer is acceptable, not how fast the cache is.
Assumptions behind the numbers
- Illustrative: a cache read takes about 1 ms and an alias store read about 8 ms inside one data centre. Real numbers vary; measure your own.
- Illustrative: a viral link peaks around 4,000 redirects per second, nearly all for one alias.
- Our URL shortener worked approach treats mappings as permanent and only disables them. This guide lets owners edit a link's target so a stale cache entry is easy to see.
- The cache TTL is 60 seconds. That number is a product decision about acceptable staleness, not a performance setting.
Where the time goesIllustrative · see assumptions
- Store read8 ms
Total ≈ 8 ms
STEP 1 OF 9 · NO CACHE
Every redirect reads the store
A browser asks for /abc123. The redirect service reads the alias row from the store and answers 302 with the long URL in the Location header.
That is fine at 400 requests per second spread over many links. At 4,000 per second for one link, every request hits the same row on the same store node, and that node becomes the bottleneck.
Why it matters. Caches are worth adding when reads repeat. A skewed workload, where a few keys get most of the traffic, is the clearest case.
STEP 2 OF 9 · ADD A CACHE
Ask a cache first
Put an in-memory alias cache next to the redirect service. The service checks the cache before the store. This pattern, where the application reads the cache and fills it itself, is usually called cache-aside.
Each entry carries a TTL of 60 seconds. After that the entry expires and the next request goes back to the store.
Why it matters. The cache is a copy. Deciding where the copy lives and how long it may be trusted comes before choosing any cache product.
Where the time goesIllustrative · see assumptions
- Cache read1 ms
- Store read avoided8 ms
Total ≈ 9 ms
STEP 3 OF 9 · CACHE HIT
A hit skips the store
The cache has /abc123, so the service answers straight away. The store is not touched.
With a 60-second TTL, the store sees about one read per minute per cache node for this alias instead of 4,000 per second. The redirect also gets a little faster, but the load drop is the real win.
Why it matters. A hit protects the system behind the cache. Measure a cache by how much load it removes from the store, not only by milliseconds saved.
Where the time goesIllustrative · see assumptions
- Cache read (miss)1 ms
- Store read8 ms
- Cache write1 ms
Total ≈ 10 ms
STEP 4 OF 9 · CACHE MISS
A miss reads the store and fills the cache
For a link nobody has opened in the last minute, the cache has nothing. The service reads the row from the store, writes it into the cache with a fresh TTL, and answers.
A miss is slightly slower than having no cache at all, because you paid for a cache lookup first. That is fine when most reads are hits. When a popular key expires or the cache restarts empty, many requests can miss together; letting only one request refill the key avoids that stampede.
Why it matters. Misses decide how much load reaches the store after a restart or an expiry. Plan for the cold cache, not only the warm one.
STEP 5 OF 9 · LINK CHANGES
The owner changes the target
The link owner repoints /abc123 from an old sale page to a new one. The edit goes to the alias store, which now holds the new target. (Our URL shortener worked approach never changes a target, only disables it. Editing is allowed here because it makes staleness easy to see; a disable has the same problem.)
Nothing has told the cache. It still holds the old target with up to 60 seconds left on its TTL.
Why it matters. A cache and its source of truth are two copies. Any write to one creates a window where they disagree.
STEP 6 OF 9 · STALE HIT
The cache serves the old answer
The next browser asks for /abc123. The cache has an unexpired entry, so the service returns the old target. From the cache's point of view this is a perfectly good hit.
A stale hit is not a bug in the cache. It is the trade you made for skipping the store: you agreed to be wrong for a while in exchange for not asking.
Why it matters. Staleness is the price of every cache. Naming how stale is acceptable is part of the design, not an afterthought.
STEP 7 OF 9 · TTL BOUNDS IT
The TTL caps how long it stays wrong
Even if nothing else happens, the stale entry expires within 60 seconds and the next miss loads the new target. The TTL is the worst-case staleness for this cache.
Every cache on the path needs its own bound, including the browser. A 302 is not cached by browsers unless you add caching headers; a 301 can be cached with no expiry, and you cannot purge a stranger's browser.
Why it matters. A TTL turns "might be wrong" into "wrong for at most N seconds", which is something product and legal teams can agree to.
STEP 8 OF 9 · PURGE
Purge after the write
To shrink the window, the edit flow also sends a purge (an explicit delete) for /abc123 to the cache. The next request misses and reads the new target.
Order matters. Write the store first, then purge. If you purge first, a request can arrive in between, miss, read the old row and put the old target straight back.
Even in the right order there is a rare race: a request that read the old row just before the write can finish its cache fill just after the purge. And purges are messages, so they can get lost. Keep the TTL as the safety net for both.
Why it matters. Invalidation makes changes fast in the normal case; the TTL keeps the worst case bounded when invalidation fails.
STEP 9 OF 9 · DENY-LIST
A takedown cannot wait for a cache
Now a moderator finds that /abc123 points to a phishing page. Even 60 seconds of redirects is too long, and a lost purge would leave it live.
So the moderator writes the alias to a small disabled list, and the redirect service checks that list on every request, including cache hits. A disabled alias gets a warning page no matter what the alias cache holds.
The list stays tiny because takedowns are rare, so checking it costs almost nothing. Copying it to every service instance still takes a few seconds, but that delay does not depend on TTLs or on a purge arriving. The purge still goes out to clean up the cache, but safety no longer depends on it.
Why it matters. When one kind of wrong answer is unacceptable, enforce it on the path every request takes instead of hoping every copy gets updated.
RECAP
What to remember
- A cache hit protects the store; the speed-up is a bonus.
- On a miss the service reads the store and fills the cache, so a cold or expired hot key can send a burst of misses to the store.
- Any change to the source of truth leaves cached copies stale until they expire or are purged.
- The TTL is the worst-case staleness. Write first, then purge, and keep the TTL as the safety net.
- For answers that must never be served (a takedown), check a small list on every request rather than trusting every cache to be purged.
NOW CLOSE THE GUIDE AND REDRAW IT
From memory, draw the read path for a short-link redirect with a cache. Then explain, on the same page, what happens when a cached redirect needs to change: once for an ordinary edit and once for an urgent takedown.
Check your drawing against this list afterwards, not before:
- Browser, redirect service, cache and alias store, with the cache checked first.
- The miss path: read the store, fill the cache, answer.
- A TTL written next to the cache and named as the worst-case staleness.
- The edit order: write the store, then purge the cache.
- A disabled list checked on every request, including cache hits.
- A note on why the redirect is a 302, not a 301.
APPLY IT
Sketch a URL shortener
GO DEEPER WITH OUR GUIDES
OFF-SITE SOURCES · NOT WRITTEN BY US
Sources and further reading
- RFC 9111: HTTP Caching
The rules for how HTTP caches decide freshness, expiry and reuse. Read the sections on freshness and on which status codes are cacheable by default.
- HTTP caching
A readable guide to browser and shared caches, Cache-Control and why fresh vs stale matters.
- Caching challenges and strategies
Lessons from running caches in production: cold starts, stale data, negative caching and what happens to the store when the cache disappears.
- Purge cache
A concrete example of explicit invalidation at an edge: purging by URL, tag or everything, and the limits that come with it.