A transactional outbox and reliable background jobs for Django on PostgreSQL. Publish an event or enqueue a job in the same transaction as the data it's about. If the transaction commits, the work happens — even if the process dies on the next line.
from django.db import transaction from reliable_outbox import publish, job @job(retries=5, backoff="exponential") def send_receipt(order_id: int) -> None: ... with transaction.atomic(): order.save() publish("order.created", {"id": order.id}, key=f"order-{order.id}") send_receipt.enqueue(order.id) # runs only if this transaction commits
Saving to the database and telling another system about it are two writes to two systems — and no transaction spans both. The outbox pattern turns it into one: the event is a row in the same database, in the same transaction; a worker delivers it afterwards and retries until it succeeds.
Publish events routed to a destination, or enqueue background jobs with retries, backoff and timeouts — all through the same outbox row.
Messages sharing a key run one at a time, in insertion order, across every worker, through retries. Different keys run in parallel.
Permanent failures land in a dead-letter state with their last error and traceback, and can be requeued from the CLI or the admin.
Local handlers, Redis Streams, Kafka or signed webhooks — route events by pattern, no broker required for the simple case.
At-least-once means consumers see duplicates. Claim a message id in the same transaction as the work and a duplicate becomes a no-op.
NOTIFY wakes idle workers on commit, with polling as a fallback.
Coordination is row locks alone — run as many workers as you like.
Every promise marked Yes is exercised by a test against a real PostgreSQL server: rollback, crash and lease expiry, concurrent workers, ordering, backoff, dead-lettering and webhook signatures.
| Guaranteed? | |
|---|---|
| Rolled-back transaction publishes nothing | Yes |
| Nothing lost once committed | Yes |
| Per-key order across all workers | Yes |
| External side effects happen exactly once | No — at least once |
| Global order across keys | No |