๐๐ณ๐ณ๐ถ๐ฐ๐ถ๐ฒ๐ป๐ ๐๐ฎ๐๐ฎ ๐ฃ๐ถ๐ฝ๐ฒ๐น๐ถ๐ป๐ฒ๐ ๐๐ถ๐๐ต ๐๐ต๐ฒ ๐ข๐๐๐ฏ๐ผ๐ ๐ฃ๐ฎ๐๐๐ฒ๐ฟ๐ป
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.