Blocking List Pops

Difficulty: Intermediate

Question

What are BRPOP and BLPOP in Redis, how do they behave, and how would you build a reliable job queue with them?

Answer

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.

How they behave

  • Syntax: BRPOP key [key ...] timeout. The timeout is in seconds; since Redis 6.0 it can be fractional, like 0.5. 0 means wait forever.
  • The reply is a two-element array: the list name and the popped value. On timeout it returns nil.
  • With several keys, Redis checks them in the order given and pops from the first non-empty one. This gives a simple priority queue: BRPOP jobs:high jobs:low 5.
  • If several clients are blocked on the same list, the one that has been waiting longest is served first.
  • Inside MULTI/EXEC or a Lua script, these commands do not block; they behave like the non-blocking versions.
  • Each blocked call ties up a connection, so workers need a dedicated connection, not one shared with normal cache traffic.

The reliability problem

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:

  1. Worker runs 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).
  2. Worker processes the job.
  3. Worker removes it with LREM processing:worker-1 1 <job>.
  4. A janitor process checks processing lists for jobs stuck too long and pushes them back to jobs.

Lists vs Streams for queues

AspectList with BRPOPList with BLMOVEStream with consumer group
DeliveryAt most onceAt least onceAt least once
Crash recoveryJob lostManual janitorXPENDING and XAUTOCLAIM
Multiple independent consumersNoNoYes, one group each
Replay historyNoNoYes
SimplicityVery simpleSimpleMore 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.

Gotchas

  • Client-side socket timeouts must be longer than the BRPOP timeout, or your library will think the connection died.
  • In Redis Cluster, all keys in one blocking call must hash to the same slot.
  • Blocked clients show up in INFO clients as blocked_clients; a high number is normal for idle workers.
  • A list has no built-in retry count or delay. Track those in the job payload or move to a library or Streams.
Reliable queue with BLMOVE. The job is parked in a processing list until the worker confirms it is done.
  1. Worker → Redis: BLMOVE jobs processing:w1 RIGHT LEFT 5
  2. Producer → Redis: LPUSH jobs send-invoice:771
  3. Redis → Worker: Job moved to processing:w1 and returned
  4. Worker → Worker: Process the job
  5. Worker → Redis: LREM processing:w1 1 send-invoice:771

Code examples

Simple queue with BRPOP

# 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.

Reliable worker in Node.js with BLMOVE

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.

Key points

  • BLPOP/BRPOP block until an element arrives or the timeout expires; 0 means forever.
  • Timeouts accept fractional seconds since Redis 6.0.
  • Multiple keys are checked in order, giving simple priorities.
  • Plain BRPOP is at-most-once; use BLMOVE with a processing list for at-least-once.
  • BRPOPLPUSH is deprecated in favour of BLMOVE (6.2+).
  • Use a dedicated connection for blocking commands.

Common follow-up questions

  • What happens to a job if the worker crashes right after BRPOP returns?
  • How would you add delayed jobs or retries on top of Redis lists?
  • When would you switch from lists to Redis Streams?
  • How does BLPOP behave inside a MULTI/EXEC block?

Interview tips

  • Explain the reliability gap of plain BRPOP and the BLMOVE fix; it is the core insight.
  • Mention modern commands (BLMOVE, BLMPOP) to show your knowledge is current.
  • Compare with Streams and with libraries like BullMQ to show you would not reinvent the wheel.

Concepts covered

BRPOP and BLPOP, Task Queues, BLMOVE Reliable Queue, Blocking Timeout, BLMPOP

More Redis & Caching questions