Chapter 22
Optimistic and Pessimistic locks
Ultra popular question.
Basically, the question is always somewhere around: "You have 2 users updating one account simultaneously. How do you make this operation safe?"
Here you should know that one of the ways is to lock the operation. There are 2 types of locks.
Optimistic Locks 🙂
It is a strategy: assume conflicts are rare, don't lock anything up front, and check at commit time whether someone else changed the data.
Rule of thumb: We have more reads than writes. When a new transaction comes in, we are optimistic that the data has not changed — because there are not many writes happening.
