The Stale Data Nightmare

I thought I could just set a TTL (Time To Live) of 5 minutes and call it a day. I was wrong.

The Problem with TTL

TTL is eventually consistent, but for a payment system, "eventual" isn't good enough. A user would update their credit card, but the checkout page would still show the old one for 4 minutes and 59 seconds.

The Fix: Event-Driven Invalidation

We moved to an active invalidation strategy using Postgres triggers and a pub/sub mechanism.

// When a user updates their profile...
await db.users.update(id, data);
await cache.del(`user:${id}`);
await pubsub.publish('user-updated', { id });

This ensures that immediately after a write, the next read will fetch fresh data from the DB.