Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.Original topic: tidb mvcc 读取数据流程的想法
Recently, I have been studying the process of reading data with TiDB MVCC. I have been pondering a question: why is there a read-write conflict during MVCC reads? In this post mvcc和读写冲突 - TiDB 的问答社区, I provided an example.
In the example I gave, “Transaction B should wait for Transaction A to complete before reading the latest data of the row with id=1 (the state at commit.tso 10:59:50, not the state at commit.tso 10:58:00)” because:
Hypothesis 1: I believe it is to prevent phantom reads. Assuming the current isolation level is snapshot isolation (repeatable read), and Transaction B executes two select queries (according to the principles of snapshot isolation, it should only get the same start.tso). If MVCC did not have read-write conflicts, the following situation might occur: Transaction B executes the first query and gets the data at commit.tso=10:58:00, then executes the second query and gets the data at commit.tso=10:59:50 (assuming Transaction A was fully committed at 10:59:00). This could result in the second query’s value being different from the first, causing a phantom read (or non-repeatable read), violating the principles of snapshot isolation (repeatable read). I personally think this is one of the reasons for read-write conflicts under MVCC.
Hypothesis 2: Based on this hypothesis, I have another guess. If the isolation level is read committed, which inherently has phantom reads, then MVCC reads might not have read-write conflicts. Does this mean that in some cases, such as when Transaction A generates a large update transaction and Transaction B wants to read the rows involved in Transaction A, if it’s under snapshot isolation, Transaction B needs to wait (for Transaction A to complete), but under read committed isolation, Transaction B does not need to wait? In such cases, does this imply that the performance of read committed isolation is better than snapshot isolation?
I am not sure if my two hypotheses are correct. Could the experts please provide some guidance?