Database caching strategies: cache-aside, write-through, write-behind and TTLs

Vinnie on keeping popular answers in memory: cache-aside, write-through and write-behind, TTLs and invalidation, and the thundering herd, when a hot key expires and every request hits the database at once. Redis, Valkey and Memcached as the usual stores, and the managed caches on AWS, Azure and Google Cloud.

GazeekDATABASE SCALING PATTERNS WITH VINNIE·EPISODE 5
EPISODE 5

Database caching strategies: cache-aside, write-through, write-behind and TTLs

Vinnie on keeping popular answers in memory: cache-aside, write-through and write-behind, TTLs and invalidation, and the thundering herd, when a hot key expires and every request hits the database at once. Redis, Valkey and Memcached as the usual stores, and the managed caches on AWS, Azure and Google Cloud.

0:00 / 5:35

Diagrams in this video · 1Free in archhy

Copy and edit in archhy: Cache-aside (new tab)

The diagrams, full size

Copy any of them into archhy, free, and change it for your own system.

After this lesson you can

  • Explain cache-aside, write-through and write-behind
  • Use TTLs and invalidation to keep cached data fresh enough
  • Prevent a thundering herd when a hot key expires
  • Name the usual cache stores, Redis, Valkey and Memcached, and each cloud's managed cache
Transcript

Database caching strategies: cache-aside, write-through, write-behind and TTLs

Vinnie here. Rung four. Caching. Some questions get asked thousands of times a minute, with the same answer every time. Why ask the database every time?

Cache-aside

The most common pattern is called cache-aside, or lazy loading. The app talks to the cache and the database itself. One. The app needs product forty-two, so it asks the cache first. If it's there, that's a hit, straight from memory. Two. If it isn't, that's a miss. The app reads the database instead. Three. Then it stores the answer in the cache, so the next request is a hit. Only data someone asked for gets cached. Four. And it sets a TTL, time to live. After that many seconds, the copy expires, so stale data can't live forever. Five. But watch this one. A popular key expires, hundreds of requests miss at the same moment, and every one runs the same query. That's the thundering herd.

Cache-aside, with a TTL

Here it is in code. Ask the cache. On a hit, return it. On a miss, read the database. Store it with an expiry. Five minutes here, plus a little randomness, so keys cached together don't all expire together. And when the data changes, delete the cached copy. That's invalidation. The next read misses, and fetches the fresh value. A read racing the write can still put an old copy back, and the TTL cleans that up.

Write-through or write-behind?

Cache-aside fills the cache on reads. Two other patterns fill it on writes. Write-through updates the cache every time you write the database. The cache stays in step with every write that goes through it, but every write is two writes, and you may cache data nobody reads. Write-behind writes to the cache, and the cache updates the database later, in the background. Writes are very fast. But if the cache fails before it catches up, those writes can be lost. Use it only where you can afford that.

Taming the herd

So how do you tame the herd? First, jitter. Random TTLs, so hot keys don't all expire at the same moment. Second, let one request rebuild the value. A set that only succeeds if the key doesn't exist yet works as a simple lock. One request wins and queries the database. The rest wait briefly, then read the fresh copy. Third, prewarm. Before a new cache node takes traffic, fill it with the keys your app needs. And if one query is getting hammered right now, the usual advice is simple. Cache it for even five seconds. That buys time while you fix it properly.

The usual stores

The usual cache stores are Redis, and Valkey. Valkey is a community fork of Redis, made in twenty twenty-four and run by the Linux Foundation. And Memcached, the simplest of the three. Just keys and values, held in memory. Whichever you pick, the patterns are the same. Only the commands differ. On Memcached, the lock is an add, not a set.

In the cloud

And every big cloud will run one for you. On AWS, Amazon ElastiCache runs Valkey, Redis OSS or Memcached. DynamoDB has its own cache too, DAX, DynamoDB Accelerator. It's write-through, and it takes eventually consistent reads from milliseconds to microseconds. Strongly consistent reads pass straight through to DynamoDB, and aren't cached. On Azure, it's Azure Managed Redis. It's clustered by default, so use a client that understands clusters. The older Azure Cache for Redis is being retired, so don't start there. Its Enterprise tiers retire in March twenty twenty-seven, the rest in September twenty twenty-eight. On Google Cloud, it's Memorystore. There's one for Valkey, one for Redis, and one for Memcached. The Memcached one has no replication, so losing a node loses its keys.

Remember

Let's recap. Cache-aside fills the cache on reads. Write-through and write-behind fill it on writes. With cache-aside, always set a TTL, and delete the cached copy when the data changes. And tame the herd with jitter, a lock, and prewarming. A good cache is like a good nose. It remembers what it's already found.

The full article, and what's next

Next time, rung five. Read replicas. Copies of the database that take the reads, and why they can lag a little behind. See you there.

Read the article

AWS: Caching best practices: the whole idea in writing, free.

Read it ↗