Core Concepts
Caching
Reuse a saved answer to serve repeated reads faster, then decide when the answer must be refreshed.
A cache keeps a faster-to-read copy of a value so repeated requests can reuse it. The original store still owns the data and can supply a new copy when the cache lacks one. Decide how recent a copy must be and how much traffic the original store can take when the cache is empty or unavailable.
Let repeated redirects skip the database
The short code a7Kp2Qx maps to example.com/spring. A request arrives at the redirect API, which looks for that code in the cache. Finding a usable entry is a cache hit. The API can tell the browser where to go with a 302 redirect.
If the entry is missing or expired, the lookup is a cache miss. The API reads the database, puts a copy of the destination in the cache, and sends the same redirect. This pattern is called cache-aside. The application chooses when to check the cache and loads from the database when it needs to.
The database may still receive plenty of traffic. At 2,000 redirects per second with a 95% hit rate, 100 requests per second miss. In this example each miss does one database lookup. The same traffic with an empty cache could send 2,000 lookups per second to the database. Measure the hit rate and the load on the database so you know what the service depends on.
Retire a cached value
Give the Spring entry a time to live (TTL) of 60 seconds. Once that time has elapsed, the API treats the entry as a miss and can fetch a new copy. The TTL limits how long a particular copy can be reused. The value might already be old when a slow reader stores it, so a 60-second TTL alone cannot guarantee the destination is less than 60 seconds out of date.
In a variant where owners can edit a destination, suppose the owner changes Spring to Summer. Save Summer in the database first. Then remove the cached Spring entry so the next miss can load Summer. Removing a copy because the source changed is invalidation. If the process stops after committing the change but before deleting the cache entry, the old copy can keep serving. When invalidation must eventually succeed, record the work to do in the same transaction as the change and let a worker retry it.
A slow read can fill an old value
Reader A misses the cache and gets Spring, version 7, from the database. A pauses before storing it. Writer B commits Summer, version 8, and deletes the cache key. When A resumes, it can put Spring back with a fresh TTL.
One way to guard the fill is for the cache to issue a one-use permission, called a fill token, on the miss. Invalidation revokes any outstanding token for that key, even when there is no cached value to delete. The cache checks that the token is still valid and stores the value in one operation. A's old token now fails the check. The old fill is rejected after invalidation reaches the cache. New loads still need to read a sufficiently recent version from the source of truth. A stale replica or an old transaction snapshot can refill a stale value even with a new token.
A read that needs the new value
The owner might expect the preview to show Summer as soon as the edit succeeds. This is a read-your-writes requirement. Let the preview read from the authoritative database, or have the edit return version 8 and let the preview accept only a cached version at least that new. If the cache cannot supply it, the preview must obtain it from the database or report that it cannot show a current result. The general redirect path can still use the shorter, cheaper cache lookup when brief staleness is acceptable.
Cache leases and stale fills, section 3.2.1
When many requests miss together
A popular code expires while ten requests for it are in flight. All ten see the miss before the first database lookup returns. Each starts a lookup. This burst of duplicate work is a cache stampede.
With request coalescing, one request loads the destination while the others wait for that result. The first request fills the cache and shares the result with the waiters. In one API process, that turns ten overlapping reads for this key into one database lookup.
A process can group the requests it sees. If four processes each see a miss, each can still start a load. Coordinate across processes only if these remaining reads are enough to overload the database. Set a maximum number of waiters per key or process, give each waiter a deadline, and cap concurrent fallback lookups. If the waiter limit is full, reject extra requests or serve an allowed stale value.
Caching challenges and strategies
Remembering a missing code
Suppose someone requests q8Nw3Ld and the database confirms that no mapping exists. If the same code is requested again and again, save a short-lived negative cache entry that means "confirmed absent." Later requests for that code can return 404 without repeating the lookup.
If someone creates the code a second later, an old negative entry can hide it until the entry expires or the create path replaces or invalidates it. A delayed negative fill can bring the old answer back, just like the Spring example. Keep the lifetime short and give any read that needs to see the new link a path to the authoritative mapping.
A negative entry helps only when the missing key repeats. A stream of distinct missing codes still produces first-time lookups and can crowd out useful entries. The URL Shortener walkthrough screens possible misses with a Bloom filter that tracks the set of codes that exist. It bypasses that filter when it cannot rely on the set being complete.
When a lookup fails
A timeout talking to the cache means we could not get its answer. If the database is healthy, fall back within a read budget the database can sustain. That can mean returning a temporary error for some redirects during a cold start or cache outage. Preserve enough capacity for creating links and for the reads the product considers most important.
If the database is unreachable, a cached destination can still work when its age and the product's rules allow it. A revoked link, for example, may require a fresh check before the API can redirect. When we lack both a usable cache result and a way to establish the mapping, the result is unknown. The URL Shortener returns 503 when an exact lookup is required and cannot finish. It returns 404 only when it can establish that the code is absent.
See also
- Redis - how one cache product expires and evicts keys and supports atomic updates.
- Consistency Models - what a session is promised after it saves an edit or sees a newer version.
- Latency vs Throughput - why the extra database work on misses can show up in slow redirects.
- Availability - how a dependency failure changes what the service can still do.
Used in
- URL Shortener - the redirect path checks Redis, reads PostgreSQL on a miss, and fills the cache for later redirects. The invalid-code deep dive explains which requests a complete Bloom filter keeps away from that path.
Done reading? Mark it so it sticks in your dashboard.