Image by Andreas Munich from Pixabay

Transactional Outbox

I was reading the “Microservices Patterns” book by Chris Richardson and came across this pattern called “Transactional Outbox”, described also on the author’s website. It allows you to solve a case when you want to write a record to the database and send a related message to the consumers via a message broker, while keeping those two actions transactional.

The problem this pattern tries to solve is as follows. Suppose you persist an Order entity in your order-service microservice, and then send an OrderCreated message to another microservice (execution-service) that has to execute this Order. If these two actions are not executed in an atomic transaction, there’s a possibility that you save an Order to the database but fail to send it if the message broker is down.

If you first send the OrderCreated message and then save the Order to the database, it could happen that the database will be down or fails to accept the Order due to some constraint violation. Then the Order will not be saved, but its processing will be started, since we’ve already sent OrderCreated message to the broker.

We can use a distributed transaction (two-phase commit) to solve this. It would guarantee exactly-once delivery by joining multiple different resources (database and message broker) in a single atomic transaction. But this technique is heavy, and doesn’t work well in a high load and high throughput application.

So the author proposes to use the Transactional Outbox pattern, described as follows:

  • order-service saves the Order to the database;
  • in the same transaction, order-service saves the OrderCreated message related to this Order into another table of the same database (OUTBOX); since transaction is the same, these two inserts constitute an atomic action;
  • the message-relay (working in a different transaction) reads the OrderCreated message from the OUTBOX table and sends it to the message broker;
  • the message-relay removes the record from the OUTBOX table.

It’s easy to see that this approach has at-least-one guarantee: if we fail to remove the message from the OUTBOX table due to the failure of the database or the message-relay, then it will be sent once more after the issue is fixed.

Such guarantee may be enough for our needs, especially if the execution-service is idempotent or is able to handle duplicate messages. But this approach with an additional table seemed too complicated to me, so I thought on how to simplify it.

Another issue with this approach is that we’re completely blind on the order-service side if we’ve actually ever been at risk of sending any particular message more than once — we only can spot this on the execution-service side.

A Simplified Approach

I think we can somewhat simplify this pattern, at least when we use a database that supports sequences, by adding just two database sequences, I’ll call them seq_message_start and seq_message_end. Here’s the process:

  • order-service saves the Order to the database; the only requirement to the order table is that it should have an incrementing numeric identifier;
  • message-relay checks if seq_message_start and seq_message_end sequences have the same values; if they do, it increments the seq_message_start sequence;
  • message-relay uses the seq_message_start sequence value as an id to select the Order, and sends it via message broker;
  • message-relay increments the seq_message_end sequence.

It’s easy to see that the only scenario for inconsistency in this approach is when the message-relay has already sent the message, but can’t bump the seq_message_end sequence for some reason. Then it will try to send the message again some other time — and this is where we could get the more-than-once delivery case.

It seems to me that this approach is a lot easier and more performant than creating a whole new table for this purpose and doing this insert-delete cycle. At the same time, in case of potential message duplication, we’d be fully aware of the risk (since we could log a warning that the sequences are different).