Transactional outbox
Definition
The transactional outbox pattern stores messages to be published in a table inside the same local transaction as the state change; a relay process later reads the table and publishes the messages to a broker.
In practice
Updating a database and publishing to a broker are two separate systems, so doing both directly can lose the event (crash after commit) or publish an event for a change that rolled back. The outbox makes the two atomic by turning the publish into a database write.
The relay delivers at least once, so consumers must be idempotent. The outbox table needs cleanup, and the relay adds a small publishing delay; change data capture tools can replace polling for lower latency.
Example
The orders service inserts the order and an OrderPlaced row into outbox in one transaction; a background worker publishes unsent rows every second and marks them sent.