Architecture & opsUpdated 2026-11-20

Message queues

Taking work out of the request: queues, retries, outboxes and the pg-boss choice I made in IMS and the outbox I designed for POS.

Take the order now, cook it in the back.

The customer gets a ticket and a smile. The kitchen works through the tickets at its own pace. Nobody stands at the till while the food is made.

How it works

A message queue separates the producer of work from the consumer of it. The request records that something must happen, then returns. A worker does the slow part later, outside the request and response cycle.

Take a photo upload. Done synchronously, the user waits for every step:

Code
Upload → Resize → Thumbnail → Moderation → Save → Response

Done with a queue, the user waits for one step:

Code
Upload → Store photo → Enqueue → Response (immediate)

Worker → Resize → Thumbnail → Moderation

That is asynchronous processing. The caller does not need the result to continue, so it does not wait for it.

What you get

  • Lower latency for the person on the other end.
  • Retries without bothering the user. A failed job can run again.
  • Smoothing. A traffic spike lands in the queue, and workers drain it at a steady rate.
  • Independent scaling. Producers and consumers can grow separately.

A queue fits anything slow and not needed right now: email, notifications, report generation, AI jobs, image and video work, and webhooks to third parties.

What you must also design

A queue does not make work reliable by itself. Three things come with it.

  1. Retry behaviour. Blind retries can turn an outage into a bigger one. Use exponential backoff, a maximum attempt count and jitter, so that a hundred failed jobs do not all return at the same second.
  2. Idempotency. A retry runs the same job again. If the job sends an email or takes a payment, running it twice is a bug. The job, or the thing it calls, has to be safe to repeat.
  3. How the user learns the result. Polling, a push over a socket, or an email. If you skip this, "done" is invisible.

The outbox

The trap is writing the business row and then enqueueing in a separate step. A crash between the two loses the work silently. The fix is an outbox: record the intent in the same transaction as the business change, and let a worker drain it.

Code
BEGIN
  insert sale
  insert outbox row (intent to send)
COMMIT
        ↓
worker reads outbox → sends → marks done (retry with backoff on failure)

Where I used it

Two of my projects lean on this, with different stages of completion.

In IMS I chose pg-boss, a queue in Postgres, so jobs are durable and tied to the business transaction, without Redis. The cost is that the database carries the queue load, and a worker and its schedules have to be running. The fiscal queue there is built against a sandbox provider only. Live submission is not finished, and I say so.

In POS SaaS the design uses a transactional outbox for fiscal invoices. The sale and its intent to transmit commit together. A worker drains the outbox with backoff, and the receipt prints at once and says fiscal confirmation is pending. I rejected sending inside the sale, because it ties the counter's availability to the tax authority's. POS is early-stage and this is a design, not a production claim.

Trade-offs

  • Eventual results. The user gets an answer before the work is done. You now have two states to explain, such as "pending" and "confirmed".
  • More to operate. Workers, schedules, dead jobs and retry limits need monitoring. A queue nobody watches fails without anyone knowing.
  • Depth is the wrong number. I would watch the age of the oldest pending job first. A deep queue can be healthy. An old job is not.
  • Duplicates are normal. At-least-once delivery means you will see repeats. That is why this note sits next to idempotency.
  • Do not queue what the caller needs now. Checking whether a username is free during signup must be answered in the request. For a small app, a deferred call may be enough, and the queue is infrastructure the app has not earned.