Context
Some operations need to synchronously write to two or more transactional systems.
Prerequisites
Every system supports preparing a transaction: making it ready to commit, then waiting for an instruction to commit or roll back. For example, PostgreSQL’s PREPARE TRANSACTION, or MySQL’s XA transactions.
Eventual consistency is acceptable. So is reduced availability: every system must be available for a transaction to commit, and if the process coordinating the transaction fails partway through, transactions can be left holding locks until it recovers, blocking other operations.
Significant extra complexity is acceptable.
Example
An e-commerce application stores orders and stock in separate databases, orders and inventory. When an order is placed, it needs to be recorded in orders, and some stock needs to be reserved in inventory.
Problem
How do we perform a transaction across more than one system, such that either all writes get committed or none of them do?
Solution
Coordinate the transaction using the two-phase commit protocol (2PC).
2PC has two roles: a single coordinator, and several participants, each storing some data. In the example, the application is the coordinator, and the orders and inventory databases are the participants.
-
Prepare – The coordinator asks each participant to prepare its transaction: do all the work, up to but not including committing. Each participant replies saying whether it can commit.
-
Commit – If every participant can commit, the coordinator tells them all to commit. Otherwise, it tells them all to roll back.
Handling failures
Once the coordinator has decided to commit or roll back, the decision is final. It must keep sending it to every participant, retrying through failures until each one acknowledges, much like reliable retries. Repeated instructions must be safe. For example, committing a transaction that has already committed should count as success.
To survive crashes, each role must record what it’s doing before acting on it:
- A participant must durably store its prepared transaction, including its locks, before replying that it can commit. After a crash, it restores the transaction and keeps holding the locks.
- The coordinator must durably record its decision, e.g. in a write-ahead log, before telling any participant to commit. After a crash, it reads the log and carries on sending the decision. If it crashed before recording a decision, it rolls back.
A participant that has prepared can’t decide on its own. Rolling back might contradict a commit the coordinator has already sent to others, and committing might contradict a rollback. So while the coordinator is unavailable, prepared transactions wait, holding their locks and blocking other operations that need them.
Fractured reads
2PC makes the writes atomic, but not fully isolated. Each participant only isolates its own transaction, and they don’t all commit at the same instant. In the example, orders might commit before inventory. In between, another operation could see the order but not its stock reservation: a fractured read. This usually lasts only as long as the commit phase, but if the commit can’t reach a participant, e.g. because of a network partition or a crash, it lasts until it does.
Preventing this means readers have to take part in the locking too. They lock everything they read, in every participant, and hold the locks until they’ve finished reading. This is slow and can deadlock across systems. It also doesn’t cover rows that don’t exist yet, such as a new order. Often it’s simpler to accept the inconsistency.
Alternatives
For longer-running transactions, or systems that don’t support preparing transactions, consider using a saga.
Also, consider whether it would be possible to consolidate your data into a single system to make atomic transactions possible. This will likely result in a much simpler system. If the data doesn’t fit in one database, a distributed database (e.g. CockroachDB, Spanner) runs distributed transactions internally, so the application doesn’t have to coordinate them. At serializable isolation, the default in both, they also prevent fractured reads.
See also
- Wikipedia: Distributed transaction
- Wikipedia: Two-phase commit protocol
- The Seven Most Classic Patterns for Distributed Transactions
- Patterns for distributed transactions within a microservices architecture
Related
- ACID transaction – Perform multiple writes, such that either all of them or none of them succeed
- Saga – Perform a series of transactions with backwards recovery