---
title: "Comment by OneLifeVibe on \"Your Cache Is Serving Stale Data: 8 Caching Mistakes and How to Fix Them\" | Hashnode"
description: "Mistake 2 is the one I see teams learn the hard way: writing the new value into the cache after the database write feels like the tidy option, and then a concur"
url: "https://hashnode.com/posts/your-cache-is-serving-stale-data-8-caching-mistakes-and-how-to-fix-them/6af1605851c5c3e6b909aff2/comment/6af197898e701a05b6b90e93"
---

[Discussion](https://hashnode.com/posts/your-cache-is-serving-stale-data-8-caching-mistakes-and-how-to-fix-them/6af1605851c5c3e6b909aff2)

Comment [Your Cache Is Serving Stale Data: 8 Caching Mistakes and How to Fix Them](https://hashnode.com/posts/your-cache-is-serving-stale-data-8-caching-mistakes-and-how-to-fix-them/6af1605851c5c3e6b909aff2)

[OneLifeVibe](https://hashnode.com/@onelifevibe)

[6d ago](https://hashnode.com/posts/your-cache-is-serving-stale-data-8-caching-mistakes-and-how-to-fix-them/6af1605851c5c3e6b909aff2/comment/6af197898e701a05b6b90e93)

Mistake 2 is the one I see teams learn the hard way: writing the new value into the cache after the database write feels like the tidy option, and then a concurrent request that read the old row repopulates the stale value after your delete. The advice to delete on write and keep a short TTL as a backstop is the honest version — it narrows the race instead of pretending it doesn't exist. The checklist item I would add to the pre-launch list is a test that logs in as two users, which you do cover under mistake 6; that two-user test has caught more cache bugs for me than any amount of TTL tuning. One question: for the stampede fix, do you prefer the single-rebuild lock with the others serving the old copy, or background refresh before expiry, when the data changes every few seconds rather than every few minutes?

[Prism Infoways](https://hashnode.com/@prisminfoways)

[3h ago](https://hashnode.com/posts/your-cache-is-serving-stale-data-8-caching-mistakes-and-how-to-fix-them/6af1605851c5c3e6b909aff2/comment/6af197898e701a05b6b90e93#reply-6afa85c5cc5f518a70e1b121)

Thanks, and agreed on the two-user test. It's cheap, and it catches the bugs that TTL tuning only hides.

On the stampede question: when the data changes every few seconds, I lean towards background refresh before expiry rather than the lock. With a TTL that short, the lock path gets taken constantly, so every expiry produces a small burst of waiting requests (or stale reads), and the lock key itself turns into a hot key. Refreshing ahead from a single worker keeps the hot keys warm, readers almost never see a miss, and your staleness bound is simply the refresh interval, which is easy to reason about.

Two caveats from doing it in practice:

1.  Only refresh keys that are actually hot. Refreshing everything ahead of time wastes work on keys nobody reads, so we track recent access and refresh only those keys. Probabilistic early expiry (a request near the end of the TTL occasionally volunteers to recompute) gets you most of the benefit without a separate worker.
2.  Keep the single-rebuild lock as the fallback for cold keys and for the moments when the refresher falls behind.

And if the data changes every second or two and readers need it exact, it's worth asking whether it belongs in a TTL cache at all. Pushing updates on write (pub/sub or change events) is often simpler than fighting the race.
