The problem
The fact ("this merchant reached a new stage") lives in one database. The action ("start their journey") lives in a different service, in a different language. There is no transaction that spans both.
The obvious designs both fail:
- Call the other service inside the transaction. Database locks are held for the length of a network call, and a slow receiver slows every write.
- Call it after committing. If the process dies between the commit and the call, the event is gone, and nothing records that it should have existed.
This is the dual-write problem: two systems and no single transaction across them.
What I built
A transactional outbox. The business change and an outbox row are written in the same database transaction, so the event exists if and only if the fact does. Delivery happens afterwards, from the outbox.
- Queued only after commit. The Celery task is scheduled with
transaction.on_commit, so it can never run for a row that was rolled back. - A sweeper as the safety net. A Celery message can be lost to a broker restart or a killed worker. Every 15 minutes a beat task re-queues outbox rows that are still unsent, and abandons ones too old to be useful.
- Idempotent by construction. A unique constraint on the lead and the event name means a retry cannot create a second event, and a merchant moving backwards through the funnel emits nothing.
- Signed requests. The task serialises the payload once, signs that exact string with HMAC-SHA256 including a timestamp, and sends those exact bytes. The receiver verifies over the raw body.
- Golden vectors. The signer is tested against fixed known-good signatures, not just against its own verifier. A round-trip test passes even when both sides are wrong in the same way.
- A stable public identifier. Events carry the company's UUID rather than its integer primary key, which would expose sequential internal IDs across a service boundary.
The delivery guarantee, stated honestly
This is at-least-once delivery. The task can send an event and then die before marking the row as sent, and the sweeper will send it again. Exactly-once delivery across a network is not achievable, so the receiving side is built to treat a repeated event as a no-op. Together that behaves like exactly-once, without claiming it.
What went wrong after launch
Events started returning 404 in production. It took 26 hours to find the cause: trailing spaces on two lines of an environment file. The configuration library returned them verbatim, so the URL ended in dozens of spaces, and the receiver correctly rejected an app slug it did not recognise. print() shows trailing spaces as nothing; only repr() and a byte dump revealed them.
The more important half was the secret, which carried the same padding. Fixing only the URL would have produced signatures the receiver could not verify, and the receiver answered HTTP 200 to a bad signature. Every event would have been marked sent while doing nothing. I wrote a startup guard that strips and validates both values; it is awaiting merge.
No merchant was affected, because dispatch was still switched off.
What I would add next
- Monitoring on outbox depth and the age of the oldest unsent row, which is the single number that says the pipeline is stuck.
- A receiver that rejects bad signatures with 401 instead of 200, so authentication failures are visible.