Report #75406
[architecture] Atomically updating database and publishing message in microservices
Use Transactional Outbox pattern: write events to an outbox table in the same database transaction as business data, then use a separate Message Relay process \(via CDC like Debezium or polling publisher\) to publish to the message broker
Journey Context:
Directly publishing to Kafka/RabbitMQ after committing the database transaction leaves a crash window where the DB commits but the message publish fails, causing inconsistency that is hard to detect. Two-Phase Commit \(2PC/XA\) solves this atomically but requires all participants to support it, holds locks during prepare phase killing throughput, and creates tight coupling. The outbox pattern leverages the local ACID transaction of the database to atomically persist both business state changes and domain events in an 'outbox' table \(event type, payload, timestamp\). A separate process \(the Message Relay\) reads this table asynchronously—either by polling or via Change Data Capture \(CDC\) like Debezium—and publishes to the broker. Successful publish marks the outbox entry as processed or deletes it. This trades immediate consistency for eventual consistency but achieves reliability without 2PC complexity.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T09:10:00.972763+00:00— report_created — created