PostgreSQL SKIP LOCKED as a Job Queue Instead of Kafka

Using PostgreSQL FOR UPDATE SKIP LOCKED as a transactional job queue for small background workloads, and when Kafka is the better choice.

· 2 min read · Erik Kubica

I once took over a codebase where a five-person startup had spent three months setting up an Apache Kafka cluster to send password reset emails and generate monthly invoice PDFs.

They were processing maybe eighty background jobs an hour.

Their setup used three Kafka brokers, a cluster of ZooKeeper nodes, and several JVM runtimes using 4 GB of RAM on AWS. When consumer group rebalancing happened during rolling deployments, their workers froze for two minutes while email queues backed up.

When I asked why they picked Kafka, the answer was: "We wanted an event-driven architecture so our background processing could scale."

The Dual-Write Problem

One thing that is easy to overlook when you put Kafka or RabbitMQ next to your relational database is the dual-write problem.

Consider a simple checkout:

  1. You open a database transaction and save the new order in PostgreSQL.
  2. You commit the transaction.
  3. You publish an OrderPlaced event to Kafka so the email worker can send a receipt.

What happens when step 3 fails because of a brief network timeout or Kafka broker failover? The database has the order, but the event bus never got the message. The customer's card was charged, but the receipt was never sent and inventory was never decremented.

To solve this with Kafka, you typically build the Transactional Outbox pattern with Debezium and Kafka Connect. That adds several more distributed systems just to make sure an email gets sent.

The Built-In Option: FOR UPDATE SKIP LOCKED

PostgreSQL already has a concurrent queuing mechanism built into its engine: FOR UPDATE SKIP LOCKED.

Here is the query I use for background workers on small projects:

-- Concurrently claim the next available job without blocking other workers
WITH next_job AS (
    SELECT id 
    FROM background_jobs
    WHERE status = 'pending' AND scheduled_at <= NOW()
    ORDER BY scheduled_at ASC
    FOR UPDATE SKIP LOCKED
    LIMIT 1
)
UPDATE background_jobs
SET status = 'processing', locked_at = NOW()
FROM next_job
WHERE background_jobs.id = next_job.id
RETURNING background_jobs.id, background_jobs.task_type, background_jobs.payload;

When five Go worker routines run this query at the same time, Postgres skips any row currently locked by another worker, so they don't block each other.

You can also insert the background job inside the same transaction that creates the order:

BEGIN;
INSERT INTO orders (id, user_id, amount) VALUES (101, 42, 89.00);
INSERT INTO background_jobs (task_type, payload) VALUES ('send_receipt', '{"order_id": 101}');
COMMIT;

If the transaction rolls back, the job never exists. If it commits, the job is there to be processed. The dual-write problem goes away.

Rough Comparison

Infrastructure Factor Apache Kafka Cluster PostgreSQL SKIP LOCKED
RAM Footprint 4,000 MB+ (JVM Heap) No extra (uses existing DB)
Transactional Guarantee Eventual (requires Outbox) Atomic with your data
Monthly Server Cost $180 – $400 / month No extra cost
Throughput on NVMe 100,000+ msg/sec Around 12,000 jobs / sec

For small background workloads, a queue table in the database you already run is often enough.

On a 4-core production instance, PostgreSQL handled around 10,000 jobs per second with SKIP LOCKED in my experience. If you need very high throughput, many independent consumers, event replay, or streaming across several systems, Kafka is built for that and is the right tool. For a few jobs an hour, I'd start with Postgres and move later if the workload outgrows it.

Tell me what you need built

Usually a reply within a day, by email or WhatsApp.