WAIT numslaves timeout
This command blocks the current client until all the previous write commands are successfully transferred and acknowledged by at least the specified number of slaves. If the timeout, specified in milliseconds, is reached, the command returns even if the specified number of slaves were not yet reached.
The command will always return the number of slaves that acknowledged the write commands sent before the WAIT command, both in the case where the specified number of slaves are reached, or when the timeout is reached.
A few remarks:
Note that WAIT does not make Redis a strongly consistent store: while synchronous replication is part of a replicated state machine, it is not the only thing needed. However in the context of Sentinel or Redis Cluster failover, WAIT improves the real world data safety.
Specifically if a given write is transferred to one or more slaves, it is more likely (but not guaranteed) that if the master fails, we'll be able to promote, during a failover, a slave that received the write: both Sentinel and Redis Cluster will do a best-effort attempt to promote the best slave among the set of available slaves.
However this is just a best-effort attempt so it is possible to still lose a write synchronously replicated to multiple slaves.
Since the introduction of partial resynchronization with slaves (PSYNC feature) Redis slaves asynchronously ping their master with the offset they already processed in the replication stream. This is used in multiple ways:
In the specific case of the implementation of WAIT, Redis remembers, for each client, the replication offset of the produced replication stream when a given write command was executed in the context of a given client. When WAIT is called Redis checks if the specified number of slaves already acknowledged this offset or a greater one.
Integer reply: The command returns the number of slaves reached by all the writes performed in the context of the current connection.
> SET foo bar OK > WAIT 1 0 (integer) 1 > WAIT 2 1000 (integer) 1
In the following example the first call to WAIT does not use a timeout and asks for the write to reach 1 slave. It returns with success. In the second attempt instead we put a timeout, and ask for the replication of the write to two slaves. Since there is a single slave available, after one second WAIT unblocks and returns 1, the number of slaves reached.
© 2009–2017 Salvatore Sanfilippo
Licensed under the Creative Commons Attribution-ShareAlike License 4.0.