Transactional Outbox, Inbox & Idempotency
Distributed systems fail across network boundaries: a database commit can succeed before a message broker call fails, or a client retry can deliver the same command twice. The EricksonLopez.Outbox, EricksonLopez.Inbox, and EricksonLopez.Idempotency libraries enforce end-to-end delivery and replay invariants.
dotnet-outboxrepository hosts bothEricksonLopez.Outbox(3.0.0, published on NuGet) andEricksonLopez.Inbox/EricksonLopez.Inbox.Abstractions(3.0.0, published on NuGet). Note thatEricksonLopez.Inbox.PostgreSqlcurrently exists only in the local workspace (Unpublished).dotnet-idempotencyrepository publishesEricksonLopez.IdempotencyandEricksonLopez.Idempotency.Abstractions(2.0.0on NuGet), plus PostgreSQL/Redis/EF Core/AspNetCore adapters.
1. Storing Outbox Messages Atomically (IOutbox)
Section titled “1. Storing Outbox Messages Atomically (IOutbox)”DefaultOutbox serializes payloads with ArrayPoolBufferWriter<byte> and persists OutboxMessage records inside the caller’s IOutboxTransactionContext, guaranteeing that business state and outgoing events commit or roll back together:
using System;using System.Threading;using System.Threading.Tasks;using EricksonLopez.Outbox;using EricksonLopez.Outbox.Persistence;
namespace Example.Application.Orders;
public sealed record OrderConfirmedMessage(Guid OrderId, DateTimeOffset ConfirmedAt);
public sealed class OrderConfirmationService(IOutbox outbox){ public ValueTask PersistConfirmationEventAsync( Guid orderId, IOutboxTransactionContext transaction, CancellationToken cancellationToken) { var message = new OrderConfirmedMessage(orderId, DateTimeOffset.UtcNow); return outbox.StoreAsync(message, transaction, cancellationToken); }}2. Deduplicating Consumers (EricksonLopez.Inbox)
Section titled “2. Deduplicating Consumers (EricksonLopez.Inbox)”Because outbox dispatchers guarantee at-least-once delivery, downstream consumers use IIdempotencyChecker and IInboxStore (EricksonLopez.Inbox.Abstractions) so duplicate message deliveries do not re-apply side effects.
3. Deterministic Command Replay (IdempotencyEngine)
Section titled “3. Deterministic Command Replay (IdempotencyEngine)”For HTTP/API and mediator commands, IdempotencyEngine (EricksonLopez.Idempotency) uses atomic ClaimResultStatus leases (IIdempotencyStore) and request payload fingerprinting (IdempotencyFingerprintHasher) to:
- Execute the handler once when the idempotency key is first claimed.
- Return the cached
ExecutionResponseon safe retries with identical payload fingerprints. - Reject conflicting payload reuse with
IdempotencyConflictException/IdempotencyProblemDetails.