Quick revision
The short answer
We send the SET and then WAIT on the same connection: with the Lettuce sync API that is commands.waitForReplication(1, 100), and with Spring Data Redis we run both commands in one RedisCallback so they share a connection. The result is the number of replicas that acknowledged all earlier writes of that connection within 100 ms. A lower number does not undo the write, and even a full acknowledgement does not stop a failover from promoting a replica that missed it, so WAIT lowers the chance of loss but does not make Redis strongly consistent.
What to remember
In this answer, we wrote WAIT after a token write with Lettuce's waitForReplication and with a Spring Data Redis callback that keeps both commands on one connection. We saw that WAIT blocks its connection, so it belongs on a dedicated connection, and that in a cluster it must reach the key's primary. The result counts replicas that acknowledged; a low count does not undo the write, and even a full count does not guarantee the write survives a failover, so WAIT reduces the risk rather than removing it.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We send the SET and then WAIT on the same connection: with the Lettuce sync API that is commands.waitForReplication(1, 100), and with Spring Data Redis we run both commands in one RedisCallback so they share a connection. The result is the number of replicas that acknowledged all earlier writes of that connection within 100 ms. A lower number does not undo the write, and even a full acknowledgement does not stop a failover from promoting a replica that missed it, so WAIT lowers the chance of loss but does not make Redis strongly consistent.
Why this question is asked
One-time payment tokens are small, short-lived and important: if a token disappears after a failover, a customer's payment fails. Teams then reach for WAIT, and the interview question checks whether we can call it correctly from Java and, more importantly, whether we know what the number it returns really means. In this answer, we will recap what WAIT does, write it with plain Lettuce, then with Spring Data Redis, deal with the connection problems it causes, and finish with what the result does and does not promise.
1A Quick Recap of WAIT
Redis replicates asynchronously: the primary replies OK before replicas have the write. WAIT numreplicas timeout is a command that blocks the calling client until at least numreplicas replicas have acknowledged every write this client sent before the WAIT, or until timeout milliseconds pass. It then returns how many replicas acknowledged, which may be more or fewer than requested.
Two words in that description matter for the code. "This client" means the connection: WAIT looks at the replication offset of the last write sent on the same connection. And "blocks" means the connection can do nothing else until WAIT returns. Both shape how we write it in Java.
2Using WAIT With the Lettuce Sync API
Lettuce exposes WAIT as waitForReplication(int replicas, long timeout). We call it right after the SET on the same connection, so it waits for that SET.
Notice that the SET has already succeeded before WAIT runs. WAIT is only a question we ask afterwards: has this reached a replica yet?
3Doing the Same With Spring Data Redis
Spring Data Redis has no dedicated WAIT method, so we send it as a raw command. The important part is to run both commands inside one callback, which guarantees they use the same connection. If we called redisTemplate.opsForValue().set() and then a separate WAIT call, a connection pool could hand out two different connections, and WAIT would wait for nothing.
The cast is needed because execute() returns Object; for WAIT, Lettuce returns the integer reply as a Long. Check your version if the cast fails, since custom command results are driver-specific.
4Keeping WAIT From Blocking Everyone Else
Here is the catch. Lettuce, and Spring Boot's default Lettuce setup, share one native connection per node across all threads in the application. Commands from every thread are pipelined over that single socket and answered in order. WAIT blocks that socket for up to 100 ms, so every other command from every other thread queues behind it. Under load, one token write can add 100 ms to unrelated cache reads.
The fix is a dedicated connection for WAIT: a separate StatefulRedisConnection as in the Lettuce example, or a second LettuceConnectionFactory with setShareNativeConnection(false) and a pool, used only by the token-writing template. In Redis Cluster there is one more rule. WAIT must go to the primary that owns the key, because each primary tracks its own replicas. A cluster-aware client may route a keyless command like WAIT to an arbitrary node, so we obtain the connection for the key's node explicitly, for example by computing the slot with SlotHash.getSlot(key) and asking the cluster connection for the node that serves it.
6 messages in order. Play them, or step through them one at a time.
Replies come back in order on one socket, so a blocking command delays everything behind it.
5What the Result Tells Us, and What It Does Not
A result of 1 tells us that at that moment, at least one replica had received the token. A result of 0 tells us none had within 100 ms, but the token is still on the primary; WAIT never rolls anything back. So the application chooses: fail the request and let the caller retry, retry the WAIT, or accept the risk for this token and carry on.
What WAIT does not tell us is that the token will survive a failover. If the primary dies, Sentinel or the cluster promotes the replica with the most data, which is usually but not necessarily the one that acknowledged. With two replicas, our token could be on one and the other could win. And the replica had it in memory, not on disk. So WAIT lowers the probability of loss; it does not make Redis strongly consistent. For a payment token, the safer design keeps the token's authoritative record in the database and uses Redis for speed.
6Follow-up questions
How would you choose the timeout?
From the replication lag we actually see and the latency budget of the request. If p99 replica acknowledgement in our environment is 3 ms, a timeout of 20 to 50 ms covers normal conditions with margin, while 100 ms keeps working when a zone link is slow. The timeout is also the worst-case latency we add when replicas are down, so it must fit inside the caller's own timeout. I would measure the WAIT results and durations as a metric and alert when results below the requested count become frequent.
Does WAIT 0 or a timeout of 0 do anything useful?
A timeout of 0 means wait forever, which is dangerous on a request path because a missing replica blocks the connection indefinitely. WAIT 0 with any timeout returns immediately with the current count, which can be used as a cheap check of how many replicas have caught up.
Should we use WAITAOF instead?
If we care about the replica's disk as well as its memory, yes: WAITAOF waits for an fsync on the replicas and optionally locally. It is slower, and it has the same limit during failover, so it complements a durable source of truth rather than replacing it.
7Conclusion
In this answer, we wrote WAIT after a token write with Lettuce's waitForReplication and with a Spring Data Redis callback that keeps both commands on one connection. We saw that WAIT blocks its connection, so it belongs on a dedicated connection, and that in a cluster it must reach the key's primary. The result counts replicas that acknowledged; a low count does not undo the write, and even a full count does not guarantee the write survives a failover, so WAIT reduces the risk rather than removing it.
Open with
I would send the SET and then WAIT on the same connection, because WAIT waits for the earlier writes of that connection. With the Lettuce sync API it is redis.set with an expiry, then redis.waitForReplication(1, 100), which returns how many replicas acknowledged within 100 milliseconds.
Then, if they want more
With Spring Data Redis there is no WAIT method, so I run both inside one RedisCallback: stringCommands().set with Expiration.seconds(300), then connection.execute("WAIT", "1", "100") as bytes, and cast the result to Long.
If the result is lower than one, the token still exists on the primary, because WAIT never rolls back, so I decide whether to fail the request, retry, or accept the risk. One practical point: Lettuce shares one connection across all threads, and WAIT blocks it, so every other command would queue behind it.
I would use a dedicated connection or a non-shared connection factory for these writes, and in Redis Cluster make sure WAIT goes to the primary that owns the key.
Finally, I would be clear about what it means: an acknowledgement says a replica had the write in memory at that moment, but a failover can still promote a different replica, so WAIT makes loss less likely without making Redis strongly consistent. For payment tokens I would keep the authoritative record in the database.
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 "Show how to issue WAIT after the write with Lettuce or Spring Data Redis, and explain...": https://scrivio-interview-library.netlify.app/databases/redis-scaling/69176566fe/