Difficulty: Advanced
What is Redis server-assisted client-side caching, how does the server notify clients about changes, and what are its modes and pitfalls?
Client-side caching lets an application keep a copy of frequently read Redis values in its own process memory, and Redis tells it when those values change so the copy can be dropped. It was introduced in Redis 6 through CLIENT TRACKING: the server remembers which keys a client has read and sends an invalidation message when any of them is modified, expired or evicted. Reads of hot keys then cost a local memory lookup instead of a network round trip.
CLIENT TRACKING ON on its connection.GET product:991) is recorded in a server-side invalidation table.product:991, Redis sends an invalidation for that key to the client and stops tracking it for that client.Redis only promises to tell you the key may have changed. It never pushes the new value.
HELLO 3): invalidations arrive as push messages on the same connection, interleaved with normal replies. This is the simplest setup.SUBSCRIBE __redis__:invalidate on it, and enable tracking on the data connection with REDIRECT <id-of-second-connection>.| Mode | Server memory | What you receive |
|---|---|---|
| Default | Remembers each key each client read | Invalidations only for keys you read |
BCAST with PREFIX | No per-key memory | Invalidations for every change matching the prefixes |
OPTIN | Tracks only keys read right after CLIENT CACHING yes | Fine-grained control over what to cache |
OPTOUT | Tracks everything except reads after CLIENT CACHING no | Exclude volatile keys |
NOLOOP suppresses invalidations for changes the same connection made itself. The default-mode table is capped by tracking-table-max-keys (default 1,000,000); when the cap is hit, Redis evicts entries from the table and sends invalidations for them, so clients drop those cached keys.
Configuration, feature flags, pricing tables, product details on a high-traffic sale page: data read thousands of times per second and changed rarely. Recent versions of popular clients (Lettuce, Jedis, redis-py, node-redis) include built-in support, so check your library before writing your own invalidation handling.
# Terminal 1: RESP3 session with tracking
redis-cli -3
127.0.0.1:6379> CLIENT TRACKING ON
127.0.0.1:6379> GET flag:new_checkout
# Terminal 2: another client changes the key
redis-cli SET flag:new_checkout off
# Back in terminal 1, run any command (e.g. PING) to see the push
127.0.0.1:6379> PING
# Broadcast mode: no per-key tracking, prefix based
127.0.0.1:6379> CLIENT TRACKING ON BCAST PREFIX flag: PREFIX price:
The invalidation arrives as a RESP3 push message. In BCAST mode the client is told about every change under the listed prefixes, whether or not it read the key.
Client-side caching, CLIENT TRACKING, Invalidation messages, RESP3 push, BCAST mode