Blocking Operations
Blocking commands that wait for data to become available, enabling queue patterns and real-time processing.
Commands
Section titled âCommandsâ| Command | Syntax | Description |
|---|---|---|
| BLPOP | BLPOP key [key ...] timeout | Block until element available at list head |
| BRPOP | BRPOP key [key ...] timeout | Block until element available at list tail |
| BLMOVE | BLMOVE src dst LEFT|RIGHT LEFT|RIGHT timeout | Block until element can be moved |
| BRPOPLPUSH | BRPOPLPUSH src dst timeout | Block pop from src, push to dst (deprecated) |
| XREAD BLOCK | XREAD BLOCK ms STREAMS key id | Block until new stream entries |
How Blocking Works
Section titled âHow Blocking Worksâ- Client sends blocking command with timeout
- If data exists, returns immediately
- If no data, server waits until:
- Data becomes available (another client pushes)
- Timeout expires (returns nil)
- Connection closes
Examples
Section titled âExamplesâBLPOP - Blocking List Pop
Section titled âBLPOP - Blocking List PopâConsumer (blocks waiting for work):
127.0.0.1:6379> BLPOP queue:jobs 30# ... waits up to 30 seconds ...1) "queue:jobs"2) "job:123"Producer (adds work):
127.0.0.1:6379> LPUSH queue:jobs "job:123"(integer) 1# Consumer immediately receives the jobMultiple Queues
Section titled âMultiple QueuesâCheck multiple queues, return from first with data:
# Wait on multiple queues (priority order)127.0.0.1:6379> BLPOP queue:high queue:medium queue:low 10# Returns from first non-empty queue1) "queue:high"2) "urgent-task"BRPOP - Pop from Tail
Section titled âBRPOP - Pop from Tailâ# FIFO queue - push left, pop right (blocking)127.0.0.1:6379> BRPOP queue:tasks 0# 0 = wait foreverBLMOVE - Reliable Queue
Section titled âBLMOVE - Reliable QueueâMove element atomically between lists:
# Worker: take job from pending, move to processing127.0.0.1:6379> BLMOVE jobs:pending jobs:processing LEFT RIGHT 30"job:456"
# After job complete, remove from processing127.0.0.1:6379> LREM jobs:processing 1 "job:456"(integer) 1XREAD BLOCK - Stream Blocking Read
Section titled âXREAD BLOCK - Stream Blocking Readâ# Wait for new stream entries127.0.0.1:6379> XREAD BLOCK 5000 STREAMS events $# Waits up to 5 seconds for new entries after current time
# When entry arrives:1) 1) "events" 2) 1) 1) "1234567890123-0" 2) 1) "type" 2) "click" 3) "user" 4) "123"Timeout Handling
Section titled âTimeout Handlingâ# 5 second timeout127.0.0.1:6379> BLPOP empty:queue 5(nil) # Nothing after 5 seconds
# Immediate return if data exists127.0.0.1:6379> LPUSH myqueue "item"(integer) 1127.0.0.1:6379> BLPOP myqueue 301) "myqueue"2) "item" # Returns immediately, doesn't waitZero Timeout (Wait Forever)
Section titled âZero Timeout (Wait Forever)â# Wait indefinitely for work127.0.0.1:6379> BLPOP queue:jobs 0# Blocks until data or connection closedQueue Patterns
Section titled âQueue PatternsâSimple Work Queue
Section titled âSimple Work Queueâ# Producer: add jobsLPUSH queue:jobs "job:1" "job:2" "job:3"
# Consumer: process jobs (blocking)while true: BLPOP queue:jobs 0 # Wait forever # Process job...Priority Queue
Section titled âPriority Queueâ# Producer: add to appropriate queueLPUSH queue:critical "urgent-job"LPUSH queue:normal "regular-job"LPUSH queue:low "background-job"
# Consumer: check in priority orderBLPOP queue:critical queue:normal queue:low 10Reliable Queue with BLMOVE
Section titled âReliable Queue with BLMOVEâ# Step 1: Worker claims jobjob = BLMOVE pending processing LEFT RIGHT 30
# Step 2: Process job# ... do work ...
# Step 3a: Success - remove from processingLREM processing 1 job
# Step 3b: Failure - move back to pendingLMOVE processing pending LEFT LEFTPub/Sub Alternative with Lists
Section titled âPub/Sub Alternative with Listsâ# Subscriber (blocking receive)while true: message = BLPOP channel:notifications 0 # Handle message...
# Publisher (non-blocking send)LPUSH channel:notifications '{"event":"update"}'Stream Blocking Examples
Section titled âStream Blocking ExamplesâReal-Time Event Processing
Section titled âReal-Time Event Processingâ# Consumer: wait for events127.0.0.1:6379> XREAD BLOCK 0 STREAMS events $# $ = only new entries
# Producer: add events127.0.0.1:6379> XADD events * type "click" page "/home""1234567890123-0"Consumer Group with Blocking
Section titled âConsumer Group with Blockingâ# Create group127.0.0.1:6379> XGROUP CREATE events mygroup $ MKSTREAM
# Consumer: blocking read from group127.0.0.1:6379> XREADGROUP GROUP mygroup consumer-1 BLOCK 5000 STREAMS events >Multiple Streams
Section titled âMultiple Streamsâ# Wait on multiple streams127.0.0.1:6379> XREAD BLOCK 10000 STREAMS orders:new orders:updated 0-0 0-0Important Notes
Section titled âImportant NotesâConnection Handling
Section titled âConnection Handlingâ- Blocking commands tie up the connection
- Use separate connections for blocking and regular commands
- Set appropriate timeouts to avoid resource exhaustion
Timeout Values
Section titled âTimeout Valuesâ| Value | Behavior |
|---|---|
0 | Wait forever |
N | Wait N seconds (BLPOP/BRPOP) or N milliseconds (XREAD) |
Mode Differences
Section titled âMode Differencesâ| Mode | Implementation | Best For |
|---|---|---|
| Embedded | Polling with configurable interval | Single-process apps |
| Server | Async/await on TCP connections | Multi-client coordination |
Fairness
Section titled âFairnessâWhen multiple clients wait on the same key:
- First client to wait is first to receive
- FIFO ordering for blocked clients
Use Cases
Section titled âUse CasesâJob Queue
Section titled âJob Queueâ# Multiple workers competing for jobs# Worker 1:BLPOP jobs 0
# Worker 2:BLPOP jobs 0
# Producer adds job - one worker gets itLPUSH jobs "task:123"Rate Limiting with Blocking
Section titled âRate Limiting with Blockingâ# Token bucket pattern# Consumer waits for tokensBLPOP rate:tokens 1
# Background process refills tokensLPUSH rate:tokens "token"EXPIRE rate:tokens 1Delayed Processing
Section titled âDelayed Processingâ# Add job with delay using sorted set + blocking listZADD delayed:jobs <future_timestamp> "job:123"
# Background process moves ready jobswhile true: # Check for ready jobs ready = ZRANGEBYSCORE delayed:jobs 0 <now> LIMIT 0 1 if ready: ZREM delayed:jobs ready LPUSH queue:jobs ready sleep 100ms
# Workers process from blocking queueBLPOP queue:jobs 0Event Aggregation
Section titled âEvent Aggregationâ# Collect events, process in batches# Producer adds eventsLPUSH events:buffer '{"event":"click"}'
# Consumer waits then processes batchwhile true: event = BLPOP events:buffer 5 # 5 second timeout if event: batch.append(event) if len(batch) >= 100 or timeout: process_batch(batch) batch = []