MongoDB transactions and Read/Write Concerns

Hello everyone,

we are working on a university project about transactions in MongoDB.
In particular, we are carrying out some experiments about how the transactions behave in situations like phantom reads, write skew, non-repeatable reads and so on.

We are trying different levels of write and read concern.

It’s not clear to us why, even with a minimum level of read concern, we can’t read data that are modified and committed by concurrent transactions.

For example, we have two transactions, T1 and T2.
These two transactions manipulate three objects, x, y and z. We have a constraint among them, that says x+y=z.

T1 reads x and awaits
T2 reads x and y and does x=x+40 and y=y-40. T2 commits.

Now T1 reads y. T1 reads the old value of y, but with a read concern ‘local’ we would expect to read the new value, so that the constraint is not satisfied, but we are still reading the old value so no phantom read happen.

What’s the difference with the read concern “snapshot” if we keep reading something coherent with the data at the start of transaction?

The anomaly that we can observe is the write skew anomaly, but not the others.
What are we missing?

Hi Federica!

First of all thank you for your question and for your interest in MongoDB transactions’ semantics.

At this time all our transactions forbid non-repeatable reads independently of the read/write concern you use. This is not a guarantee we document, it’s an implementation detail, but it does mean that in practice you won’t be able to experience them. It’s possible that in future versions non-snapshot RC experiences this anomaly though.

If you are interested in evaluating anomalies it’s worth noting that non-snapshot transactions suffer from read skew across shards. If your X resides in shard A and Y in shard B, then you can have the following history:

X=100
Y=100
Z=200

T1 reads X = 100 and awaits.
T2 reads X = 100, Y = 100 and does X = 140 and Y = 60, commits.
T1 reads Y = 60, Z = 100, which violates that X+Y=Z.

This is not possible with RC snapshot, but it’s possible with RC local or majority.

2 Likes