Implementing Blocking Commands

1. Using Blocking List Operations

CommandDescription
BLPOP k1 k2 ... timeoutBlock until element available; 0=forever
BRPOP k1 k2 ... timeoutTail equivalent

2. Using BLMOVE for Reliable Queues

CommandUse
BLMOVE src dst LEFT|RIGHT LEFT|RIGHT timeoutAtomic transfer; survive crashes by re-processing from dst

3. Using BLMPOP for Multiple List Operations

CommandDescription
BLMPOP timeout numkeys k1 ... LEFT|RIGHT [COUNT n]Block until first non-empty list 7.0+

4. Using Blocking Sorted Set Operations

CommandDescription
BZPOPMIN k1 k2 ... timeoutBlock for lowest-score member
BZPOPMAX k1 k2 ... timeoutBlock for highest-score member

5. Using BZMPOP for Multiple Sorted Sets

CommandDescription
BZMPOP timeout numkeys k1 ... MIN|MAX [COUNT n]Multi-key blocking pop 7.0+

6. Using Blocking Stream Reads

CommandDescription
XREAD BLOCK ms STREAMS key $Tail new entries only
XREADGROUP GROUP g c BLOCK ms STREAMS key >Group consumer pattern

7. Setting Block Timeout

ValueBehavior
0Block forever
Positive int (seconds)Return nil on timeout
Float (e.g. 0.5)Sub-second timeout 6.0+

8. Blocking Multiple Keys

BehaviorDetail
First-non-empty winsKeys checked left-to-right
FairnessFIFO across blocked clients per key

9. Understanding Client Blocking

AspectDetail
Connection stateSingle connection held until unblock/timeout
CLIENT UNBLOCKForce unblock another client
INFO clientsblocked_clients counter

10. Handling Timeout Responses

Command FamilyOn Timeout
BLPOP/BRPOPReturns nil
XREAD BLOCKReturns nil

11. Implementing Queue Patterns

Example: Reliable worker loop

while (running) {
    String job = jedis.brpoplpush("jobs:pending", "jobs:processing", 5);
    if (job == null) continue;
    try {
        process(job);
        jedis.lrem("jobs:processing", 1, job);
    } catch (Exception e) {
        // leave in processing; reaper requeues stale entries
    }
}

12. Avoiding Indefinite Blocking

Warning: Pair every blocking call with a reasonable timeout. Use a pool with TCP keepalive to detect dead sockets.
MitigationDetail
Finite timeoute.g. 5–30s, retry loop
Separate poolDon't share with non-blocking ops
CLIENT UNBLOCK id TIMEOUTOperator override