r/devops • u/ResistPast3564 • 3d ago
Discussion Do you monitor whether your Redis cache is serving stale data?
Working with a Postgres + Redis setup. We monitor hit rate, latency and memory, but nothing tells us when the cache is wrong.
Has this bitten you? How did you find out - users, an alert, something else?
And does anyone actually measure it, or is it a low TTL and hope?
0
Upvotes
7
u/dutchman76 3d ago
That's the point of ttl so you know the data can be out of date that much. You have to design your app to tolerate that
3
u/BetterSWE_Official 3d ago
You can measure it cheaply by sampling. On a small percent of cache hits, also read from Postgres in the background, compare, and count mismatches as a metric. If you store an updated_at or version with the cached value you can even graph how stale the mismatches are. It's the only way I know to tell "TTL is fine" apart from "an invalidation path is broken".
9
u/Normal_Red_Sky 3d ago
Have you seen a specific issue related to this?
1
u/BrocoLeeOnReddit 3d ago
Do you have a lot of issues with stale data? In our stack, it's not really a problem since our setup isn't write-heavy so we basically just use TTLs (depending on which queries we use different TTLs).
If you REALLY need to have Redis and PostgreSQL line up perfectly, you could do Change Data Capture (CDC), e.g. use Debezium to sync PostgreSQL's WAL to Redis or use Redis' own implementation RDI (Redis Data Integration): https://redis.io/docs/latest/integrate/redis-data-integration/
And to monitor it, you could do some sampling, e.g. check 0.1% of all cache hits against the actual database and log/count the differences.
1
u/Aide_Sea 3d ago
One sneaky cause I think is a slow read that grabbed the old row, then writes it to cache after the invalidation already ran. Code looks right but the stale value sits until TTL. Versioned keys maybe help with that.
1
u/Mean_Guest_5043 2d ago
Stale cache data can be easy to miss, so tracking cache invalidation and comparing key values against the source database seems worthwhile.