Redis Keys: WAIT
Redis WAIT Command
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:
- 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.
- 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.
- A timeout of 0 means to block forever.
- 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.
Here are some common uses and scenarios for the "WAIT" command:
- Consistency: Ensure that a specified number of replicas have acknowledged writes.
- Durability: Wait for data to be safely replicated across Redis instances.
- Synchronization: Coordinate operations across multiple Redis servers.
- High Availability: Maintain data integrity in Redis clusters.
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
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/redis/redis-wait-key.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics