# How MVCC works here

**URL:** <https://forums.foundationdb.org/t/how-mvcc-works-here/2764>\
**Category:** Using FoundationDB\
**Created:** [June 20, 2021, 7:43pm UTC](https://forums.foundationdb.org/t/how-mvcc-works-here/2764 "2021-06-20T19:43:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vorandrew](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/vorandrew/32/1117_2.png) [@vorandrew](https://forums.foundationdb.org/u/vorandrew)\
**Post date:** [June 20, 2021, 7:43pm UTC](https://forums.foundationdb.org/t/how-mvcc-works-here/2764/1 "2021-06-20T19:43:33Z")

</div>

If I concurrently run 100 times this. What will I get key1 == 100 or key1 = 1? If 100, why?

```auto
db.transaction(trx => {
 k1 = trx.read('key1) || 0
 trx.write('key1',k1++)
})

```

---

<div class="post-metadata">

**Author:** ![andrew.noyes](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/andrew.noyes/32/443_2.png) [@andrew.noyes](https://forums.foundationdb.org/u/andrew.noyes)\
**Post date:** [June 21, 2021, 6:09pm UTC](https://forums.foundationdb.org/t/how-mvcc-works-here/2764/2 "2021-06-21T18:09:53Z")

</div>

Did you mean `++k1` instead of `k1++` ?

Assuming this is using the default retry loop, after all 100 transactions commit you will see key1 \>= 100. This is because the default retry loop assumes transactions are idempotent, so it retries `commit_unknown_result`. It could be that there was a network fault after attempting to commit one of these transactions and before getting a reply, but the attempt actually succeeded and the retry would increment a second time.

You would also see a lot of retries because of `not_committed` errors, since by default transactions don’t commit if any of the reads they performed might have given a different result at commit time. Each of the 100 transactions will commit one at a time and see the writes from all previously committed transactions.

---

<div class="post-metadata">

**Author:** ![vorandrew](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/vorandrew/32/1117_2.png) [@vorandrew](https://forums.foundationdb.org/u/vorandrew)\
**Post date:** [June 22, 2021, 2:40am UTC](https://forums.foundationdb.org/t/how-mvcc-works-here/2764/3 "2021-06-22T02:40:33Z")

</div>

Thanks Andrew,

> It could be that there was a network fault after attempting to commit one of these transactions and before getting a reply, but the attempt actually succeeded and the retry would increment a second time.

What is best practice to mitigate such effects (except indempotence)?

---

<div class="post-metadata">

**Author:** ![andrew.noyes](https://sea1.discourse-cdn.com/foundationdb/user_avatar/forums.foundationdb.org/andrew.noyes/32/443_2.png) [@andrew.noyes](https://forums.foundationdb.org/u/andrew.noyes)\
**Post date:** [June 22, 2021, 3:19am UTC](https://forums.foundationdb.org/t/how-mvcc-works-here/2764/4 "2021-06-22T03:19:48Z")

</div>

There’s some advice here: [Developer Guide — FoundationDB 6.3](https://apple.github.io/foundationdb/developer-guide.html#transactions-with-unknown-results). There’s also some discussion on handling this automatically here: [Automatically providing transaction idempotency](https://forums.foundationdb.org/t/automatically-providing-transaction-idempotency/1873), but I don’t think anyone is actively working on this.
