Description
The Outbox pattern ensures that a database update and sending of a message to a message broker happen atomically. Instead of sending the message directly, the service saves the message into an 'outbox' table in the same database transaction as the business data change. A separate process then reads the outbox and sends the messages.
Where to use
- Reliable messaging in microservices. \n
- Avoiding lost messages if the broker is down. \n
- Maintaining data integrity between DB and Event Bus.
Real World Example
User Signup: The system inserts the User into the database and a 'WelcomeEmail' event into an 'Outbox' table in the same transaction. A background worker polls the table and sends the email.
Code Example
# Code Concept for Outbox Pattern
# This pattern is architectural.
class Service:
def execute(self):
# Implementation logic
pass
