Read through the layers in order, and use Redis Pub/Sub to tell every instance to delete changed keys. A short TTL on L1 is the safety net.
Reading:
- Check the local L1 cache first. L1 is a cache inside the app process's own memory.
- On a miss, check Redis (the L2 cache, shared by all instances).
- On a miss, read the database.
- On the way back, fill each layer with the value.
Give L1 a short TTL (time to live), in seconds rather than minutes. Also give it a size limit. This keeps stale data and heap (process memory) use small.
Invalidation (removing old copies):
- A service updates the data.
- It deletes the Redis key.
- It publishes the key name on a Pub/Sub channel.
- Every instance subscribes to that channel and evicts the key from its L1.
Pub/Sub can lose messages while a connection is down. So the short L1 TTL stays as the safety net. An instance can also clear its whole L1 after it reconnects.
Redis 6 client-side caching: the CLIENT TRACKING command lets the server help. The server remembers which keys a connection read. In broadcast mode, it tracks key prefixes instead. When those keys change, the server pushes invalidation messages to the client. Client library support varies, so check your library.
- L1 is the in-process cache, L2 is Redis, and the database comes last.
- Give L1 a short TTL and a size limit.
- Broadcast changed keys with Pub/Sub so every instance evicts its L1 entry.
- The short TTL covers missed messages, and an instance clears L1 after it reconnects.
CLIENT TRACKINGlets the Redis server send invalidations for you.