Integrating RabbitMQ with database transactions
Codemia Knowledge Hub ·
RabbitMQ does not support standard two-phase commit, so you cannot wrap a database write and a publish in one atomic transaction. To keep them consistent you choose a pattern: best-effort single-phase commit, reading events from the database transaction log (change data capture), or an outbox table written inside the database transaction.
The dual write problem
RabbitMQ is an open-source message broker used for asynchronous communication and for decoupling services in a microservices architecture. A database transaction is a group of operations that must all succeed or all fail (atomicity). Combining RabbitMQ messaging with database atomicity is known as the dual write problem: if one write succeeds and the other fails, the two systems disagree.
Strategies for keeping them consistent
- Two-phase commit (2PC): a distributed protocol where every participant (for example a database and a message queue) commits fully or not at all. RabbitMQ does not support 2PC in its standard form.
- Database transaction log as the event source: messages to RabbitMQ are derived from the database's transaction log, so they always match database state. This is called transaction log mining or change data capture (CDC).
- Best-effort 1PC: run a local database transaction and a local RabbitMQ operation as if they were one single-phase commit. It is simpler and common, but inconsistent if one side succeeds and the other fails.
| Strategy | Pros | Cons |
| Two-Phase Commit (2PC) | Ensures data consistency across systems. | Complex, not natively supported by RabbitMQ. |
| Database transaction log mining | Reliable consistency. Automates the process. | Requires additional tools and setup. |
| Best Effort 1PC | Simpler and commonly used. | Possible data inconsistencies. |
Example: account creation with a welcome message
A financial application creates an account and queues a welcome message. Both the account row and the message must be saved, or neither, so you never get an account without a welcome message or the reverse.
- Begin a database transaction and insert the account details.
- Publish to RabbitMQ after the insert succeeds.
- Commit or roll back: commit if the publish succeeded, roll back the database transaction if it failed.
This is the best-effort 1PC pattern, so it still carries that pattern's risk of inconsistency.
Failure handling and monitoring
Design for failure: retries and compensating transactions reduce the chance of inconsistent data. Thorough monitoring and logging help you detect discrepancies between database transactions and message operations.
Practices that improve reliability
- Atomic operations: keep database and broker operations atomic where feasible.
- Error handling: implement robust error handling and rollback.
- Synchronization: use synchronization or locking if needed so database operations and message sending are thread-safe.
- Testing: test the integration against many failure scenarios.
- Outbox and CDC tooling: with the outbox pattern, state changes and outgoing messages are stored in an outbox table during the database transaction. Transaction log mining tools such as Debezium further improve resilience.
Related reading
- Integration Testing with Bunny Gem and RabbitMQ
- Integration tests of a polyglot stack (Java/MongoDB/RabbitMQ...)
- Interact with celery ongoing task
- Interact with kafka docker container from outside of docker host
- Interpreting missing slot information in redis cluster nodes command
- Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?
- Interactive admin shell for Apache Kafka
- Interoperating with Django/Celery From Java
System design interview problems on this topic
- Design a Distributed Messaging System: solution and walkthrough
- Design a Log Collection and Analysis System: solution and walkthrough
- Design an Ad Click Aggregation System: solution and walkthrough
- Design a Webhook Notification Service: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.