Skip to the page

Background work, webhooks and real-time · mid

Queues and background jobs (BullMQ)

Slow or unreliable work (emails, image processing, calling partners) should not run inside the HTTP request: the API puts a job on a queue, answers quickly (often 202), and separate workers process jobs with retries, accepting that a job may run more than once.

The notes

What you must know

  • Why: fast responses, retries when a provider is down, smoothing traffic spikes, and running work on separate machines.
  • BullMQ (v6 current) is the common Node choice on Redis: queue.add(name, data, opts); a Worker processes jobs with a concurrency setting; jobs can be delayed, prioritised, rate-limited and repeated (Job Schedulers via upsertJobScheduler).
  • Retries: { attempts: 5, backoff: { type: 'exponential', delay: 1000 } } -> waits of 1 s, 2 s, 4 s...; failed jobs stay in the failed set for inspection (a dead-letter list).
  • Delivery is at-least-once: a worker can crash after doing the work but before acking, so the job runs again. Make handlers idempotent (dedupe by job or business id).
  • Deduplication: a fixed jobId or BullMQ's deduplication option stops the same job being queued twice.
  • Other options: AWS SQS (visibility timeout, dead-letter queue), Kafka (an event log with consumer groups and replay), RabbitMQ.
  • Run workers as separate processes or services; they must also shut down gracefully (finish or release active jobs).

The red pen

Common traps: mistake → fix

  • Mistake: A queue guarantees exactly-once processing

    Fix: most queues are at-least-once; design idempotent consumers.

  • Mistake: Use setTimeout or a cron inside the API process for background work

    Fix: jobs are lost on restart and duplicated when you scale out.

Easy to hard

Questions interviewers ask, easy to hard

  1. Why use a queue instead of doing the work in the request?
  2. How do retries work in BullMQ or SQS? What is a dead-letter queue?
  3. A job sends an email and the worker crashes right after. What happens next?
  4. Kafka vs SQS vs BullMQ: when would you use each?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview