Sobes.tech
Middle+

You talked a lot about Inbox pattern. Do you know anything about Outbox?

sobes.tech AI

Answer from AI

Outbox pattern is an architectural pattern that helps ensure reliable and atomic sending of messages or events from microservices or applications.

Pattern essence: Instead of immediately sending an event to a message bus or another service, the application writes the event to a special "outbox" table within the same transaction that modifies the main business data. Then, a separate process or worker reads these events from the table and sends them to the appropriate destination (e.g., Kafka, RabbitMQ).

Advantages:

  • Guarantees that the event will not be lost and will not be sent without the corresponding data change.
  • Ensures atomicity of the data change operation and event publication.
  • Simplifies error handling and retries.

Example:

// In a transaction, record the order and event in outbox
tx, _ := db.Begin()
_, _ = tx.Exec("INSERT INTO orders ...")
_, _ = tx.Exec("INSERT INTO outbox (event_type, payload) VALUES (?, ?)", "OrderCreated", orderPayload)
tx.Commit()

// A separate worker reads from outbox and sends events
for {
  events := fetchOutboxEvents()
  for _, e := range events {
    sendToMessageBus(e)
    markEventAsSent(e)
  }
  time.Sleep(time.Second)
}

Thus, the Outbox pattern helps reliably integrate microservices and systems, avoiding data and message desynchronization.