Interview frequency: …
Short answer
It happens when saving to a database and publishing to RabbitMQ/Kafka together, with one of the operations failing; Outbox saves the message in a local table within the same transaction, then publishes it later.
Explanation
A reliable Distributed Transaction between a database and a Message Broker is not available. If the order is saved to SQL but the network fails before the message is sent, the warehouse never learns about it. With Outbox, the message is inserted into the OutboxMessages table in the same database transaction. A background worker (or MassTransit Outbox) reads the table and publishes messages to the broker with at-least-once delivery.
Code example
// Enable MassTransit Transactional Outbox
builder.Services.AddMassTransit(x => {
x.AddEntityFrameworkOutbox<AppDbContext>(o => {
o.UsePostgres();
o.UseBusOutbox();
});
});Common mistakes
- Sending Message Broker messages directly from Controller methods before the database transaction has successfully committed.