w3resource

Redis Keys: WAIT

WAIT numslaves timeout

The Redis WAIT 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:

  1. When WAIT returns, all the previous write commands sent in the context of the current connection are guaranteed to be received by the number of slaves returned by WAIT.
  2. If the command is sent as part of a MULTI transaction, the command does not block but instead just return ASAP the number of slaves that acknowledged the previous write commands.
  3. A timeout of 0 means to block forever.
  4. Since WAIT returns the number of slaves reached both in the case of failure and success, the client should check that the returned value is equal or greater to the replication level it demanded.

Implementation details

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.

Syntax:

WAIT numslaves timeout 

Available since

3.0.0.

Return Value

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

Return Value Type

Integer

Example: Redis WAIT

First, create some keys in redis and set some values in it.

127.0.0.1:6379> SET key1 Apple
OK
127.0.0.1:6379> WAIT 2 1
(integer) 0
127.0.0.1:6379> WAIT 2 1000
(integer) 0
(1.01s)

In the above 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.

Previous: TYPE
Next: Redis Strings SET



Follow us on Facebook and Twitter for latest update.