Dealing With Contention easy
Two users try to 'Like' a post (Count=10) at the exact same moment. Both read 10. Both add 1 and write 11. What is the result?
- A Count = 12 (Correct)
- B Count = 11 (One like is lost)
- C Count = 10
- D Database error.
Answer: B. Count = 11 (One like is lost)
This is the classic Lost Update. The second write overwrites the first write because it was based on stale data. The fix is Atomic increments or Locking.
One system design question a day, with spaced repetition so the ones you miss come back.
Get the appRelated questions
- Why is 'Exponential Backoff' preferred over constant intervals for retries?
- What is the primary purpose of a Mutex (Mutual Exclusion) lock?
- Why does adding more threads sometimes *decrease* performance?
- In a Saga, if a Compensating Transaction (Undo) fails repeatedly due to contention, what is the last resort?
- In a database, what happens if Transaction A locks a row and Transaction B tries to modify the same row?
- A database restart causes thousands of application servers to instantly try to reconnect. This crashes the DB again. This is called: