WAIT numreplicas timeout

    Time complexity: O(1)

    This command blocks the current client until all the previous write commands are successfully transferred and acknowledged by at least the specified number of replicas. If the timeout, specified in milliseconds, is reached, the command returns even if the specified number of replicas were not yet reached.

    A few remarks:

    • When returns, all the previous write commands sent in the context of the current connection are guaranteed to be received by the number of replicas returned by WAIT.
    • If the command is sent as part of a transaction, the command does not block but instead just return ASAP the number of replicas that acknowledged the previous write commands.
    • A timeout of 0 means to block forever.

    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, improves the real world data safety.

    However this is just a best-effort attempt so it is possible to still lose a write synchronously replicated to multiple replicas.

    Since the introduction of partial resynchronization with replicas (PSYNC feature) Redis replicas asynchronously ping their master with the offset they already processed in the replication stream. This is used in multiple ways:

    • Detect timed out replicas.
    • Perform a partial resynchronization after a disconnection.
    • Implement WAIT.In the specific case of the implementation of , 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 replicas already acknowledged this offset or a greater one.

    In the following example the first call to does not use a timeout and asks for the write to reach 1 replica. It returns with success. In the second attempt instead we put a timeout, and ask for the replication of the write to two replicas. Since there is a single replica available, after one second WAIT unblocks and returns 1, the number of replicas reached.