Scrivio Interview library
InternalsIntermediate6 min read · updated 13 November 2026

A team builds server-assisted client-side caching into a Java service and wants to know whether it can ever serve a value that Redis has already changed.

Walk through the races and failure cases in client-side caching and how a correct client handles each one.

Yes, a naive client can serve a value Redis has already changed, in four main ways: an invalidation that overtakes the reply to the read that filled the cache, invalidations lost while a connection was down, tracking state lost on failover or restart, and special cases such as a flush or the client's own writes under NOLOOP. A correct client marks keys as in flight and discards a reply if an invalidation arrived meanwhile, flushes its whole cache on any disconnect or failover, treats a null invalidation as drop everything, and bounds the cache with a size limit and a maximum TTL as a safety net.

Why this question is asked

Server-assisted client-side caching looks simple: Redis tells us when a key changes and we drop our copy. The trouble is that a message saying 'this key changed' and the reply carrying the key's value travel separately and can arrive in either order, and messages can be lost entirely. Interviewers ask this to see whether we think about distributed timing rather than the happy path. In this answer, we will recap how tracking works, then go through each race and failure case with what a correct client does, and finish with the safety nets that catch whatever we miss.

1A Quick Recap of Tracking

With CLIENT TRACKING ON, Redis records which keys a connection reads and sends that connection an invalidation, a message carrying key names, when any of them is modified, expires or is evicted. The client keeps a local map from key to value and deletes entries named in invalidations. With RESP3 the invalidation arrives as a push message on the same connection. With RESP2 it arrives on a second connection subscribed to the __redis__:invalidate channel, linked to the data connection with REDIRECT.

Everything below follows from one fact: the local cache is correct only if every invalidation is received and applied in the right order relative to the reads that fill the cache.

2Race 1: The Invalidation Arrives Before the Reply

Suppose our client sends GET price:7 to fill its cache. While the GET is in flight, another client runs SET price:7 99. Redis can register our read, then process the SET, and send us an invalidation. If the invalidation travels on a separate RESP2 connection, it can reach our application before the GET reply does. Our code applies the invalidation (nothing to delete yet), then receives the old value and stores it. Now we hold a stale value and no further message will come, because Redis already sent its one invalidation for that key. Even with RESP3 on one connection, a client that processes push messages and replies on different threads can create the same window.

Step throughThe read race
Our clientRedisOther client
GET price:7 (cache fill)
SET price:7 99
invalidate [price:7] (arrives first)
old value 80 (arrives second)
stores 80: stale forever

5 messages in order. Play them, or step through them one at a time.

The fix is to remember that the key was in flight and discard the reply if an invalidation came in meanwhile.

3Fixing the Read Race With an In-Flight Marker

The standard fix is to put a placeholder in the local cache before sending the read. If an invalidation for that key arrives while the placeholder is there, we mark it as invalidated. When the reply arrives, we store the value only if our placeholder is still present and unmarked; otherwise we return the value to the caller for this one request but do not cache it.

Java
Object marker = new Object();
local.put(key, new Entry(marker, null, false)); // placeholder: in flight
String value = redis.get(key); // "80"
local.compute(key, (k, e) ->
(e != null && e.marker() == marker && !e.invalidated())
? new Entry(marker, value, false) // safe: cached
: e); // invalidated meanwhile: not cached
// invalidation handler
void onInvalidate(String k) {
local.computeIfPresent(k, (key, e) -> e.value() == null
? new Entry(e.marker(), null, true) // in flight: mark it
: null); // cached: remove it
}

Notice that the marker is a unique object per read, so two overlapping reads of the same key cannot confuse each other, and the updates use compute so the check and the store happen atomically with respect to the invalidation handler.

4Failure 2: Lost Invalidations on Disconnect

Invalidations are fire-and-forget. If the connection drops for two seconds, any invalidations Redis tried to send in that time are gone, and Redis does not replay them when we reconnect. A client that keeps its cache across a reconnect is serving data that may have changed during the gap.

So the rule is strict: on any disconnect of the data connection, or of the invalidation connection with RESP2, the client flushes its entire local cache before serving from it again, re-enables tracking on the new connection, and refills lazily. With RESP2 there is an extra trap: if the invalidation connection dies while the data connection stays up, Redis keeps tracking but has nowhere to deliver messages, so the client must treat that as a disconnect too.

5Failure 3: Failover, Restart and Flushes

Tracking state lives only in the memory of the Redis node the client is connected to. It is not written to disk and not replicated to replicas. After a failover, the new primary knows nothing about our tracked keys, so it will never send invalidations for them. A restart has the same effect. A correct client detects the topology change or reconnect, flushes its cache, and enables tracking on the new primary.

Two more special cases need handling. After FLUSHALL or FLUSHDB, Redis sends an invalidation whose key list is null, which means drop everything, and a client that only loops over key names will silently do nothing. With NOLOOP, Redis does not tell a connection about its own writes, so when our client writes a key it must update or delete its own copy in the same code path as the write.

6Safety Nets: Bounds, TTL and Monitoring

Even with all of this, bugs and missed messages happen, so the local cache gets two hard limits. A maximum size, for example 50,000 entries in Caffeine, stops memory growth. A maximum TTL, for example 60 seconds, caps how long any stale value can survive whatever went wrong. The TTL is not the main invalidation mechanism; it is the bound on the damage.

Finally we measure it: local hit rate, invalidations received per second, cache flushes on reconnect, and a sampled comparison of local values against Redis. A sudden drop in invalidations with steady writes is the sign that the channel is broken.

Step throughWhat a correct client does on each event
  1. Read a keyPut an in-flight marker, then GET
  2. Invalidation for keyRemove value, or mark in-flight entry
  3. Invalidation with null keysDrop the whole cache
  4. Own write under NOLOOPUpdate or drop own copy
  5. Disconnect or failoverFlush all, re-enable tracking
  6. AlwaysSize limit and max TTL as a backstop

6 steps in order. Play them, or step through them one at a time.

7Follow-up questions

Why is a TTL still useful when invalidation works?

Because invalidation works only if every message is delivered and applied, and real systems lose messages through bugs, missed reconnect handling, network quirks or server-side table pressure. A maximum TTL turns an unbounded staleness bug into a bounded one: whatever goes wrong, no value is older than, say, 60 seconds. It also lets rarely used entries leave the cache so memory stays proportional to the working set.

Do client libraries handle these races for me?

Good implementations handle some of them, such as flushing on reconnect, but the details differ by library and version, so read the client's documentation and test the behaviour, especially failover. If you build on top of raw tracking, you own all of these cases.

Does RESP3 remove the read race entirely?

It removes the ordering problem on the wire, because pushes and replies travel in order on one connection. But if the client hands push messages and replies to different threads, or applies the reply after a delay, the race comes back inside the application, so the in-flight marker is still good practice.

8Conclusion

In this answer, we saw that server-assisted caching can serve stale data when an invalidation overtakes the reply that fills the cache, when invalidations are lost during a disconnect, when tracking state disappears on failover or restart, and when flushes or the client's own writes are not handled. A correct client uses in-flight markers, flushes everything on reconnect or failover, treats a null invalidation as drop everything, updates its own writes under NOLOOP, and keeps a size limit and maximum TTL with monitoring as a safety net.

What to say in the interviewAbout 80 seconds aloud

Open with

Yes, it can serve stale data unless the client handles a few specific cases. The first is a read race: I send a GET to fill the cache, another client changes the key, and the invalidation reaches me before the GET reply, which is likely with a separate RESP2 invalidation connection.

Then, if they want more

If I then store the reply, I keep a stale value forever, because Redis only sends one invalidation. So I put an in-flight marker in the cache before the read, mark it if an invalidation arrives, and only cache the reply if the marker is clean.

The second is connection loss: invalidations sent while I am disconnected are simply lost, so on any disconnect of either connection I flush the whole local cache. The third is failover or restart: tracking state is in the node's memory and not replicated, so on the new primary I enable tracking again and flush.

I also handle an invalidation with a null key list, which comes after FLUSHALL and means drop everything, and with NOLOOP I update my own copy when I write. On top of that I bound the cache by size and a maximum TTL, and I monitor hit rate and invalidation rate.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Redis documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Walk through the races and failure cases in client-side caching and how a correct client...": https://scrivio-interview-library.netlify.app/databases/redis-internals/0c1fcbe774/