How to Choose the Transaction Isolation Level in TiDB

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: TiDB的事务隔离级别如何选择

| username: alfred

[TiDB Usage Environment] Production Environment / Testing / PoC
[TiDB Version]
[Encountered Issues]
[Reproduction Path] What operations were performed to encounter the issue
[Issue Phenomenon and Impact]

TiDB supports optimistic transaction model, pessimistic transaction model, SI, REPEATABLE READ, READ COMMITTED. How should one choose in actual use?

[Attachment]

Please provide the version information of each component, such as cdc/tikv, which can be obtained by executing cdc version/tikv-server --version.

| username: xiaohetao | Original post link

I think based on the business scenario and requirements, among the 4 isolation levels, it is generally read committed, right?

| username: 特雷西-迈克-格雷迪 | Original post link

Oracle only has RC, MySQL’s default isolation level is RR, but RDS-MySQL is RC. Depending on the business, the RC level can be lighter.

| username: xfworld | Original post link

Most will choose pessimistic, and then READ COMMITTED is almost enough.

| username: 张雨齐0720 | Original post link

In financial scenarios, pessimistic locking is used, RC+ select for update.

| username: Raymond | Original post link

I recommend using RC. In TiDB, to prevent phantom reads under the RR isolation level, there will be read-write conflicts, which can be quite costly.

| username: cs58_dba | Original post link

If you want compatibility with MySQL, choose RC.

| username: Hacker007 | Original post link

For general development, the default settings are fine. Higher isolation levels can easily lead to table locking, causing some operations to not function properly.

| username: 特雷西-迈克-格雷迪 | Original post link

How is the RR level of TiDB implemented? I need to research it when I have time.

| username: alfred | Original post link

Yes, it is important to choose the appropriate isolation level.

| username: 人如其名 | Original post link

You can refer to these two articles. The snapshot read isolation level mainly addresses the phantom read problem, which requires waiting for locks during the prewrite phase of other transactions. If the other transaction is a large one, it may lead to significant lock conflicts. This is an issue with snapshot reads, but the advantage is that it only needs to obtain a timestamp at the beginning of the transaction. The RC isolation level does not have such read-write conflicts, but each statement within the transaction needs to obtain a timestamp, which incurs a cost in terms of timestamps. However, considering everything, a pessimistic lock + RC isolation level might be more effective.

For the implementation and principles of snapshot reads, you can refer to the following two articles:

| username: gary | Original post link

Choose different isolation levels for different scenarios, commonly using the RC isolation level.

| username: zhouzeru | Original post link

If there are no other requirements, then RC.

| username: buddyyuan | Original post link

It is recommended to choose RC isolation level + pessimistic transactions.

| username: alfred | Original post link

TiDB defaults to SI + pessimistic transactions, right?

| username: zhanggame1 | Original post link

By default, it is SI + pessimistic transactions. SI is actually RR, but RR doesn’t seem as reliable as RC.

| username: linnana | Original post link

The financial industry generally uses RC + pessimistic locking.

| username: redgame | Original post link

RC + Pessimistic Lock

| username: Anna | Original post link

Oracle only has RC, MySQL’s default isolation level is RR, but RDS-MySQL is RC. Depending on the business, the RC level can be lighter.