Ensuring Data Consistency with Distributed Transactions: From Single DB to TCC

This article walks through the evolution of distributed transaction architectures—from simple single-database transactions, through post-commit multi-database approaches and two-phase commit, to TCC and eventual-consistency patterns—illustrating each method with game-style purchase examples, diagrams, and practical implementation tips.

dbaplus Community
dbaplus Community
dbaplus Community
Ensuring Data Consistency with Distributed Transactions: From Single DB to TCC

Overview

In systems that require strong consistency—such as payment, order, or in-game currency purchases—operations must either all succeed or all fail. The article uses a simple scenario where a player transfers 100 RMB to illustrate the need for atomicity across multiple data stores.

Single-Database Transaction

When the gold and item tables reside in the same database, a classic single-database transaction can guarantee consistency. The process consists of two steps: deduct gold and add the item. If any SQL statement fails, rolling back the transaction restores consistency.

Post-Commit Multi-Database Transaction

When the gold and item tables are split across separate databases (vertical sharding), a single transaction cannot span both. Using a post-commit strategy, the system executes SQL on both databases first and commits only after both succeed. If an exception occurs before commit, both transactions are rolled back.

Two-Phase Commit (2PC)

To handle the case where the early commit of one database could cause inconsistency, the two-phase commit separates the process into a prepare phase and a commit phase. All participating databases first prepare; if any prepare fails, all are rolled back. Only when all prepares succeed does the final commit occur.

TCC (Try-Confirm/Cancel)

TCC extends the two-phase idea to service-oriented architectures. Each business operation provides three APIs: Try (reserve resources), Confirm (finalize), and Cancel (release). The article shows how gold deduction and item addition can be implemented with TCC, ensuring consistency even when downstream services fail.

Eventual Consistency

Because strong-consistency protocols can degrade performance under high concurrency, the article introduces eventual consistency: core data is kept strongly consistent, while non-critical data is reconciled asynchronously via message queues or scheduled jobs. Combining real-time queue triggers with periodic compensation offers a balanced solution.

Practical Reference

A minimal XA transaction demo using the Atomikos library is available at github.com/liangyanghe/xa-transaction-demo.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

tcceventual consistencytwo-phase commit
dbaplus Community
Written by

dbaplus Community

Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.