Most applications reach for Redis when a page is slow, and most of the benefit arrives in the first afternoon. The trouble comes later, and it is never that the cache was too slow. It is that something in it stopped being true and nobody noticed, because a cache does not fail loudly — it serves yesterday's answer with total confidence.
What it is
Redis holds data in memory rather than on disk. A read is typically well under a millisecond, where a database query hitting disk is milliseconds at best and considerably more under load.
It is not only a key-value store. It provides real data structures, which is what separates it from a plain cache:
- Strings — the basic case, including counters.
- Hashes — field-value maps, good for objects.
- Lists — ordered, usable as simple queues.
- Sets and sorted sets — membership tests and leaderboards.
- Streams — append-only logs with consumer groups.
Operations on these are atomic, which matters more than it sounds. Incrementing a counter, or checking and setting in one step, needs no lock of your own.
The caching patterns
Cache-aside (lazy loading)
The default, and what most people mean by caching. Your application looks in the cache; on a miss it reads the database, writes the result to the cache, and returns it.
Good: simple, and only caches what is actually
requested.
Bad: every miss pays the full cost, and a burst of misses on
the same key can stampede the database.
Write-through
Writes go to the cache and the database together, so the cache is never stale.
Good: consistency.
Bad: every write costs more, including writes to data
nobody will read.
Write-behind
Writes go to the cache and are flushed to the database asynchronously.
Good: fast writes.
Bad: data can be lost if the cache dies before the flush.
Use only where that loss is acceptable.
Invalidation, which is the actual problem
Three approaches, in increasing order of effort and correctness:
- Time to live. Set an expiry and accept staleness up to that window. Crude, effective, and the right starting point for most things.
- Explicit invalidation. Delete the cached key when the underlying data changes. Correct when you remember every place that writes; the failure mode is the one you forgot.
- Versioned keys. Include a version or timestamp in the key, so new data writes to a new key and the old one simply expires. Avoids the "did I invalidate everywhere" problem entirely.
Start with a TTL. Reach for the others when you can name specific data where staleness causes a real problem — not before.
Eviction: what happens when memory fills
Redis holds everything in RAM, so you must decide what happens at the limit. The policy is configured, and the default may not be what you want:
- noeviction — writes start failing. Correct for a queue or session store where losing data is worse than an error.
- allkeys-lru — discard the least recently used. The usual choice for a pure cache.
- allkeys-lfu — discard the least frequently used. Better when some keys are persistently hot.
- volatile-* — the same policies, but only over keys that have a TTL set.
Using one Redis instance as both a cache and a session store is a common source of pain, because the two want opposite policies. Separate them.
Redis against Memcached
Memcached is simpler and multithreaded, and for a pure string cache at scale it remains perfectly good. Redis gives you data structures, persistence options and replication. If all you need is "store this blob under this key", Memcached is not a worse answer — it is a smaller one.
The licence change, and Valkey
Worth knowing before you commit. Redis changed its licence terms in 2024, moving away from the permissive BSD licence it had used for years. In response, a fork of the last BSD-licensed version was created and is now maintained under the Linux Foundation as Valkey.
For most applications Valkey is a drop-in replacement, and several cloud providers adopted it. Which you choose is mostly a licensing and support question rather than a technical one — but check both projects' current terms yourself, because the terms here have already changed once.
Before you add a cache at all
A cache is a second source of truth, and that has a permanent cost in complexity. Two things to try first:
- Look at the query. A missing index frequently explains the whole problem, and fixing it leaves nothing to invalidate.
- Check the access pattern. A query running inside a loop is a code problem, not a caching problem.
Our comparisons of PostgreSQL against MySQL and SQLite against PostgreSQL cover the layer underneath, and what a CDN is covers caching at the edge, which solves a different part of the same problem and is often the cheaper win for a content site.
Written by the WritePures newsroom. Spotted something that needs updating? Tell our editors.
0 Comments
Join the conversation
Replying toNo comments yet. Be the first to share your thoughts.