Difficulty: Intermediate
What are BRPOP and BLPOP in Redis, how do they behave, and how would you build a reliable job queue with them?
BLPOP and BRPOP are blocking versions of LPOP and RPOP. If the list has an element, they pop and return it immediately; if it is empty, the connection waits until another client pushes an element or the timeout expires. This avoids polling and makes a Redis list a simple, low-latency work queue where producers LPUSH jobs and workers BRPOP them.
BRPOP key [key ...] timeout. The timeout is in seconds; since Redis 6.0 it can be fractional, like 0.5. 0 means wait forever.BRPOP jobs:high jobs:low 5.MULTI/EXEC or a Lua script, these commands do not block; they behave like the non-blocking versions.With BRPOP, the job leaves Redis the moment it is delivered. If the worker crashes halfway through processing, the job is gone. For a notification you might accept that; for a payment reconciliation job you cannot.
The classic fix is the reliable queue pattern:
BLMOVE jobs processing:worker-1 RIGHT LEFT 5, which atomically moves the job to a per-worker processing list (6.2+; BRPOPLPUSH is the deprecated older form).LREM processing:worker-1 1 <job>.jobs.| Aspect | List with BRPOP | List with BLMOVE | Stream with consumer group |
|---|---|---|---|
| Delivery | At most once | At least once | At least once |
| Crash recovery | Job lost | Manual janitor | XPENDING and XAUTOCLAIM |
| Multiple independent consumers | No | No | Yes, one group each |
| Replay history | No | No | Yes |
| Simplicity | Very simple | Simple | More concepts |
Redis 7.0 also added BLMPOP timeout numkeys key ... LEFT|RIGHT [COUNT n] to pop several elements at once from the first non-empty list.
Many job libraries build on these primitives. BullMQ, popular in Node.js, uses lists, sorted sets and Lua scripts for delayed jobs, retries and locks, so you do not have to write the janitor yourself.
BRPOP timeout, or your library will think the connection died.INFO clients as blocked_clients; a high number is normal for idle workers.# Terminal 1: worker blocks for up to 10 seconds
redis-cli BRPOP jobs:high jobs:low 10
# Terminal 2: producer pushes a job
redis-cli LPUSH jobs:low '{"type":"sms","to":"+919876543210"}'
The worker unblocks as soon as the job arrives. jobs:high is checked first, so it acts as a priority lane.
import Redis from 'ioredis';
// Dedicated connection for blocking commands
const redis = new Redis();
const WORKER = 'processing:worker-1';
async function work() {
while (true) {
// Atomically move a job into this worker's processing list
const job = await redis.blmove('jobs', WORKER, 'RIGHT', 'LEFT', 5);
if (!job) continue; // timed out, loop again
try {
await handle(JSON.parse(job));
await redis.lrem(WORKER, 1, job); // acknowledge
} catch (err) {
console.error('job failed, leaving for janitor', err);
}
}
}
async function handle(job) {
console.log('processing', job);
}
work();
If the worker crashes, the job stays in its processing list where a janitor can find and requeue it.
BRPOP and BLPOP, Task Queues, BLMOVE Reliable Queue, Blocking Timeout, BLMPOP