WAIT numslaves timeout

此命令阻塞当前客户端,直到所有以前的写命令都成功的传输和指定的slaves确认。如果超时,指定以毫秒为单位,即使指定的slaves还没有到达,命令任然返回。

命令始终返回之前写命令发送的slaves的数量,无论是在指定slaves的情况还是达到超时。

注意点:

  1. 当’WAIT’返回时,所有之前的写命令保证接收由WAIT返回的slaves的数量。
  2. 如果命令呗当做事务的一部分发送,该命令不阻塞,而是只尽快返回先前写命令的slaves的数量。
  3. 如果timeout是0那意味着永远阻塞。
  4. 由于WAIT返回的是在失败和成功的情况下的slaves的数量。客户端应该检查返回的slaves的数量是等于或更大的复制水平。

一致性(Consistency and WAIT)

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, WAITimproves 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.

Implementation details

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:

  1. Detect timed out slaves.
  2. Perform a partial resynchronization after a disconnection.
  3. Implement WAIT.

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.

@return

@integer-reply: The command returns the number of slaves reached by all the writes performed in the context of the current connection.

@examples

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






本文作者:陈群
本文来自云栖社区合作伙伴rediscn,了解相关信息可以关注redis.cn网站。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值