Client-Side Caching (Redis 6+)

Difficulty: Advanced

Question

What is Redis server-assisted client-side caching, how does the server notify clients about changes, and what are its modes and pitfalls?

Answer

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.

How tracking works

  1. The client enables tracking with CLIENT TRACKING ON on its connection.
  2. Every key the client reads (for example GET product:991) is recorded in a server-side invalidation table.
  3. The client stores the value locally.
  4. When anyone changes product:991, Redis sends an invalidation for that key to the client and stops tracking it for that client.
  5. The client deletes its local copy; the next read goes to Redis and is tracked again.

Redis only promises to tell you the key may have changed. It never pushes the new value.

RESP3 vs RESP2 delivery

  • RESP3 (after HELLO 3): invalidations arrive as push messages on the same connection, interleaved with normal replies. This is the simplest setup.
  • RESP2: push messages do not exist, so you open a second connection, SUBSCRIBE __redis__:invalidate on it, and enable tracking on the data connection with REDIRECT <id-of-second-connection>.

Tracking modes

ModeServer memoryWhat you receive
DefaultRemembers each key each client readInvalidations only for keys you read
BCAST with PREFIXNo per-key memoryInvalidations for every change matching the prefixes
OPTINTracks only keys read right after CLIENT CACHING yesFine-grained control over what to cache
OPTOUTTracks everything except reads after CLIENT CACHING noExclude 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.

Pitfalls

  • Connection loss. If the connection drops, you may miss invalidations. A correct client flushes its whole local cache on disconnect.
  • Race between read and invalidation. Mark the key as in-flight before the read reply arrives; if an invalidation comes in first, do not cache the stale reply.
  • Memory on both sides. Bound the local cache with an LRU and a max TTL as a safety net.
  • Fan-out cost. With many app instances caching the same hot key, every write sends many invalidations. Works best for read-heavy, rarely changing data.

When it is worth it

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.

Tracking and invalidation over RESP3. Redis remembers what the app read and tells it when that key changes.
  1. App (local cache) → Redis: HELLO 3, CLIENT TRACKING ON
  2. App (local cache) → Redis: GET flag:new_checkout
  3. Redis → App (local cache): "on" (key now tracked)
  4. Admin service → Redis: SET flag:new_checkout off
  5. Redis → App (local cache): push: invalidate flag:new_checkout
  6. App (local cache) → Redis: Next read misses locally, GET again

Code examples

Seeing invalidations with two redis-cli sessions

# 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.

Key points

  • Enabled with CLIENT TRACKING ON (Redis 6+); the server sends invalidations, never new values
  • RESP3 delivers invalidations on the same connection; RESP2 needs REDIRECT to a connection subscribed to __redis__:invalidate
  • Modes: default, BCAST with PREFIX, OPTIN, OPTOUT; NOLOOP skips your own writes
  • Flush the local cache on disconnect to avoid serving stale data
  • Best for hot, read-heavy, rarely changing keys

Common follow-up questions

  • How would you handle the race where an invalidation arrives before the read reply?
  • When would you choose BCAST mode over default tracking?
  • What happens when tracking-table-max-keys is exceeded?
  • How does this compare to a plain in-process cache with a short TTL?

Interview tips

  • Explain the full loop: track on read, invalidate on write, refetch on miss
  • Mention RESP3 vs RESP2 REDIRECT; it is the detail most candidates miss
  • Discuss the disconnect problem to show you think about consistency

Concepts covered

Client-side caching, CLIENT TRACKING, Invalidation messages, RESP3 push, BCAST mode

More Redis & Caching questions