Activate STM example scenario

24
Two concurrent bank transactions. If there is inconsistency, money can appear or money can disappear.

Transcript of Activate STM example scenario

Two concurrent bank transactions. If there is inconsistency, money can appear or money can disappear.

Transactions are stated at the same time.

Transaction 1 (T1) reads the Account1 balance and takes a snapshot. The snapshot is now in the transaction scope.

Transaction 2 (T2) also reads the Account1 balance.

Each transaction now has its isolated snapshot of the Account1 balance.

T1 updates Account1 balance snapshot to 100-50=50

T2 also updates Account1 balance snapshot to 100-50=50

T1 reads the Account2 balance and takes a snapshot.

T2 also reads the Account2 balance and takes a snapshot.

Each transaction now has its isolated snapshot of the Account2 balance.

T1 updates Account2 balance snapshot to 0+50=50

T1 also updates Account2 balance snapshot to 0+50=50

T1 ends first and can commit the Account 1 balance new value.

Commit the Account2 balance.

The STM detects that T2 has reads and writes conflicts with the T1 already commited values.

Assuming that T2 does not have side effects, it can be executed again (retried).

On T2 retry, the T1 is already finished.

T2 reads the Account1 balance and takes a snapshot.

T2 updates its Account1 balance snapshot to 50-50=0

T2 reads the Account2 balance and takes a snapshot.

T2 updates its Account2 balance snapshot to 50+50=100

T2 commit Account1 balance.

T2 commit Account2 balance.

The two transactions end with success. Reads and writes were isolated and the STM detected the conflict, resolving it with the T2 retry.