(Note · · 5 min read)
The cache is fast and the data is wrong
Caching gets added when something is slow and removed when something is wrong, and in between nobody is sure which values are current. The decision worth making deliberately is not whether to cache but how stale each piece of data is allowed to be, and who is allowed to be surprised by it.
By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka
Ask how wrong it may be, and for how long
Different data tolerates staleness very differently. A marketing page can be minutes old. A product price can be seconds old. A user's own balance, permissions or just-submitted change must never be. Decide per item rather than applying one duration to everything.
The sharpest rule: never serve stale data back to the person who just changed it. Everyone else seeing a slightly old value is a performance trade; that person seeing it is a bug report, because they know what they did.
Delete rather than update
Writing the new value into the cache at the same time as the database means two systems to keep in step, and they will drift whenever one write succeeds and the other does not. Deleting the key instead means the next read repopulates from the source of truth.
Delete after the database write has committed, not before, or a concurrent read can repopulate the cache with the old value and leave it there until it expires.
Key it for everyone who must not see each other's data
The worst cache bug is not staleness, it is serving one user's data to another. Any cache key covering data that varies per user, per company or per role must contain that identifier.
This matters most in multi-tenant systems, where the same query shape serves many organisations: a key built from the query alone will happily return one company's rows to the next caller. The separation rule that applies to queries applies to cache keys.
Expect the stampede
When a popular key expires, every request that wanted it misses at once and they all recompute the same thing simultaneously, which is how a cache improves average latency and causes an outage on the same afternoon.
Let one caller recompute while the others wait or briefly serve the old value, rather than allowing all of them through. Adding a little randomness to expiry times also stops keys written together expiring together.
Make it inspectable
You will eventually need to answer whether a value is cached, how old it is, and where it came from — usually while someone is insisting the site is showing the wrong thing. Build that in: a way to see a key's age and a way to clear one safely.
And measure the hit rate. A cache with a low hit rate is pure overhead with added inconsistency, and is more common than anyone expects, because it was added for a page that is rarely requested twice.
Common questions
Should I update or delete a cache entry after a write?
Delete it, after the database write has committed. Updating both means keeping two systems in step and they drift when one write fails; deleting means the next read repopulates from the source of truth. Deleting before the commit lets a concurrent read restore the old value.
What must never be served from cache?
Data being read by the person who just changed it. Everyone else seeing a slightly old value is a performance trade-off; the person who made the change seeing it is a bug they will report, because they know what they did.
How do I stop a cache leaking data between users?
Include the user, company or role identifier in the cache key wherever the data varies by it. In multi-tenant systems a key built from the query alone will return one organisation's rows to the next caller.
What is a cache stampede and how do I avoid it?
When a popular key expires, every waiting request misses at once and recomputes the same value simultaneously. Let one caller recompute while others wait or briefly serve the stale value, and add randomness to expiry so keys written together do not expire together.