r/SQL • u/ResistPast3564 • 4d ago
Discussion Keeping a Redis cache in sync when jobs and manual SQL also write to the same tables
I've got Postgres with a Redis cache in front. The app deletes the cache key after it updates a row, and that part works. What I'm unsure about is everything else that writes to those tables:
- background jobs doing bulk `UPDATE`s
- someone fixing data in psql
- migrations and backfills
- another service sharing the same database
None of those go through the app's update, then delete the key path, so as far as I can tell, the cache just stays stale until the TTL runs out.
How are you handling this? Do your jobs and scripts clear the cache too, do you rely on the TTL, or did you move to something based on logical replication or Debezium? Has one of these other writers ever bitten you?
2
u/rathboma 4d ago
I use rails, so usually add a hook in the ORM to do it automatically. So when changing any record the cache gets updated.
That means you've just got to be careful about always using the ORM to make changes.
You can also do it on-read and have a short ttl. So on read it checks the cache, then if no entry it queries the database and adds to the cache, but yes then you can get a stale cache, so the ttl has to be short.
For the Beekeeper Studio cloud services we do the first version (ORM integration).
1
u/Free-Good9203 4d ago
orm callbacks always leak the moment someone runs a bulk update or manual psql fix. if you want invalidation at the database boundary without pulling in kafka and debezium, a tiny `AFTER INSERT OR UPDATE OR DELETE` trigger that calls `pg_notify('cache_inv', table_name || ':' || id)` plus a lightweight worker listening via `LISTEN` and deleting the redis key catches every write path including raw sql. pair it with a short ttl as a safety net in case the listener briefly disconnects.
1
u/jshine13371 4d ago
If you want your cache to immediately refresh for every DML change that happens in the database, it no longer is a cache and now is the database. Hence the reason caching is natively TTL based.
1
u/Discordbothost 4d ago
A reader can fetch the old row, then put it back in Redis after a write commits and clears the key. To test that race, pause a reader between its SELECT and cache write, run a psql update and let invalidation finish, then resume the reader.
1
u/ayamlejen 3d ago
One way around the lost-NOTIFY problem is an outbox table: a trigger writes the changed key into a small cache_invalidations table in the same transaction, and a worker reads those rows, deletes the Redis keys, then deletes the rows. Because it's a real table, nothing is lost if the worker is down. It just catches up when it's back. If you don't want triggers on every table, logical replication (Debezium, or a small consumer on pgoutput/wal2json) sees every write, including psql fixes and migrations, at the cost of more moving parts.
1
u/MongooseWonderful198 18h ago
If every writer cannot call the same invalidation code, move the signal next to the database commit. One practical pattern is an outbox row written in the same transaction as the data change; a worker publishes the affected key/version to Redis and retries until acknowledged. Manual SQL and backfills must use the same stored procedure or explicitly enqueue the outbox event.
Put the row version in the cache value and in the invalidation event. Then a delayed event cannot delete or replace a newer cache entry. TTL remains the final safety net, not the mechanism that makes external writers correct.