9 min readEngineering

URL Redirect Cache: How Our Two-Tier Cache Works Today

How a URL redirect cache works end to end: in-process L1, shared Redis L2, the origin, real TTLs, how edits purge both tiers, and what browsers cache.

Marius Voß
DevRel · edge infra

A URL redirect cache is the layer that answers "where does this slug point?" without asking the database every time. Ours has two tiers in front of the origin: a small in-process LRU that lives 60 seconds, and a shared Redis cache that lives 5 minutes. If both miss, the edge asks the origin and fills both on the way back.

That is the whole design, and the numbers matter more than the shape. This post walks through the tiers, the TTLs we run today, how an edit clears both tiers (and what happens if that fails), and the other redirect cache people usually mean when they search for this: the one inside the browser. The redirect p95 cornerstone covers the full latency budget. This is the cache-specific piece.

I wrote the first version of this post with more confidence than the code deserved. I've rewritten it against the actual edge service, so everything below describes what runs now, including the parts we'd like to improve.

Why a Redirect Needs a Cache at All

A redirect does one lookup: host plus slug in, destination and rules out. Nothing else belongs on the request path. The destination, status, expiry, targeting rules and click cap all live in the link record, and that record lives in Postgres behind the origin API.

Calling the origin per click is the slow, fragile option. Every origin call adds a network hop to the request, and a database hiccup would become a redirect outage. So the edge keeps recent records close and treats the origin as the last resort. For the user, the visible result is a redirect that doesn't depend on the dashboard being healthy.

The Two Tiers, With Real Numbers

Each lookup runs in this order, using the key host/slug:

  1. L1 is in-process: an expiring LRU holding up to 100,000 link records, each for 60 seconds. It uses the expirable LRU from hashicorp/golang-lru. A hit costs no network at all.
  2. L2 is Redis, one cache shared by every edge process, with records stored as JSON under an elido:link: prefix for 5 minutes. An L2 hit also refills L1.
  3. The origin is the link API over gRPC or HTTP. It is the only layer allowed to say "this link does not exist". A hit here fills both caches.
Flow diagram of the two-tier redirect cache: the in-process L1 LRU holds 100,000 entries for 60 seconds, the Redis L2 holds records for 5 minutes, and the origin is the source of truth

Two details are easy to miss. First, a Redis error is treated exactly like a miss, so a Redis outage slows redirects down instead of breaking them. Second, a "not found" answer from the origin is not cached. Typos and scanners that probe random slugs go to the origin every time. That is a known gap, and it's the first thing I'd fix if probe traffic ever mattered.

We also don't coalesce concurrent misses today. If a cold link gets a burst of clicks in the same instant, each one can reach the origin. It hasn't hurt us, but it's a limit you should know about before copying this design.

Why L1 Is Only 60 Seconds

An in-process cache can't be shared, so each edge process holds its own copy. To clear it from outside, every edge process subscribes to a Redis pub/sub channel in the background and drops a key when an invalidation message arrives. The 60-second TTL is the safety net behind that: short enough that a missed message heals quickly, long enough that a link getting steady traffic is served from memory almost every time.

The same reasoning explains the bound on size. 100,000 entries covers a hot working set because redirect traffic is heavily skewed. A small share of slugs gets most of the clicks, and the rest can fall through to L2 without anyone noticing.

If you're sizing your own, start with the TTL. Pick the staleness you can defend to a customer, then work backwards to the hit rate it buys you.

How Edits Reach the Edge

When someone edits a link, Postgres updates first. Right after the write is saved, api-core deletes the link's Redis key and publishes an invalidation message on elido:link:invalidate. Every edge process is listening and drops its in-memory copy. The next click misses both tiers and loads the new record from the origin. Visitors see the change within a second or two.

The purge covers more than the destination: targeting rules, password, expiry, click limit, status, redirect type, a slug change (both the old and the new short URL are cleared, so the old one stops resolving at once), deletes, restores from the trash, bulk status changes, A/B variants, a new scanner verdict, attaching a CTA overlay and rotating encryption. There's no setting; every plan gets it.

One race needed a second pass. A lookup that started just before the write can read the old row and put it back into Redis after the purge. So api-core purges once more, 2 seconds later, which closes that window.

The purge is best effort and never sits on the redirect path. If Redis is briefly unavailable, the TTLs take over as the fallback, and the worst case is the old one: L2 can hold the old record for up to 5 minutes, and an edge process can load it into L1 just before L2 expires and keep it for another 60 seconds, roughly six minutes in total.

Fallback timeline if a purge is missed: the database updates at once, the Redis copy can live up to 5 minutes, and the in-process copy adds up to 60 seconds, about 6 minutes in total That's the fallback, not the normal path. In normal operation the purge makes it a second or two.

Not everything is purged per link. Workspace-level changes, such as pixels, branding, edits to a CTA overlay's content and domain-wide settings, still follow the normal TTLs unless an admin action already cleared them.

For a launch or a takedown, that means you can change the destination at the moment you need and check it with curl -I a few seconds later. Before the purge existed, the advice here was to schedule changes six minutes early. You no longer need to.

Cache Warming and Cold Starts

We don't pre-warm anything. A fresh Redis starts empty and fills as links get clicked. A freshly started edge process starts with an empty L1 and falls through to Redis for the first requests, then fills as links are used.

