# The transactional outbox and inbox, explained with Postgres

> How Mocha and Postgres keep messages from getting lost and prevent duplicate stock reservations when things go wrong.

Canonical source: https://chillicream.com/blog/2026-12-03-transactional-outbox-and-inbox-with-postgres

![](https://chillicream.com/images/blog/2026-12-03-transactional-outbox-and-inbox-with-postgres/header.png)

[![Pascal Senn's avatar](https://chillicream.com/_optimized/images/remote/3ebc6212aa4091005799d46f902a92e2eb7e77cd966797ecb5f4eaeab3bb26fe.png)Pascal Senn](https://chillicream.com/authors/pascal-senn)2026-12-034 min read

[Share this post on X](https://x.com/intent/tweet?url=https%3A%2F%2Fchillicream.com%2Fblog%2F2026-12-03-transactional-outbox-and-inbox-with-postgres&text=The+transactional+outbox+and+inbox%2C+explained+with+Postgres) [Share this post on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fchillicream.com%2Fblog%2F2026-12-03-transactional-outbox-and-inbox-with-postgres)

- [messaging](https://chillicream.com/blog/tags/messaging)
- [outbox](https://chillicream.com/blog/tags/outbox)
- [inbox](https://chillicream.com/blog/tags/inbox)
- [postgres](https://chillicream.com/blog/tags/postgres)
- [mocha](https://chillicream.com/blog/tags/mocha)

Saving data and sending a message are two separate operations. In a distributed system, we need to account for what happens when one succeeds and the other doesn't.

In the [previous post](https://chillicream.com/blog/2026-11-29-exactly-once-message-processing), we explored exactly-once message processing using a simple checkout scenario. A customer orders a lamp, checkout creates an order, and inventory reserves the stock.

Now let's look at how we can implement this with Mocha, Postgres, and RabbitMQ, starting with the checkout service.

## The order exists, but the command is lost

Checkout saves the order to Postgres and sends a `ReserveStock` command to inventory.

C#

```
db.Orders.Add(order);
await db.SaveChangesAsync(cancellationToken);
await bus.SendAsync(command, cancellationToken);
```

There is a small but important gap between `SaveChangesAsync` and `SendAsync`. If the process crashes during that gap, we've saved the order but lost the command.

Sending the command before saving the order doesn't really help either. It just gives us the opposite problem.

Try sending the command first. Inventory can end up reserving stock for an order that was never saved.

## Saving the order and the command together

Instead of sending the command directly to RabbitMQ, we can store it in Postgres alongside the order. That way, either both are saved or neither is. This is the idea behind the transactional outbox. To set it up in Mocha, add `AddPostgresOutbox()` to the order model, apply a migration, and register the context with `UsePostgresOutbox()`. There are two ways we can use it, depending on how we manage our transactions.

[Outbox setup](https://chillicream.com/docs/mocha/reliability#guarantee-delivery-with-the-transactional-outbox)

If we're already using EF Core's unit of work, we can stage the command on the same context as the order.

C#

```
db.Orders.Add(order);
await db.Outbox(bus).SendAsync(command, cancellationToken);
await db.SaveChangesAsync(cancellationToken);
```

When we call `SaveChangesAsync`, EF Core saves both the order and the outbox message in the same transaction.

Once the transaction commits, a background worker picks up the message from the outbox, sends it to RabbitMQ, and waits for a [publisher confirm](https://chillicream.com/docs/mocha/transports/rabbitmq#publisher-confirms) before deleting it. Now, even if checkout crashes right after saving the order, the command is still in Postgres, waiting to be sent. There's another interesting failure case, though. What if RabbitMQ receives the message, but the worker crashes before removing it from the outbox? When the worker restarts, it sends the same message again. We've solved the problem of losing messages, but we can still end up delivering them more than once.

## Handling duplicate messages with the inbox

Let's follow our command into the inventory service. Inventory receives `ReserveStock`, reserves a lamp, and commits the transaction. Before it can acknowledge the message, the connection to RabbitMQ drops. RabbitMQ doesn't know that inventory has already processed the command, so it requeues it for redelivery. At this point, we already have a reservation and nine lamps left in stock. Processing the command again could leave us with two reservations and eight lamps. This is where the transactional inbox comes in. On the inventory context, add `modelBuilder.AddPostgresInbox()` and apply a migration. Then register `UseTransaction()` followed by `UsePostgresInbox()`.

[Inbox setup](https://chillicream.com/docs/mocha/reliability#deduplicate-messages-with-the-transactional-inbox)

Before the handler runs, the inbox tries to insert a record using the message ID and consumer type as its key. That record is saved in the same transaction as the reservation and the stock update. If the transaction rolls back, the inbox record rolls back with it, and the next delivery can try again. If the transaction commits, the inbox record stays. When the same message arrives again, Mocha finds the existing record and skips the handler. We're still receiving the message more than once, but we're only applying its database changes once.

Switch between a dropped connection after the commit and a crash before it to see whether the inbox record survives.

There are two things to keep in mind here. First, inbox records aren't necessarily kept forever. Configure [inbox retention](https://chillicream.com/docs/mocha/reliability#configure-inbox-retention) based on how far back you expect messages to be replayed. Second, two messages with different message IDs can still refer to the same reservation. The inbox won't catch that, so it's worth enforcing a unique reservation ID in the database as part of the same transaction that updates stock.

## Try it yourself

We've put together a runnable example with Postgres, RabbitMQ, and both checkout patterns. Everything runs with Docker Compose. You can crash checkout after saving the order and use `status` to see that the command is still waiting in the outbox. You can also simulate a crash in inventory after committing the reservation but before acknowledging the message. After restarting the services, you should end up with exactly one reservation and nine lamps in stock.

[Run the sample in platform-examples](https://github.com/ChilliCream/platform-examples/tree/main/examples/Mocha/TransactionalOutbox)

You can also play through the different failure scenarios right here:

Choose a crash point and toggle the outbox or inbox to see how each affects recovery.

## You might also like

[View all](https://chillicream.com/blog)
