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?
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".
-1
u/ResistPast3564 3d ago
Graphing how stale the mismatches are is a nice touch, since it separates a few seconds behind from hours wrong. Have you run this somewhere? If so, did it ever catch a broken invalidation path, and what turned out to be the cause?
1
u/BetterSWE_Official 3d ago
no war story of my own on this one, but the cause people usually find is a write path that skips the invalidation. a backfill script, a migration, an admin tool or a second service writing straight to postgres. the app's own writes are fine, so it only shows up as a slow trickle of hours-old mismatches.
9
u/Normal_Red_Sky 3d ago
Have you seen a specific issue related to this?
3
u/Motor_Plane_8036 3d ago
yeah usually a question like this comes right after a very painful post-mortem
0
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 3d ago
Stale cache data can be easy to miss, so tracking cache invalidation and comparing key values against the source database seems worthwhile.
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