Sobes.tech
Senior

Outbox pattern: how to ensure atomicity of database write and event sending to the queue?

sobes.tech AI

Answer from AI

The Outbox pattern is used to ensure atomicity between writing data to the database and sending an event to a message queue, to avoid desynchronization.

How it works:

  1. Instead of immediately sending the event to the queue, the application writes the event to a special "outbox" table within the same transaction that modifies the main business data.
  2. After the transaction commits successfully, a separate process (e.g., a worker) reads events from the outbox table and sends them to the message queue.
  3. After successful sending, the event is marked as processed or removed from the table.

Thus, writing to the database and saving the event occur atomically — either both happen or neither.

Example in Java using Spring and transactions:

@Transactional
public void saveEntityAndOutboxEvent(Entity entity, OutboxEvent event) {
    entityRepository.save(entity); // save business data
    outboxRepository.save(event); // save event to outbox
}

A separate service periodically reads events from the outbox and publishes them to the queue.

This guarantees that the event will not be lost and will not be sent without the corresponding changes in the database.