System Design · Concept Library · Free article
What Is Caching, and When Does Cache-Aside Help?
Use a temporary copy to remove repeated reads from a busy source, then manage misses, staleness, and failure deliberately.
Caching stores a reusable copy closer to the read path so repeated requests can avoid slower or more constrained work behind it. Cache-aside is a common application pattern: check the cache, read the source on a miss, then store the result with an expiry. It helps when reads repeat and the source is the measured bottleneck. The hit ratio tells you how much traffic the cache actually removes. TTL and invalidation bound how stale a copy may become. Because the cache is another system, you also need a plan for cold starts, popular-key expiry, and cache outages.
One efficient lookup can still run too often
Hatch resolves x7Gk2 through an indexed PostgreSQL query. The index keeps one lookup from scanning unrelated rows, but it does not make the database capacity unlimited. At about 2,000 redirects each second, repeated reads compete for the same connections, CPU, memory, and storage resources. Requests queue even though the query plan is still sensible.
Caching addresses a different problem from indexing. An index reduces work inside one database query. A cache can prevent a repeated query from reaching the database at all. That distinction tells you when to use each mechanism and why a larger database instance may only postpone the same aggregate-load limit.
How does cache-aside work?
With cache-aside, the application owns the sequence. It looks for url:x7Gk2 in Redis. A hit returns the cached destination. A miss reads PostgreSQL, returns that result, and stores a copy for a later request. This admits only data somebody requested, so the cache tends to fill with the active working set rather than the whole table.
A miss is not a one-time event for the lifetime of a key. It can happen after expiry, eviction, invalidation, restart, or failover. Treat the miss path as normal production traffic and make sure the source can survive it.
The interactive version of this example is available when JavaScript loads.
Step a hit and a miss side by side and the asymmetry is the thing to carry: a hit is one lookup, while a miss is a lookup, the original work, and a write, so it costs slightly more than having no cache at all. That is why the hit ratio, not the presence of a cache, decides whether the cache helped, and why a cache that misses most of the time is a tax rather than an optimisation.
What hit ratio does a cache need to be worth it?
High enough that the miss traffic is a load the source can carry, which for a hot read path usually means the high nineties rather than a round number. The cache hit ratio is hits divided by cache lookups. At 2,000 reads each second, a 90% hit ratio still sends about 200 reads each second to PostgreSQL. A 99% hit ratio sends about 20. That change cuts source traffic by 10 times even if the average user-visible latency moves only a little.
A low hit ratio has several possible causes: the cache is cold, it cannot hold the working set, entries expire before reuse, eviction removes useful entries, or the workload simply has little repetition. Diagnose those separately. A faster cache node cannot fix a workload that rarely hits.
The interactive version of this example is available when JavaScript loads.
Drag the ratio and the relationship is not the intuitive one. Load on the source falls with the MISS rate, so the interesting range sits at the top: going from 90% to 95% removes half the remaining source reads, while going from 50% to 55% barely moves them. Each extra point is worth more than the last, which is why the last few points of hit ratio usually justify effort that the first fifty did not.
How do you invalidate a cache?
A cached value is a copy. If Maya changes a link in PostgreSQL while Redis still holds the previous destination, a cache hit can return the old URL. A TTL limits how long an entry may remain before it expires, but expiry alone does not make a copy immediately fresh after a write.
For Hatch, the write updates PostgreSQL and then deletes the cached key. A later read misses and refills from the source. Keep a TTL as a backstop for missed invalidations and choose it from the product tolerance for staleness. Shorter TTLs generally create more misses; longer TTLs retain copies longer. There is no useful universal TTL.
Cache-aside, write-through or write-behind: which should you use?
Cache-aside is a useful default for read-heavy data because the application can fall back to the source when the cache is unavailable. Other strategies change who owns the source write and when it completes. Write-through acknowledges after the cache and source are updated. Write-behind acknowledges before the source is durable, which can improve write throughput but introduces a loss window. Write-around sends writes to the source and lets later reads populate the cache. Pick from the required freshness and durability, not from the pattern name.
The interactive version of this example is available when JavaScript loads.
Compare the strategies and none of them wins outright, which is the point of laying them next to each other. They differ in what they make expensive: cache-aside keeps writes cheap and admits a stale window, write-through keeps the cache fresh and pays for it on every write, and write-behind makes both fast while accepting a window where acknowledged data exists only in memory. Pick from the failure you can live with rather than from the latency number.
Eviction should protect the working set
A cache is smaller than the source by design. When it reaches its memory limit, the eviction policy decides which entries leave. Least recently used and least frequently used policies are different guesses about which data will be requested again. Redis policies are configurable and use approximations rather than a perfect global ordering.
Size the cache from the active working set, entry size, metadata overhead, and headroom. Then verify it with memory use, eviction rate, and hit ratio. A full cache is not automatically unhealthy, but high evictions combined with a falling hit ratio usually means useful data does not fit.
What is a cache stampede, and how do you stop one?
When a popular key expires, many concurrent requests can miss together and all rebuild the same value. Request coalescing lets one request load the source while the others wait for its result. TTL jitter reduces synchronized expiry across many keys. Early refresh can renew a hot entry before it disappears.
A cache outage has a similar load shape. If the database normally receives only the misses, sending every read there at once can overload it. Replication and failover reduce cache downtime, but the application still needs bounded fallback, load shedding, or stale-if-error behavior suited to the data. Availability depends on the whole miss path, not the cache node alone.
The interactive version of this example is available when JavaScript loads.
| Signal | Question it answers | Useful follow-up |
|---|---|---|
| Hit ratio | How many lookups avoid the source? | Segment by operation or key family before changing capacity. |
| Miss traffic | Can the source absorb the current and worst-case fallback? | Test cold start and cache-unavailable behavior. |
| Cache latency | Are hits still fast at the tail? | Inspect hot keys, large values, blocking commands, and network delay. |
| Evictions and memory | Does the active working set fit? | Compare eviction rate with hit-ratio movement. |
| Stale or invalidation defects | Does the copy violate the product freshness promise? | Trace writes, deletes, versions, and expiry by key family. |
Primary references
- Microsoft Azure Architecture Center: cache-aside pattern
- AWS: database caching strategies using Redis
- Redis: cache-aside use case
- Redis: key eviction
A cache is useful only when it removes the right work
By GOAT Engineer. Back to Concept Library. Privacy Policy · Contact.