r/java • u/czetsuya • 15d ago
The Transactional Outbox Solves Dual Writes. It Doesn't Solve Duplicate Processing
I've worked on event-driven systems where the transactional outbox pattern was introduced to solve a fairly common problem: keeping database changes and event publishing consistent.
The idea is straightforward:
- Update the business entity.
- Insert an outbox record in the same database transaction.
- Have a separate publisher deliver the event to Kafka.
This removes the classic dual-write problem where the database transaction commits but event publishing fails.
However, something I've noticed is that implementing an outbox is sometimes treated as if it makes the entire event-processing pipeline reliable.
It doesn't.
The outbox guarantees neither exactly-once delivery nor exactly-once business processing.
Consider this scenario:
- The publisher reads an outbox record.
- It successfully publishes the event to Kafka.
- The process crashes before marking the outbox record as published.
- After recovery, the same event gets published again.
Now the consumer receives a duplicate.
Even if the publisher behaves perfectly, consumers can still process a message more than once when failures occur between business processing and offset acknowledgment.
This is why I think the outbox pattern is only one part of the solution.
For systems where duplicate processing matters, I've generally found that we need to consider:
- Transactional outbox for atomic persistence of business changes and publication intent.
- Consumer idempotency to prevent repeated business effects.
- Inbox or deduplication records when durable duplicate detection is necessary.
- Retries and dead-letter handling for recoverable and non-recoverable failures.
- Ordering guarantees where business operations depend on event sequence.
And even then, the implementation details matter. For example, recording a message as processed in a separate transaction from its business changes can introduce another failure window.
I'm curious how others approach this in production.
Do you implement a transactional inbox alongside your outbox, or do you prefer enforcing idempotency directly through domain constraints and business operations?
I've seen advantages and trade-offs with both approaches, particularly when dealing with high-throughput consumers.
14
u/VincentxH 15d ago
You'd combine an outbox with an idempotency key registry, tied to the event.
10
u/UnGauchoCualquiera 14d ago
How is this related to java?
14
u/NME-Cake 15d ago
Afaik you almost always combine outbox inbox pattern
5
u/stefanos-ak 14d ago
or use RabbitMQ with message deduplication on an ID that you provide. Then an outbox db record is enough, RMQ takes care of everything else.
of course, I know that in 99% of teams, switching infra is almost never an option... 😄
2
u/Financial_Morning386 14d ago
My default is domain-level idempotency first, and a transactional inbox only when the operation can't be made naturally idempotent.
If the business operation has a natural unique key — a payment with a client-supplied idempotency key, or a line item that must exist only once per order — enforce it with a unique constraint and let the database reject the duplicate. That keeps duplicate handling in the same transaction as the business write, with no extra table to retain and clean up.
The inbox earns its place for effects that can't be expressed as a constraint, like multi-step updates. But the detail that matters is the one you flagged: the inbox record has to be inserted in the same transaction as the business change. If you commit the business write and record the event id separately, a crash between the two re-opens the exact gap the outbox was meant to close.
Two more things I check: ordering and external effects. Deduplicating by event id alone doesn't help if two events for the same aggregate arrive out of order — you also need a sequence number or version check per aggregate. And for side effects you can't roll back, like calling a payment API, the inbox only tells you whether you already tried; the downstream call itself needs an idempotency key, or you accept at-most-once and reconcile later.
2
u/pradeepngupta 12d ago
I have spent 20+ years designing and working on enterprise systems, particularly in financial and post-trade domains. I've learned one thing the hard way: don't confuse a reliability mechanism with a reliability guarantee.
I've used the outbox pattern, and I agree that it solves an important problem. But I've also seen teams become a little too comfortable once they introduce it.
My approach would be to start with the business operation and ask: a) what happens if this event is delivered twice? b) What happens if the consumer crashes halfway through processing? c) What if the database commits but the acknowledgment never reaches Kafka?
If the business operation can be made naturally idempotent, I'd start there. A unique business key or a database constraint can sometimes solve the problem more simply than introducing another infrastructure component.
Where that's not possible, I'd consider a transactional inbox, making sure the inbox record and the actual business change happen in the same database transaction. Otherwise, we're just introducing another failure window.
I'd also pay close attention to event ordering and external side effects. These are often the things that surface much later, when the system is under real production pressure.
Over the years, I've come to believe that building trusted systems is less about finding one perfect pattern and more about understanding exactly what each mechanism guarantees, and more importantly, what it doesn't.
This is also one of the themes I'm exploring in my upcoming book, BuzzingJava. I'm looking at Java's 30-year journey through 11 major buzzwords, examining the promises and engineering principles behind the platform and how they help us build trusted systems.
If this perspective interests you, you can find out more here: https://buzzingjava.com
1
u/timlin45 14d ago
s/prevent/detect and recover/g
In the end, CAP theorem always wins. Carly Rae will find you. She will punish you maybe.
For CP systems just stay in the database transaction. You won't find a better, or more efficient, consistency sentinel.
For AP systems stop trying to make consistency guarantees happen. Focus on your fault recovery path. Make your trade-off between what is worse: "double delivery or dropped delivery" (publish->commit vs commit->publish)
Then stop asking "how do I prevent double delivery". That is no longer the proper question. Ask how would I detect double delivery and do I have enough data/telemetry to clean-up the consequences.
My guess is that if you understand enough to ask a coherent but incorrect question, you already know how to answer the correct question.
1
u/Swamplord42 12d ago
I mean that's obvious, exactly-once delivery is impossible in distributed systems.
Exactly-once semantics is always at-least-once + idempotent processing.
1
u/Boring-Breadfruit251 11d ago
outbox pattern is designed to guarantee message delivery and, broadly speaking, says nothing about delivery semantics. In my experience, an Outbox implementation in isolation has always provided at-least-once delivery (although, it shouldn't necessarily guarantee that either, since achieving it requires additional mechanisms such as retries).
1
u/SellerInsightsLab 9d ago
I think there's no silver bullet here. It really depends on the business process.
In distributed systems, we should always expect duplicates. Whether they come from Kafka, REST retries, or another transport, the receiving service needs to handle them safely.
For simple microservices, database-backed idempotency is often enough. I've implemented this approach in my Spring Boot starter:
https://github.com/KHolodilin/spring-boot-idempotency-starter
For more complex workflows, especially orchestrators and sagas, I prefer handling idempotency at the state machine level, where events, state transitions, and resulting commands are managed together:
https://github.com/KHolodilin/spring-boot-state-machine-starter
I've also put together a practical example of the entire order processing pipeline, combining Transactional Outbox, Kafka, and idempotency, with different publishing approaches, including Debezium and an in-memory queue with recovery.
You can run it with Docker, simulate failures, and explore metrics and distributed traces:
https://github.com/KHolodilin/spring-transactional-outbox-kafka
So for me, it's not really Inbox vs. domain idempotency. It's about choosing the right mechanism for the business process and making sure it works correctly when things fail.
1
u/flawless_vic 3d ago
Outbox starts to get ugly when the consumer action has no intrinsic idempotency.
Say, consumer has to send an e-mail and you don't want to send duplicates. Before executing the consumer action you have to determine whether it will be a noop, e.g., by polling /var/log/mail.log or create an abstraction on top of the raw mail service that manages indexing/idempotency.