Sobes.tech
Senior

Have you used Inbox and Outbox patterns?

sobes.tech AI

Answer from AI

The Inbox and Outbox patterns are used to ensure reliable asynchronous integration between services, especially in a microservices architecture.

Inbox pattern is a message receiving mechanism. The service saves incoming messages in a local table (Inbox), checks their uniqueness, and processes them, guaranteeing idempotency.

Outbox pattern is a message sending mechanism. When the state changes in the service, a message is recorded in the Outbox table within the same transaction as the business operation. Then, a separate process reads the Outbox and sends messages to a queue or another service.

This helps avoid issues with message loss and data desynchronization during failures.

In Go, these patterns can be implemented using database transactions and separate goroutines for processing the Outbox.

Example of a simplified Outbox:

func SaveOrder(ctx context.Context, order Order) error {
    tx, err := db.BeginTx(ctx, nil)
    if err != nil {
        return err
    }

    // Save the order
    if err := saveOrderTx(tx, order); err != nil {
        tx.Rollback()
        return err
    }

    // Record event in Outbox
    if err := saveOutboxEventTx(tx, order.Event); err != nil {
        tx.Rollback()
        return err
    }

    return tx.Commit()
}

Then, a separate worker reads the Outbox and sends events to the message broker.