KHANH NGUYENENGINEERING · AI · PRODUCTS
EN

Writing / Backend architecture

Postgres is the queue

From Companion OS and a content pipeline for techdecoded.net

Two systems I built this year need background work. Companion OS ingests and indexes documents; a content pipeline for my blog ingests posts, generates social assets and pulls analytics. Neither runs a message broker. In both, the queue is a Postgres table.

The bug a broker gives you for free

Consider the simplest version with Redis or SQS next to the database:

BEGIN;
  INSERT INTO documents ...;
COMMIT;
queue.enqueue("index", doc_id);   -- what if the process dies right here?

If the process dies between the commit and the enqueue, the document exists but never gets indexed. Swap the order and you can index a document that was rolled back. This is the dual-write problem, and every fix for it (retries, reconciliation jobs, idempotency keys) is code written to paper over having two sources of truth.

When the queue is a table, the enqueue is just another row in the same transaction. It commits or rolls back with the data. The bug can’t exist.

Claiming work without a broker

SELECT id, payload FROM platform.jobs
WHERE status = 'queued' AND run_at <= now()
ORDER BY run_at
FOR UPDATE SKIP LOCKED
LIMIT 1;

SKIP LOCKED lets many workers poll the same table without blocking each other or taking the same job. Each claimed job gets a lease; if a worker dies, the lease expires and the job becomes claimable again. That means handlers can run more than once, so they are written to be idempotent.

Events: the transactional outbox

Companion OS also needs domain events. When a document revision is created, indexing should start. The use case writes the event into an outbox table in the same transaction as the change. A relay, which polls and also listens to LISTEN/NOTIFY for low latency, publishes outbox rows to an in-process event bus, and handlers enqueue jobs. Delivery is at-least-once, and processed event ids are recorded so handlers can ignore duplicates.

The same idea fits serverless too. The blog pipeline runs on Vercel with no long-lived worker: a scheduled HTTP tick drains the jobs table with the same SKIP LOCKED query, staying inside the platform’s execution limits.

What I rejected, and what would change my mind

  • Redis / BullMQ: reintroduces the dual write, and adds another stateful service to run and back up.
  • Kafka or NATS: built for throughput and fan-out I don’t have.
  • Database triggers with NOTIFY: NOTIFY isn’t durable; a listener that is down misses the message.
  • Change data capture (Debezium): powerful, and far too much infrastructure for one team of one.

None of these are bad tools. The question is what problem I have today. I’d revisit the decision when a measurement says so: job throughput that makes the jobs table a hot spot, fan-out to consumers outside this database, or a context extracted into its own service. Until then, the most reliable distributed system is the one I didn’t build.

Working on a hard problem?

I’m happy to talk about retrieval, backend architecture, developer tools, or anything on this site.