Transactional outbox with Postgres and pg-boss: deliveries lost between commit and enqueue
By Michael Cooper, Founder
Measurements from three Postgres-backed AGLedger Servers that send each other every shared state change through a pg-boss queue: what a commit-then-enqueue design lost under database saturation, and what a transactional outbox changed in 3.0.0.
Findings
- Committing a change and then enqueuing its delivery is two writes. During 26 minutes at the database's capacity ceiling, the enqueue failed after commit 77 times on records with no later federation work, and a two-minute recovery sweep re-sent all 77.
- Two deliveries were never made: a dispute resolution that was never dispatched, and a verdict the sweep skipped because the record had queued dispute deliveries since. A sweep keyed on “no work queued for this record” misses any record that queued other work after the lost send.
- No operator signal showed either loss. The status field read
deliveredorpending, no peer was failing, and the dead-letter queue was empty. Reading every record back from every receiver found them. - With the dispatch written as outbox rows in the state-change transaction and relayed by a sweep (3.0.0), a 66-minute run with two saturation windows tracked 1,696 records with 0 missing.
- A retry budget longer than the outage kept an 11.5-minute peer outage out of the dead-letter queue: 100 of 100 records arrived, each applied once.
- A delivery cursor that advanced on a 301 lost 178 of 218 rows. Advancing only on the receiver's own 2xx lost 0 of 169.
How delivery worked before 3.0.0
Each company runs its own AGLedger Server with its own database and signing keys. When Servers federate, the Server that created a shared record sends each state change to its peers as a signed message, and each peer appends it to its own chain.
The state change committed first. An event subscriber then enqueued one pg-boss job per peer, and a worker sent and retried it. pg-boss keeps its jobs in the same Postgres, but the enqueue ran as its own statement after the commit, so a failure between the two left a committed change with no delivery. Every two minutes a sweep re-drove committed terminal records with no federation work queued.
What was lost between commit and enqueue
The API logged 77 failed publishes, each after its transaction had committed, on records that took no further federation work. The sweep recovered all of them. Two deliveries were never made:
- A dispute resolution. The origin's chain held a dispatch entry for the dispute opening and none for the resolution, which committed during the saturated window. Both peers read the dispute as open more than three hours later. The origin read
federationStatus: deliveredand its dead-letter queue was empty. - A verdict. The verdict committed, its publish failed on a database connection timeout, and the caller got a 503. The record was then disputed, which queued dispute deliveries, so the sweep (which took only records with no federation work queued) skipped it. Both peers answered 404 for the record. Its dispute deliveries dead-lettered, and replaying them sent them into the same 404.
Two more records showed a delivery status of partial or pending for a signal both peers already held.
Across the three-hour run, 3,985 records were tracked and 5,382 peer views compared. 3,944 records were clean on every check. The other 41 differed in trail length or delivery bookkeeping, mostly steps that reached a peer out of order and left it a shorter trail with the same outcome. No record ended in a different final state on two Servers or appeared on a Server it was not shared with. Every chain verified (3,364, 3,284 and 3,558), and 120 of 120 exports verified offline.
The outbox in 3.0.0
Each dispatch is staged inside the transaction that changes the record's state. The dispatch entry on the chain, one pending marker per peer carrying the message body, and the event rows commit with the state change or not at all. The enqueue still runs after the commit. If it is lost, the committed marker has no live job, and a sweep every two minutes enqueues its stored body again. The marker is the outbox and the sweep is its relay.
- The recovery sweeps match per kind of delivery, so queued dispute work no longer hides a missing state change.
- Disputes stage their own dispatch (open, withdraw, resolve and the release signal), and a second sweep re-drives a dispute whose state no delivery carries.
- A staging error rolls back to a savepoint and never fails the state change. The dispatch then runs after the commit, covered by the sweeps, and
agledger_federation_dispatch_staging_fallback_totalcounts it. - A delivered leg commits as delivered first. The chain entry recording the delivery is appended in its own transaction and retried without sending again.
- A notarize-only create still dispatches after the commit, with a sweep behind it.
- A staged marker is re-queued whatever its age. The sweeps for the post-commit paths look back six hours by default (
AGLEDGER_FEDERATION_ZERO_ROW_HORIZON_MINUTES).
Staging signs the dispatch entry while the record's lock is held, so on an install that signs with AWS KMS a federated state change holds its lock for one more KMS call. Operators read a record's deliveries in federationLegs and the totals in federation.legs of the operations summary (day-2 operations).
With the outbox: 1,696 records, 0 missing
Three Servers on the 3.0.0 release candidate, 66 minutes at 8 records a minute per Server. A pgbench CPU burner ran against each Server's database twice: 24 clients for 12 minutes, then 80 clients for 8 minutes.
| Check, read from all three Servers | Result |
|---|---|
| Records tracked end to end | 1,696 |
| Missing from a peer they were shared with | 0 |
| Present on a peer they were not shared with | 0 |
| In a different final state on two Servers | 0 |
| Chains verified (A, B, C) | 1,301, 1,299 and 1,482 of the same; 0 broken |
| Exports verified offline, 40 records each exported from all three Servers | 120 of 120 |
| Verdicts that hit a 503 and later converged on every peer | all of them |
| Settlement signals per state change per peer | one |
Retry or dead-letter: an 11.5-minute outage
Each delivery retries 8 times with pg-boss's exponential backoff (10 seconds doubling, with jitter), which spans about 42 to 85 minutes before it dead-letters.
One Server's API and worker were scaled to zero for 11.5 minutes while the other two wrote 100 shared records. On the sender, the peer's consecutive-failure counter read 204 at five minutes, 422 at eight and a half and 596 at ten and a half; the operations summary showed one peer failing, and each affected record read as partially delivered, and nothing dead-lettered. After the Server returned, 100 of 100 records arrived, each with exactly one sync entry, the last about 18 minutes later, as each delivery waited for its next retry slot.
Four cuts of 20 to 60 seconds with writes before, during and after each: 24 records, 0 violations, 0 dead-lettered. Write-to-peer lag was 1 second minimum, 195 seconds median and 524 seconds maximum, because a delivery that fails once waits for its retry slot. In undisturbed windows the lag was 1.5 to 1.8 seconds at p50 and 2.1 to 2.6 seconds at p95.
A stopped worker moves no failure counter, because every peer is healthy. With one worker stopped for five minutes, the count of deliveries queued with no sender went from 0 to 3 at 3 minutes 4 seconds (threshold 180 seconds), and health read degraded: “no worker process is connected to this database”.
Idempotency keys across a database reboot
An outbox makes the sender at-least-once, so each receiver deduplicates on the message identity. A duplicate with the same identity was accepted once, with one chain entry.
Across a 62-second Aurora writer reboot and several pod deletions, 58 client writes retried under their original Idempotency-Key all succeeded, 53 of them across the reboot, with no record created twice. On a single-Server soak, 2,555 keys were used and 205 were sent twice concurrently: no key mapped to two records.
A write can also commit and lose its response. In 3.0.0 a retry under the same key gets 409 IDEMPOTENCY_KEY_IN_FLIGHT with Retry-After while the first attempt is still writing its response, and 409 IDEMPOTENT_WRITE_COMMITTED, with a link to read the result, once that attempt has ended without one (day-2 operations).
A delivery cursor that advanced on a redirect
The Server pushes its security event feed to an HTTP collector in batches and advances a stored cursor after each batch. On 2.2.0 a collector answering 301 counted as delivered, and 178 of 218 rows were never delivered. 3.0.0 advances only on a 2xx from the collector itself. On a pre-release build with that change, the same collector had the batch held and re-sent, and after the collector was fixed 169 of 169 rows arrived. The audit page documents push export, including deduplicating on the row id.
Replayed and forged messages
Each message was sent from a peered Server and scored on what the receiver recorded afterwards.
| Attempt | Answer | Effect on the receiver |
|---|---|---|
| Body altered after signing | 401 | none |
| A valid message replayed | 409 duplicate nonce | none |
| Signed with the wrong key | 401 | none |
| From a Server that is not a peer | 401 | none |
| Addressed to a different Server | 401 | none |
| Timestamp 10 minutes off, either way | 400 | none |
| Signature in the wrong case or the wrong length | 401 | none |
| A peer changing a record it did not create (4 routes, 3 kinds of record) | 404 | none; one row per refusal on the security feed |
Numbers at a glance
| Measure | Result |
|---|---|
| Publishes failed after commit and recovered by the sweep | 77 |
| Deliveries never made, with no operator signal | 2 (a verdict and a dispute resolution) |
| With the outbox: records tracked, missing | 1,696, 0 |
| Exports verified offline (three-hour run, outbox run) | 120 of 120, 120 of 120 |
| Retry budget before a delivery dead-letters | 8 retries, about 42 to 85 min |
| Peer outage; records written during it; arrived exactly once | 11.5 min; 100; 100 |
| Dead-lettered during that outage | 0 |
| Writes retried under the same key across a 62 s reboot and pod deletions | 58 of 58 succeeded |
| Concurrent duplicate keys mapped to two records | 0 of 205 |
| SIEM rows lost to a redirecting collector (2.2.0, pre-release 3.0.0) | 178 of 218, 0 of 169 |
| Forged or replayed messages accepted | 0 |
Test setup
Three Servers on EKS, one t3.medium node each, with three databases on one Aurora PostgreSQL 18 Serverless v2 cluster that five other test databases shared. Twelve agents per Server ran about three hours of cross-server work in all six directions: notarized records, gated accepts and rejects, revisions, cancellations, expiries, disputes and delegation trees. A client-side ledger held the expected state of every record on every Server, and the run ended by reading each record back from all three. The burstable nodes make none of these latencies a capacity figure; the performance post has those.
The three-hour run used a pre-release build 58 commits past 2.2.0, before the outbox change. The cluster was capped at 4 ACU and sat at 100% for the first 26 minutes, with requests timing out and commit paths answering 503, until the ceiling was raised to 16 ACU online. All the losses above date from those 26 minutes; the rest of the run, which included a database reboot, pod deletions and a 10-minute peer block, produced none. The outbox run used the 3.0.0 release candidate. Each figure is from one run.
Sources & further reading
- Pattern: Transactional outbox: the pattern as Chris Richardson describes it, with the relay options
- PostgreSQL 18: Transactions: the all-or-nothing guarantee the outbox rows ride on
- pg-boss on GitHub: the Postgres job queue that carries every delivery here, and its retry backoff
- Federation guide: peering, what is shared, and the delivery fields an operator reads
Related