Transactional Outbox & Deduplicating Inbox in Distributed Services
Guaranteeing atomic state persistence and idempotent event delivery across asynchronous message brokers with EricksonLopez.Outbox.
In distributed architectures, performing dual writes between a relational database and an external message broker (RabbitMQ, Kafka, Azure Service Bus, AWS SQS) leads to silent inconsistency during network partitions or process crashes.
The Dual-Write Problem
Consider a service handler that commits database state and then publishes an event over the network:
await _database.SaveChangesAsync(ct);await _bus.PublishAsync(new OrderPlacedEvent(order.Id), ct);If the database transaction commits and the process crashes or the broker connection drops before PublishAsync succeeds, downstream consumers never learn that the order was placed. Rolling back the already-committed transaction is impossible.
The Architecture of EricksonLopez.Outbox
The dotnet-outbox family (36 published NuGet packages including EricksonLopez.Outbox and EricksonLopez.Inbox, plus 2 local unpublished packages EricksonLopez.Inbox.PostgreSql and EricksonLopez.Outbox.OpenTelemetry in local v4.2.0) solves dual-write divergence in three stages:
- Atomic Outbox Persistence: The serialized domain event is written to the
outbox_messagestable inside the exact same ACID transaction that persists domain state. - Batched Polling & Dispatch: A background relay worker reads pending rows in batches (using PostgreSQL
UNNESTarray updates inPostgreSqlOutboxRepositoryto mark processed batches atomically) and dispatches them to the configured broker adapter. - Consumer Inbox Deduplication: Downstream handlers record each processed
message_idinsideinbox_messages(EricksonLopez.Inbox/EricksonLopez.Inbox.Abstractions) so at-least-once broker redeliveries execute business effects at most once.
This dual-layer invariant guarantees that business state and asynchronous event publication remain coupled—satisfying Principle 1 (Correctness over convenience).