Implementing Blocking Commands
1. Using Blocking List Operations
| Command | Description |
BLPOP k1 k2 ... timeout | Block until element available; 0=forever |
BRPOP k1 k2 ... timeout | Tail equivalent |
2. Using BLMOVE for Reliable Queues
| Command | Use |
BLMOVE src dst LEFT|RIGHT LEFT|RIGHT timeout | Atomic transfer; survive crashes by re-processing from dst |
3. Using BLMPOP for Multiple List Operations
| Command | Description |
BLMPOP timeout numkeys k1 ... LEFT|RIGHT [COUNT n] | Block until first non-empty list 7.0+ |
4. Using Blocking Sorted Set Operations
| Command | Description |
BZPOPMIN k1 k2 ... timeout | Block for lowest-score member |
BZPOPMAX k1 k2 ... timeout | Block for highest-score member |
5. Using BZMPOP for Multiple Sorted Sets
| Command | Description |
BZMPOP timeout numkeys k1 ... MIN|MAX [COUNT n] | Multi-key blocking pop 7.0+ |
6. Using Blocking Stream Reads
| Command | Description |
XREAD BLOCK ms STREAMS key $ | Tail new entries only |
XREADGROUP GROUP g c BLOCK ms STREAMS key > | Group consumer pattern |
7. Setting Block Timeout
| Value | Behavior |
0 | Block forever |
| Positive int (seconds) | Return nil on timeout |
Float (e.g. 0.5) | Sub-second timeout 6.0+ |
8. Blocking Multiple Keys
| Behavior | Detail |
| First-non-empty wins | Keys checked left-to-right |
| Fairness | FIFO across blocked clients per key |
9. Understanding Client Blocking
| Aspect | Detail |
| Connection state | Single connection held until unblock/timeout |
| CLIENT UNBLOCK | Force unblock another client |
| INFO clients | blocked_clients counter |
10. Handling Timeout Responses
| Command Family | On Timeout |
BLPOP/BRPOP | Returns nil |
XREAD BLOCK | Returns 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.
| Mitigation | Detail |
| Finite timeout | e.g. 5–30s, retry loop |
| Separate pool | Don't share with non-blocking ops |
CLIENT UNBLOCK id TIMEOUT | Operator override |