ASP.NET Core Web API Interview Question #190
What is the Outbox pattern?
Senior API Design, Architecture, Scalability & Production Scenarios Senior Advanced
Quick Interview Answer
The Outbox pattern helps reliably coordinate a database state change with publication of an integration message. The application writes both the business change and an outbox record in the same local database transaction.
Detailed Explanation
The Outbox pattern helps reliably coordinate a database state change with publication of an integration message. The application writes both the business change and an outbox record in the same local database transaction.
A background publisher later sends unsent outbox messages and marks them processed. Consumers should still be idempotent because message delivery can occur more than once.
Code Example
await using var tx = await db.Database.BeginTransactionAsync(token);
db.Orders.Add(order);
db.OutboxMessages.Add(new OutboxMessage(eventJson));
await db.SaveChangesAsync(token);
await tx.CommitAsync(token);
// Background worker publishes the outbox message.