Selecting a Distributed Transaction Mode
DDM supports three distributed transaction modes, including single-shard, best-effort commit, and eventual atomicity.
Table 1 describes their features.
| Transaction Mode | Advantage | Disadvantage |
|---|---|---|
| Single-shard | The execution efficiency is high, and the underlying database ensures strong consistency. | If a transaction involves multiple shards, DDM refuses to execute the transaction and returns an error. |
| Best-effort commit | Cross-shard transactions are supported. Their execution results are independent and do not interfere with each other. | The execution efficiency is lower than that of the single-shard mode. If transaction commit fails for any shard, no compensating transaction is provided. In this case, transaction execution of some shards fails while that of other shards succeeds. |
| Eventual atomicity | Cross-shard transactions are supported, and the transaction execution results are eventually consistent for all shards. | The execution efficiency is lower than that of the best-effort commit mode. If transaction commit fails for any shard, a compensating transaction is provided. During the compensation, the result queried from the client is not the latest, which means that intermediate states exist. |
- For DDM, only one transaction mode can be selected for one schema. When creating a schema, plan the data structure for storage and required SQL statements, for example, determine whether cross-shard deployment is implemented and whether transaction consistency must be ensured. Select an appropriate distributed transaction mode.
- Currently, you can only customize hints for SELECT or UPDATE statements.
- In final atomicity mode, do not delete any fields in tables or add fields. If no exception occurs in the current transaction or the compensation is complete, you can add a field at the end of the table.
Single-Shard
Overview
A transaction can be executed only on one shard. If a transaction involves multiple shards, DDM refuses to execute the transaction and returns an error. Figure 1 illustrates the principle.
Application Scenario
This mode is suitable when service splitting is reasonable, a complete transaction processing framework is available at the application layer, and transactions are to be executed on only one shard. The underlying database ensures strong consistency of single-shard transactions. In this mode, DDM refuses to execute cross-shard transactions and returns an error so that expected results are achieved.
Best-Effort Commit
Overview
Transactions are independently committed on each shard. Transaction commit results remain consistent on all shards to the greatest extent possible, but it is likely that some shards succeed while others fail. Figure 2 illustrates the principle.
Application Scenario
This mode is suitable for most services that do not involve monetary transactions. It achieves a proper tradeoff between performance and consistency. The transaction commit command is sent to multiple nodes, and there is a small chance that some nodes succeed while others fail in the event of a commit time window exception.
Eventual Atomicity
Overview
Transaction commit results do not always remain consistent on all shards. If transaction commit fails for any shard, DDM enables the compensation mechanism on other shards with successful commit results to undo previous modifications. In this way, the transaction states of all shards remain eventually consistent.
In eventual consistency scenarios, if there are concurrent query requests, the query results may not reflect the final states, but intermediate states. For example, the commit operation may be completed on some shards while being executed on other shards.
Application Scenario
This mode is suitable for scenarios with high consistency requirements. It solves the issue that transaction commit succeeds on some nodes while fails on others under the best-effort commit mode. If high consistency requirements are placed on some read SQL statements, you can use select for update or lock in share mode to avoid reading inconsistent states (coexistent failed and successful states).
For example,
select col1, col2,...coln from table1 where col1={sharding key} for update;
select col1, col2,...coln from table1 where col1={sharding key} lock in share mode;
You are advised to specify a sharding key in the WHERE condition.
Last Article: Using Global and Unsharded Tables
Next Article: Migrating the Entire MyCat Database to DDM


Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.