· Source: Lukas Grigis, Spring Boot outbox and inbox and the MIT-licensed companion repo lukas-grigis/spring-outbox-inbox, Spring Boot 4.2, Spring Modulith 2.2, RabbitMQ 4.4 over AMQP 1.0, Postgres 18, Java 25.
The short version
A service records a sale and must tell another service about it. That is two systems and two commits, with no transaction spanning them. Most teams know the outbox pattern in theory. This experiment measures four wirings under identical, seeded chaos: kill -9 on both apps, SIGKILL on RabbitMQ, and Toxiproxy dropping broker confirmations and consumer acknowledgements for 6 seconds at a time. A script counts the damage directly from both databases.
Each run sent 2,000 sale requests; between 1,049 and 1,415 sales committed depending on the wiring:
| Wiring | Lost | Ghosts (stamped but rolled back) | Applied twice |
|---|---|---|---|
| Commit, then send | 274 | 0 | n/a |
| Send inside the transaction | 0 | 25 | n/a |
| Transactional outbox (Spring Modulith) | 0 | 0 | 25 |
| Outbox plus consumer inbox | 0 | 0 | 0 (25 duplicates caught) |
There was one run per version on one machine, and the inbox version ran on three seeds. The direction matches the theory, and the scale is the useful part: 274 of 1,316 committed sales (about one in five) lost by the naive "save then send" approach under this fault mix.
How the winning design works
Producer outbox. The sale row and an
event_publicationrow commit in one local transaction. A relay sends after commit and marks the publication complete only when the broker answers ACCEPTED. A scheduled resubmitter retries failed or stale publications with the same bytes, the same message id and the same SHA-256 content hash.Broker. A quorum queue with a delivery limit and a dead-letter (parking) queue, configured by policy, not queue arguments.
Consumer inbox. In one transaction,
INSERT INTO inbox (message_id) ... ON CONFLICT DO NOTHING, and apply the effect only if the insert returned 1. A 0 means already seen: count a duplicate if the hash matches, and park the message as an id-reuse error if it doesn't.Settle after commit. The consumer accepts the delivery only after its transaction commits, requeues with a count on error, and discards to the parking queue when the message is bad.
The outbox fixes lost and ghost messages but guarantees at-least-once delivery, so duplicates appear. The inbox turns at-least-once delivery into effectively-once processing.
Non-obvious findings (each pinned by a test in the repo)
Released deliveries loop forever. Spring's default AMQP 1.0 listener releases a message when the handler throws. Since RabbitMQ 4.3 a release doesn't count toward the quorum queue's delivery limit, so a poison message is never parked. The fix is to settle every delivery by hand with a counted requeue.
Lost connections do count. A connection that dies with an unsettled delivery counts as a failed delivery. That is why the consumer takes one message at a time: with a deeper prefetch, one dropped connection charges a failure to every in-flight message.
Unroutable sends fail cleanly. A send the broker can't route isn't accepted. The publication stays in the registry, and the resend carries an identical id and hash.
The inbox insert must be the uniqueness test. It is an
INSERT ... ON CONFLICTquery, not an ORMsave(). Two consumer instances receiving the same id concurrently then serialise on the row: the second waits and gets 0.
Tradeoffs
Latency and extra writes. Every message costs an outbox row on the producer and an inbox row on the consumer, and delivery waits for the relay.
Inbox retention. The inbox table grows without limit unless you expire ids older than the broker's maximum redelivery window.
Only for local effects. The inbox works because the effect (a stamp row) lives in the same database. If the consumer calls an external API, you need idempotency keys on that API instead.
Single prefetch limits throughput. One message at a time is safe but slow. Raising prefetch needs a different answer to lost-connection accounting.
Review checklist
Does any code path commit a state change and then send a message without an outbox?
Does the consumer record the message id in the same transaction as its effect, using an insert whose result decides whether to proceed?
Is the message settled only after the consumer's commit?
Do poison messages reach a parking queue? Test it, because broker and client defaults changed (RabbitMQ 4.3 release semantics).
Is prefetch set with lost-connection failure counting in mind?
Do resends reuse the same id and bytes, and does the consumer verify a content hash so an id reused for a different payload is caught?
Do you have a seeded chaos test that counts lost, ghost and duplicate messages from the databases, not from logs?
Related entries
R084 Building always-on agents: five primitives for agents that sleep, wake and act. It puts the same outbox and inbox primitives around an agent loop.
R035 Durable execution: the same crash-between-commit-and-send gap, solved at the workflow layer instead of the messaging layer.
R102 AI-assisted performance tuning overfits to the benchmark machine. Another example of measuring a claim with a script, not by assertion.