Mastering Distributed Transactions: 2PC, 3PC, TCC, and Message‑Queue Strategies
This article explains the fundamentals of distributed transactions, illustrates why they are essential for multi‑service operations such as e‑commerce order processing, and compares four major solutions—two‑phase commit, three‑phase commit, TCC, and message‑queue based eventual consistency—detailing their workflows, advantages, and drawbacks.
Distributed Transactions Overview
In a distributed system a transaction may involve several independent databases or services. All participating operations must either commit together or abort together, otherwise data inconsistency occurs. Example: an e‑commerce order creation that touches an order service, an inventory service and a payment service. If the order record is persisted but the inventory deduction fails, the system ends up in an invalid state, which a distributed transaction prevents.
Order Service – creates order record.
Inventory Service – decrements stock.
Payment Service – processes payment.
Two‑Phase Commit (2PC)
2PC introduces a dedicated coordinator that orchestrates the participants.
Prepare phase : the coordinator sends a prepare request to every participant. Each participant performs all local checks, writes a tentative log entry, and replies “ready” or “abort”.
Commit phase : if all replies are “ready”, the coordinator broadcasts a commit command; otherwise it sends abort. Participants then finalize or roll back based on the command.
Advantages: provides strong consistency and atomicity across nodes.
Drawbacks:
Synchronous blocking – participants wait for the coordinator throughout both phases.
Single point of failure – if the coordinator crashes during the commit phase, some participants may have already committed while others remain pending, leading to inconsistency.
Performance overhead due to two round‑trips and persistent logging.
Three‑Phase Commit (3PC)
3PC adds an extra “CanCommit” phase and timeout mechanisms to reduce blocking.
CanCommit phase : coordinator asks participants whether they can execute the transaction. Participants only respond with a yes/no, without performing any work.
PreCommit phase : after receiving unanimous “yes”, the coordinator sends a pre‑commit request. Participants write a prepare log and acknowledge.
DoCommit phase : coordinator issues a final commit or abort based on acknowledgments.
Advantages: eliminates the indefinite blocking of 2PC by allowing participants to abort after a timeout, and reduces the impact of coordinator failure.
Drawbacks: still cannot guarantee absolute consistency under network partitions, and the protocol is more complex and incurs additional message overhead.
Try‑Confirm‑Cancel (TCC)
TCC is a business‑level pattern that splits a transaction into three explicit operations.
Try : perform all necessary validation and reserve required resources (e.g., lock inventory, pre‑authorize payment) without making permanent changes.
Confirm : after all services have successfully completed their Try steps, each service executes the actual business logic using the reserved resources.
Cancel : if any Try step fails, all previously reserved resources are released.
Advantages: achieves eventual consistency at the application layer, offers good performance for long‑running transactions, and avoids distributed locks.
Drawbacks: high intrusiveness – developers must implement three separate interfaces for every business operation, increasing development effort and coupling.
Message‑Queue Based Eventual Consistency
This approach decouples services by using asynchronous messages.
The originating service completes its local transaction and publishes an event message.
If message publishing fails, the local transaction is rolled back.
Consumer services receive the message, execute their local operation, and retry on failure until successful (idempotent processing is required).
Advantages: loose coupling, high throughput, and low latency for the initiator.
Drawbacks: only eventual consistency is guaranteed; not suitable for strict real‑time requirements. Reliability depends on durable message delivery and idempotent consumption handling.
Choosing a Solution
The appropriate distributed‑transaction mechanism depends on the trade‑off between consistency, availability, latency, and implementation complexity. Strong consistency needs (e.g., financial transfers) often favor 2PC or 3PC, whereas high‑throughput, loosely coupled systems may adopt TCC or message‑queue based eventual consistency.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect Chen
Sharing over a decade of architecture experience from Baidu, Alibaba, and Tencent.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
