此指令阻塞目前用戶端,直到所有以前的寫指令都成功的傳輸和指定的slaves确認。如果逾時,指定以毫秒為機關,即使指定的slaves還沒有到達,指令任然傳回。
指令始終傳回之前寫指令發送的slaves的數量,無論是在指定slaves的情況還是達到逾時。
注意點:
當’WAIT’傳回時,所有之前的寫指令保證接收由<code>WAIT</code>傳回的slaves的數量。
如果指令呗當做事務的一部分發送,該指令不阻塞,而是隻盡快傳回先前寫指令的slaves的數量。
如果timeout是0那意味着永遠阻塞。
由于<code>WAIT</code>傳回的是在失敗和成功的情況下的slaves的數量。用戶端應該檢查傳回的slaves的數量是等于或更大的複制水準。
Note that <code>WAIT</code> 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, <code>WAIT</code>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:
Detect timed out slaves.
Perform a partial resynchronization after a disconnection.
Implement <code>WAIT</code>.
In the specific case of the implementation of <code>WAIT</code>, 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 <code>WAIT</code> is called Redis checks if the specified number of slaves already acknowledged this offset or a greater one.
@return
@integer-reply: The command returns the number of slaves reached by all the writes performed in the context of the current connection.
@examples
In the following example the first call to <code>WAIT</code> 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 <code>WAIT</code> unblocks and returns 1, the number of slaves reached.
本文作者:陳群
本文來自雲栖社群合作夥伴rediscn,了解相關資訊可以關注redis.cn網站。