StoicBuilder

HandbookXIII the AqueductQueues and workers

XIIIthe Aqueduct, Scale it

Queues and workers

1 min readUpdated 28 November

In plain words

A queue holds jobs that don't need to finish during the request, and workers are separate processes that take jobs off it, retry the ones that fail and set aside the ones that never succeed.

Where you see it

When a Harbor customer upgrades, the request saves the order and answers at once. A job to send the receipt, render the invoice PDF and update the CRM goes onto a queue, such as BullMQ on Redis or Amazon SQS, and workers finish it seconds later.

services/billing/queue.tsserver
import { Queue, Worker } from "bullmq";const invoices = new Queue("invoices", { connection }); await invoices.add("render", { orderId }, { jobId: `invoice-${orderId}`, attempts: 5, backoff: { type: "exponential", delay: 2000 },}); new Worker("invoices", async (job) => renderInvoice(job.data.orderId), { connection, concurrency: 10 });
A stable jobId stops duplicates, attempts and backoff ride out passing failures, and concurrency caps how many run at once.

What goes wrong

  1. Jobs that aren't safe to run twice

    Most queues deliver at least once, so a worker that crashes after sending the receipt may send it again on retry. Make each job idempotent: check whether the work is already done before doing it.

  2. Retrying forever

    A job that fails because its data is wrong fails on every retry and keeps workers busy. Cap the attempts, then move it aside to a dead-letter queue or failed list where someone will look at it.

  3. Nobody watching the queue

    Workers die quietly and the queue grows to 200,000 jobs before a customer asks where their invoice is. Alert on queue depth and on the age of the oldest waiting job.

What to tell your agent

Copy this into Lovable, Bolt, Cursor or any agent
Move invoice rendering and receipt emails out of the checkout request into a queue with separate workers. Make each job idempotent with a stable job ID, retry with exponential backoff up to 5 times, move jobs that still fail to a dead-letter queue, and expose queue depth and oldest-job age as metrics.

Quick check

Sign in to earn +10 XP for it

A Harbor worker crashed right after emailing a receipt but before marking the job done. The queue retries it. What prevents a second email?

Got a prompt that touches queues and workers?

Paste it. You get a better prompt first, then the cards that explain it.

Check your own prompt

Free, no sign-up.