Backend Engineering
Background jobs: when a Postgres queue is enough and when to reach for Redis
Database-backed and Redis-backed job queues compared: durability, throughput, transactional enqueueing, retries and idempotency, and how to choose.
By Envexa Engineering6 min read
Sooner or later every product has work that should not happen inside a web request: sending emails, processing uploads, calling slow APIs, generating reports, running model inference. The question is where the queue lives. The two common answers are your existing PostgreSQL database and Redis. Both work; they fail differently.
The case for a Postgres-backed queue
The decisive advantage is transactional enqueueing. If a job is created in the same transaction that creates the data it relates to, then either both exist or neither does. With an external queue you must handle the gap: the row commits but the enqueue fails, or the job is enqueued but the transaction rolls back.
-- claim one job safely across many workers
UPDATE jobs
SET status = 'running', locked_at = now(), attempts = attempts + 1
WHERE id = (
SELECT id FROM jobs
WHERE status = 'queued' AND run_at <= now()
ORDER BY run_at
FOR UPDATE SKIP LOCKED
LIMIT 1
)
RETURNING *;FOR UPDATE SKIP LOCKED lets many workers pull from one table without blocking each other. For hundreds to low thousands of jobs per second, this is typically sufficient, durable by default, queryable with ordinary SQL and one less system to run.
The case for Redis
Redis is built for high-throughput, low-latency operations and offers primitives that are awkward in SQL: delayed sets, rate limiting, distributed locks, pub/sub for fan-out. It is the better choice when job volume is very high, when jobs are short and numerous, or when the queue load would compete with your primary database.
The costs are durability and consistency. Depending on persistence settings, a failure can lose recently enqueued jobs, and you must design for the enqueue-versus-commit gap, typically with an outbox table: write the job to the database transactionally, then have a relay publish it to Redis.
Whichever you choose: design for at-least-once
Practically every queue delivers a job at least once. Workers crash after doing the work but before acknowledging it, and the job runs again. Therefore:
- Make handlers idempotent. Use an idempotency key (such as the job or business-event ID) and record completion, so repeats are harmless.
- Retry with exponential back-off and jitter, with a maximum attempt count.
- Send exhausted jobs to a dead-letter state that people can inspect and replay.
- Set visibility timeouts: a running job whose worker dies must become claimable again.
- Keep jobs small. Pass identifiers, not large payloads.
A simple decision guide
- Starting out, or under roughly a thousand jobs per second? Use a Postgres-backed queue, with the same transaction as your data.
- Need delays, rate limiting or locks at high volume? Add Redis for those primitives.
- Need durable fan-out, replay or independent consumers across services? Consider a dedicated broker or log such as a streaming platform.
Whatever the backing store, put it behind a small internal interface (enqueue, handler, retry policy). The infrastructure can then change as your load does, without rewriting every job.
Related articles
A production checklist for Dockerised FastAPI services
A practical checklist for running FastAPI in Docker in production: images, workers, health checks, configuration, logging, migrations, shutdown and CI.