If your backend processes a few thousand jobs a day, you almost certainly don't need a broker. A Postgres job queue on the database you already run is enough, and it comes with transactional enqueueing that Redis can't give you. But there are two ways to build it, and they fail in very different ways. This post compares them, explains what we picked for a small backend, and shows where each one breaks.
The two approaches for a Postgres job queue
SELECT FOR UPDATE SKIP LOCKED uses row locks. A worker claims rows and skips any that another transaction already holds:
BEGIN; SELECT id, payload FROM jobs WHERE status = 'queued' AND run_at <= now() ORDER BY run_at LIMIT 1 FOR UPDATE SKIP LOCKED;- Do the work, then
UPDATE jobs SET status = 'done'andCOMMIT.
Advisory locks use pg_try_advisory_lock(key), an application-defined lock that isn't attached to any row. A worker picks a candidate job, tries to lock its id, and works only if the call returns true.
The difference that matters is the lock's lifetime. A row lock lives and dies with the transaction. A session-level advisory lock lives until you unlock it or the connection closes. Nearly every failure mode below follows from that.
Where SKIP LOCKED breaks
Long transactions. The simple pattern holds the transaction open for the whole job. If a job calls a slow third-party API for 40 seconds, you hold a transaction, a connection, and a snapshot for 40 seconds. That blocks vacuum from cleaning dead tuples newer than your snapshot. On a hot jobs table with frequent status updates, bloat shows up quickly and the claim query slows down as it scans dead rows.
The fix is to claim and work in separate steps. Claim in a short transaction by setting status = 'running', locked_at = now() and locked_by = worker_id. Commit, do the work outside any transaction, then write the result in a second short one. You now need a reaper that requeues rows stuck in running past a timeout, and your jobs must be idempotent, because a slow-but-alive worker can lose its claim and the job runs twice.
Connection exhaustion. In the hold-the-transaction version, each in-flight job pins a connection. Ten workers at concurrency 10 is 100 connections. Postgres handles that poorly, and your API shares the same pool. We've seen queue workers starve the request path because both drew from one limit. Claim-then-release avoids it, and so does giving workers their own small, capped pool.
Hot-row contention. With ORDER BY and LIMIT 1, every worker scans the same head of the index. SKIP LOCKED makes this cheap, but a partial index on (run_at) WHERE status = 'queued' is what keeps it that way as the table grows with finished rows.
Where advisory locks break
Lock leaks on pooled connections. This is the one that bites. Session-level advisory locks belong to the backend connection, not to your code. Behind PgBouncer in transaction pooling mode, the pg_advisory_lock call and the later pg_advisory_unlock can land on different server connections. The unlock returns false with only a warning, and the original connection keeps the lock after it's returned to the pool. The job is now invisible to every worker until that backend dies.
Even without PgBouncer, an exception path that skips the unlock leaves the lock on a connection your app pool will happily reuse. The symptom is a job that never runs again and no error anywhere. pg_locks with locktype = 'advisory' shows the culprit, but only if you think to look.
Transaction-level locks (pg_try_advisory_xact_lock) remove the leak, since commit or rollback always releases them. But then you're back to holding a transaction for the duration of the work, which is the first SKIP LOCKED problem again.
Key space collisions. Advisory locks take a 64-bit integer, or two 32-bit ones. Hashing a UUID down to that range risks collisions, and an unrelated feature using the same key space can deadlock or starve your queue. Nothing in the database tells you who owns a key.
No durable state. A lock records nothing about attempts, errors, or progress. You still need a jobs table, so advisory locks only replace the claim step. They don't give you a queue.
What we chose
For small backends we default to a jobs table, a short claim transaction using FOR UPDATE SKIP LOCKED, work outside the transaction, a locked_at heartbeat, and a reaper. Every failure is visible in an ordinary row: attempts, last error, status. We keep advisory locks for a narrower job, ensuring only one instance runs a singleton task like a nightly reconciliation, using the transaction-level variant.
When you really do need Redis or a broker
Postgres stops being the right answer in a few concrete cases:
- Sustained throughput where claim and update churn dominates your write load and autovacuum can't keep up.
- Fan-out and pub/sub patterns, where polling adds latency.
LISTEN/NOTIFYhelps, but it isn't durable and has payload limits. - Jobs enqueued from services that don't share your database, which loses the transactional-enqueue advantage.
- Strict ordering or rate limiting per tenant, which you can build in SQL but will end up reimplementing a broker.
For most early-stage products, the real cost of Redis isn't the hosting. It's a second system that can disagree with your database. A job enqueued before a transaction rolls back, or lost after it commits, is a bug class you only avoid by writing the job in the same transaction as the data.
At orithLabs, we pick infrastructure by how it fails, not how it benchmarks, and we say plainly when a simple design has a ceiling. If you're deciding whether your queue needs a broker, or you have one that's misbehaving under load, we're happy to look at your schema and worker code with you.