𝗘𝗳𝗳𝗶𝗰𝗶𝗲𝗻𝘁 𝗗𝗮𝘁𝗮 𝗣𝗶𝗽𝗲𝗹𝗶𝗻𝗲𝘀 𝘄𝗶𝘁𝗵 𝘁𝗵𝗲 𝗢𝘂𝘁𝗯𝗼𝘅 𝗣𝗮𝘁𝘁𝗲𝗿𝗻
You need data to move between services in real time. You also need this data to be correct.
Sending messages to a broker during a database transaction is risky. If the broker fails, your data gets out of sync.
The outbox pattern fixes this.
Here is how it works:
- Save your data and the event in one transaction.
- Put the event in a local outbox table.
- A background worker reads this table.
- The worker sends the event to your message broker.
- The worker marks the event as sent.
You also need idempotent consumers. These consumers ignore duplicate messages. This prevents errors when the same event arrives twice.
This setup gives you a reliable system:
- Database and messages stay in sync.
- Services stay separate.
- You have a clear audit trail of events.
Use this for high reliability in distributed systems.