# Outbox Pattern

> A design pattern that writes an outgoing message in the same local transaction as the state change, and a relay publishes it afterwards.

Record: `architecture:outbox-pattern`
Kind: pattern
Layer: [Execution Core](https://banes-lab.com/records/layer/execution-core.md)
Severity: recommended
Scope: persistence, messaging
Canonical: https://banes-lab.com/ontology#architecture-outbox-pattern

Listed in [Architecture principles](https://banes-lab.com/api/records/architecture.md), after [Saga Pattern](https://banes-lab.com/records/architecture/saga-pattern.md) and before [Compensating Transaction](https://banes-lab.com/records/architecture/compensating-transaction.md).

## Repair

- Refactored by: Add Outbox Table, Add Relay Worker
- Detected by: dual-write patterns
- Violated by: database write followed by direct publish without atomicity
- Measured by: lost-message rate, outbox coverage
- Enforced by: persistence rules, integration tests

## Requires

- [Local Transaction](https://banes-lab.com/records/lexicon/local-transaction.md)
- [Message Relay](https://banes-lab.com/records/lexicon/message-relay.md)

## Reinforces

- [Event Reliability](https://banes-lab.com/records/lexicon/event-reliability.md)

## Enables

- [Atomic State Change + Message Publish](https://banes-lab.com/records/lexicon/atomic-state-change-message-publish.md)

## Conflicts with

- [Dual Write](https://banes-lab.com/records/architecture/dual-write.md)

## In tension with

- [Relay Complexity](https://banes-lab.com/records/lexicon/relay-complexity.md)

## Tensions

- [Outbox Pattern / Relay Complexity](https://banes-lab.com/records/tension/outbox-pattern-relay-complexity.md)

## Severity

- [recommended](https://banes-lab.com/records/vocabulary/severity-recommended.md)

## Category

- [Event / Messaging / Asynchronous Architecture](https://banes-lab.com/records/architecture-category/event-messaging-asynchronous-architecture.md)

## Linked from

- [Anti-patterns](https://banes-lab.com/ontology/principles/architecture-category-anti-patterns.md)
- [Event Messaging Async](https://banes-lab.com/ontology/lexicon/lexicon-category-event-messaging-async.md)
- [Severity levels](https://banes-lab.com/ontology/schema/the-vocabulary-severity.md)
- [The resolutions](https://banes-lab.com/ontology/schema/the-resolutions.md)
