Part 1 of 3 — the single-document case.
There's a class of bug that every backend engineer ships at least once, usually
without noticing for months. It hides inside the most innocent-looking operation:
read a document, decide something, write it back.
Take a concrete invariant: a team can hold at most 10 seats. To add a seat you
read the team document, count the seats, check count < 10 , and write. A textbook
Read → Modify → Write .
Now run it twice at the same instant. Request A reads count = 9 , decides "9 < 10,
fine", and writes 10. Request B, a millisecond apart, also read count = 9 ,
decided "fine", and writes 10. You now have a team that thinks it has 10 seats but
actually granted 11. Neither request did anything wrong on its own. One write
silently erased the premise of the other.…