This is boring on purpose. Because every layer is best-effort and the origin is authoritative, a cold cache costs latency on the first click per link, never correctness. If you've been told you need a pre-warm button, ask what the cold-click latency actually is first. For most workloads, nobody can see it.

The Other Redirect Cache: Your Browser

Most searches for "redirect caching" are about a different cache entirely, and it causes more support tickets than ours does. Browsers cache redirect responses, and the status code decides how hard.

A 301 is "permanent". The HTTP caching spec (RFC 9111) treats it as cacheable by default, and browsers have historically stored it with no expiry when no header says otherwise. Rowan Collins' write-up describes the classic trap: a redirect gets cached, the URL is later reused, and visitors with the old copy bounce forever. A 302 or 307 is only reused when a freshness header allows it. MDN's caching guide is the clearest reference for the headers involved: max-age sets freshness, private restricts storage to the browser, and no-store stops storage altogether.

Our redirect responses send Cache-Control: private, max-age=0, no-store, whichever of 301, 302 or 307 the link uses. Browsers therefore ask the edge again on the next click. That's a deliberate trade. You lose a little speed on repeat visits and gain two things: every click is counted, and a destination edit isn't stuck in someone's browser. The interstitial pages for disabled, expired or missing links are no-store too, so a link that comes back to life isn't masked by a cached error.

If you run your own redirects, choose the status on purpose. Our 301 vs 302 guide and the overview of redirect types cover when each fits.

Clearing a Cached Redirect in Chrome

If a browser is stuck on an old 301, fix the server first, then clear the browser's copy:

  1. Open DevTools and switch to the Network tab.
  2. Tick Preserve log and clear the log.
  3. Load the redirecting URL so the redirect request shows up.
  4. Right-click that request and clear the browser cache for it.

A normal hard refresh often won't do it. For a fast check without touching your normal profile, load the URL in a private window, or run curl -I https://your.short/link to see the real status and headers with no browser cache involved. If you chain several redirects, a stale cached hop is also a classic cause of loops. See how to fix a redirect loop.

What Each Choice Costs

Every number above is a trade you can move.

  • Raise the L1 TTL and you save a few Redis reads, at the price of a longer stale window whenever an invalidation message is missed. We found little reason to.
  • Raise the L2 TTL and the origin sees less traffic, but a missed purge takes longer to heal. Five minutes was our compromise.
  • Skip L2 and each edge node needs more origin capacity, since nothing is shared between processes.
  • Let browsers cache (301 without no-store) and you shed repeat traffic, but you lose accurate click counts and the ability to change your mind.

A two-tier cache with purge on write fits when reads dominate writes, the key distribution is skewed, and a few minutes of staleness is acceptable in the rare case a purge is lost. It fits badly when destinations change often and must change instantly, or when every request is unique.

Watching It From the Outside

You can't see our cache counters, but you can see its effects. A synthetic probe that records redirect latency and status will show a slow first request after a deploy and flat latency after that. The Datadog redirect latency dashboard walks through that setup, and fire-and-forget click ingestion covers what happens to each click after the redirect is served.

If you'd like branded short links on your own domain with this behaviour handled for you, custom domains is where to start, and the pricing page shows which plan includes how many domains.

Frequently asked questions

Do browsers cache 301 redirects?

Yes, and aggressively. A 301 with no cache headers is cacheable, and some browsers keep it with no expiry, so the browser can keep jumping to the old target after you change it server-side. A Cache-Control header with no-store or a short max-age is what stops that.

Are 302 redirects cached?

Not by default. A 302 is temporary, so browsers only reuse it when a Cache-Control or Expires header says it is fresh. That is why a 302 is the safer choice while you are still testing a redirect.

How do I clear a cached redirect in Chrome?

Fix the server first, then open DevTools, go to the Network tab, tick Preserve log, load the redirecting URL, and clear the cache for that request. A normal hard refresh often leaves the cached redirect in place. Testing in a private window is the quick check.

How long should a redirect cache TTL be?

As long as you can tolerate a stale destination if a purge is ever missed. We use 60 seconds in-process and 5 minutes in the shared cache, and every edit purges both, so a change normally shows within a second or two. The TTLs are the fallback: if the purge fails, an edit can take up to about six minutes to show for everyone.

Why does my short link still go to the old URL after I edited it?

On Elido, an edit purges the edge copies within a second or two, so after that the usual cause is your browser or a redirect in front of Elido that stored an earlier 301. Check with curl -I to see the real response, then test in a private window to rule out the browser.

Does Elido let browsers cache its redirects?

No. The redirect response carries Cache-Control private, max-age=0, no-store, even for links set to 301. Every click reaches the edge, which keeps click counts honest and lets a destination change without waiting for browsers.

Try Elido

Paste a URL, get a working short link

No signup. Link lives for 30 days. Sign up to keep it forever.

Free account required. The free plan includes 25 links in total. Already have an account? Log in

Try Elido

EU-hosted URL shortener with custom domains, deep analytics, and an open API. Free tier - no credit card.

Tags
url redirect cache
redirect caching
two tier cache
cache invalidation
redirect ttl
301 redirect cache

Continue reading