Senior
What is the Outbox pattern? How is it implemented?
sobes.tech AI
Answer from AI
The Outbox pattern is an architectural approach for reliable message delivery between services or components, especially in distributed systems. It addresses the issue of consistency between data recording in the database and message (e.g., event) sending to a queue.
Idea:
- Instead of sending a message directly at the moment of data change, the service records an event in a special "outbox" table within the same transaction as the main data change.
- A separate process or service periodically reads this table and sends messages to a broker (Kafka, RabbitMQ, etc.).
- After successful sending, the message is marked as sent or removed from the table.
This achieves atomicity: either both data and event are recorded together, or not recorded at all, preventing desynchronization.
Implementation:
- Add an
outboxtable to the database with fields: id, payload (event), status, timestamp. - In the business logic code, within a single transaction:
- Update the main data
- Record the event in
outbox
- Run a separate worker that:
- Periodically selects unsent events from
outbox - Sends them to the messaging system
- Updates the status of events in
outbox
- Periodically selects unsent events from
Example of an outbox table:
CREATE TABLE outbox (
id UUID PRIMARY KEY,
event_type VARCHAR(255),
payload TEXT,
status VARCHAR(20),
created_at TIMESTAMP
);
The Outbox pattern increases integration reliability and simplifies error handling in distributed systems.