Updating multiple databases in distributed transaction
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Understanding Distributed Transactions
A distributed transaction is a sequence of operations carried out on two or more networked databases, which must be committed in a coordinated manner to maintain data consistency and integrity. This process guarantees that either all operations in the transaction are committed, ensuring the entire transaction is successful, or none are, in which case the system must revert to the state before the transaction was initiated.
Key Concepts
- Atomicity: This principle ensures that a transaction is treated as a single unit, which either succeeds completely or fails entirely.
- Consistency: Transactions must transition the system from one consistent state to another. No transaction should violate database constraints.
- Isolation: Modifications made by a concurrent transaction should not be visible to others until they are committed.
- Durability: Once a transaction has been committed, it must remain so, even in the event of a system failure.
Mechanisms for Distributed Transactions
Two-Phase Commit Protocol (2PC)
The Two-Phase Commit Protocol is a classic algorithm in distributed systems to ensure all-or-nothing transaction safety across multiple data stores. This protocol involves two phases:
- Prepare Phase: The coordinator node proposes a transaction. All participant nodes prepare the transaction and either promise to commit (vote "yes") or abort (vote "no").
- Commit Phase: Depending on the votes, if all participants agree (i.e., vote "yes"), the coordinator instructs them to commit. If any node votes "no," all nodes are instructed to roll back.
Three-Phase Commit Protocol (3PC)
An improvement over 2PC, the Three-Phase Commit Protocol adds a pre-commit state to reduce the likelihood of a transaction being locked in the commit state if the coordinator fails.
Challenges in Distributed Transactions
- Network Issues: Latencies or failures can affect coordination.
- Performance Overhead: Coordination and logging can affect performance.
- Maintaining Isolation Levels: Higher isolation levels can lead to higher contention and thus lower performance.
Examples of Distributed Transaction Implementations
Example with SQL Databases
Consider updating two SQL databases, for instance, one database serving customer information and another handling orders. Both need to be updated in a single go when a new order is placed.
Technologies Supporting Distributed Transactions
- Microsoft Distributed Transaction Coordinator (MSDTC): Facilitates transactions across multiple Windows-based systems.
- XA Transactions: An X/Open standard that enables a global transaction identifier to be used for coordinating transactions across multiple resources.
- Two-Phase Commit Extensions in DBMSs: Many relational databases, including Oracle, PostgreSQL, and Microsoft SQL Server, have built-in support for 2PC.
Summary Table: Distributed Transaction Key Points
| Principle | Importance in Distributed Transactions |
| Atomicity | Ensures all parts of the transaction either complete fully or not at all. |
| Consistency | Prevents database corruption by ensuring that all transaction operations comply with all rules and constraints. |
| Isolation | Keeps transactions independent of each other, preventing them from interfering. |
| Durability | Ensures that once a transaction is committed, it remains so even after a system failure. |
Conclusion
Updating multiple databases in a distributed transaction setup is complex but essential for maintaining strong data integrity and consistency across potentially disparate systems. Through protocols like 2PC and technologies such as XA Transactions, this essential function can be efficiently managed. However, enterprises need to measure the overhead and consider using more modern, possibly non-traditional approaches such as eventual consistency or microservices-based architectures, especially when operating in environments that require high scalability and availability.
Related reading
- Updating MySQL primary key
- Use MassTransit transactional outbox to update db and send multiple (batched) mails
- Use Prometheus operator with DB volume for k8s
- uses for mongodb ObjectId creation time
- Upgrade and Failure Domains in Kubernetes
- UPGRADE FAILED another operation install/upgrade/rollback is in progress
- Upload a InputStream to AWS s3 asynchronously non-blocking using AWS SDK for Java, version 2
- Usage of ConfigureAwait in .NET

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.