7 Caching Patterns Every Backend Engineer Should Know (and Where They Break in Production)
Caching is one of the fastest ways to cut latency and take pressure off a database, yet it is also one of the easiest places to introduce subtle production bugs. Choosing how data enters, updates, and leaves your cache matters just as much as choosing Redis or an in-memory store.
In this article, we will walk through the 7 foundational caching patterns, when to reach for each one, and the real-world failure modes that tend to bite teams once traffic scales.
1. Cache-Aside (Lazy Loading)
How it works: The application checks the cache first. On a cache miss, it queries the database, writes the result into the cache, and returns it to the caller.
Read: App -> Cache (miss) -> DB -> App writes to Cache -> Return
Write: App -> DB -> App deletes (or updates) Cache key
- Best for: General-purpose, read-heavy workloads. This is the default pattern in most applications—Spring’s
@Cacheable, for example, follows cache-aside. - Why teams like it: Only requested data gets cached, and if the cache goes down, the application can still fall back to the database.
Production Gotcha: The Stale-Write Race Condition
Cache-aside has a classic race condition between concurrent readers and writers:
- Request A misses the cache and reads the old row from the database.
- Request B updates that row in the database and deletes the cache key.
- Request A finishes and writes the old value back into the cache.
That stale value now sits in the cache until the TTL expires. Mitigations include short TTLs, versioned keys, or conditional cache writes when strict consistency matters.
2. Read-Through
How it works: The application only talks to the cache. On a miss, the cache itself is responsible for loading the missing entry from the database, storing it, and returning it.
- Best for: Centralizing data-loading logic in one place so callers do not repeat cache-check-and-fill boilerplate. Libraries like Java’s Caffeine (
LoadingCache) implement this pattern natively. - Trade-off: Your caching layer is now tightly coupled to your data-fetching logic, and the first request for a cold key still pays the database latency penalty.
3. Write-Through
How it works: Every write goes to both the cache and the database in the same call before returning success to the caller.
- Best for: Data that is frequently read immediately after being written, where you want the cache to stay warm and consistent with the database.
- Trade-off: Higher write latency (two writes per mutation) and cache pollution if you write data that is never read again.
4. Write-Behind (Write-Back)
How it works: Writes go to the cache first and return immediately. The cache flushes those changes to the database asynchronously—often in batches.
- Best for: Write-heavy workloads where throughput matters more than strict durability (counters, telemetry, analytics buffers).
- Trade-off: Write-behind is tempting for raw throughput until a node crashes. If the cache loses uncommitted writes before they are flushed, that data is gone—and explaining missing records to product stakeholders is never fun.
Production Gotcha: Ghost Data in Write-Through and Write-Behind
Both write-through and write-behind require a reliable guarantee that the database write actually succeeded. If the cache is updated while the database write fails (or rolls back), you end up with ghost data—values served from the cache that do not exist in your source of truth.
5. Write-Around
How it works: Writes go directly to the database, bypassing the cache completely. The cache is only populated later on a read miss.
- Best for: Data that is written frequently but read rarely (logs, audit trails, bulk imports). It keeps your cache from filling up with one-time writes that evict genuinely hot keys.
- Trade-off: A read immediately after a write will always be a cache miss (or stale if an older value was already cached and not invalidated).
6. Refresh-Ahead
How it works: Before a hot key’s TTL expires, the cache proactively reloads it in the background. Readers continue receiving the slightly older cached value while the refresh runs, so no user request blocks on the database.
- Best for: A small set of hot keys that are expensive or slow to compute. Caffeine’s
refreshAfterWriteis a well-known implementation of this pattern. - Trade-off: Predicting which keys are worth refreshing ahead of time can be tricky—refreshing keys that nobody reads wastes database and CPU resources.
7. Request Coalescing (Singleflight)
How it works: When 100 concurrent requests miss the same cache key at the exact same moment, only one request goes to the database. The other 99 wait for that single in-flight fetch to finish and share its result.
- Best for: Preventing a cache stampede (thundering herd) when a popular key expires during a traffic spike.
Production Gotcha: Coalescing Is Usually Per-Instance
In-process request coalescing (such as Go’s singleflight or an in-memory promise map in Node.js) only deduplicates requests inside a single process. If you run 30 pods behind a load balancer and a hot key expires, all 30 pods can still miss simultaneously and send 30 concurrent queries to the database.
For ultra-hot keys in a multi-instance deployment, pair in-process coalescing with one of:
- A short distributed lock in Redis so only one pod rebuilds the key.
- Refresh-ahead or stale-while-revalidate (for example, serving a slightly stale page immediately while revalidating in the background in Next.js or at the CDN/edge layer).
A Modern Twist: Caching LLM and AI API Calls
Caching patterns are not just for SQL databases anymore. In AI-powered products, calling paid LLM APIs is both slow and expensive—making caching and deduplication a direct cost-control lever.
Two practical lessons stand out for LLM caching:
- Use request coalescing for concurrent identical prompts. When multiple users trigger the same prompt at once, coalescing prevents you from paying for the same completion multiple times.
- Design the cache key carefully. Always normalize the prompt (whitespace, formatting, parameter ordering) and include the model version (and relevant generation settings like temperature or system prompt version) in the key. Without the model version in the key, upgrading to a newer model will silently keep serving stale answers from the old one.
Quick Reference: Which Pattern Should You Choose?
| Pattern | Primary Strength | Main Risk / Trade-off |
|---|---|---|
| Cache-aside | Simple, resilient, great default for reads | Stale-write race condition; stampedes on expiry |
| Read-through | Centralizes loading logic | Tightly coupled to cache library; cold-read latency |
| Write-through | Cache stays warm after writes | Extra write latency; ghost data if DB write fails |
| Write-behind | High write throughput via batching | Data loss on crash; ghost data on flush failure |
| Write-around | Avoids cache pollution on heavy writes | Cache miss right after write |
| Refresh-ahead | Zero-wait reads for hot, slow keys | Wasted refreshes if access patterns change |
| Request coalescing | Stops cache stampedes | Per-instance by default; needs Redis lock or SWR at scale |
Summary
Most systems start with cache-aside, and for good reason—it is simple and handles the majority of read-heavy workloads well. The moment your traffic spikes or your data gets expensive to compute, however, you will likely layer request coalescing, stale-while-revalidate, or refresh-ahead on top of it.
Whichever pattern you pick, design for the failure modes up front: know how it behaves during concurrent writes, key expiration under load, multi-pod deployments, and node crashes.