Node.js · Senior · Question 72 of 75
Two requests read a record, change it and save it at the same time, and one update is lost. How does optimistic locking prevent this?
- 1Requests are processed by one Node.js process only
- ✓Correct answer: Each row has a version; the update includes
WHERE version = $oldand fails if another write changed it, so the client retries - 3The database locks the whole table for every read
Explanation
Optimistic locking works well when conflicts are rare. Pessimistic locking (SELECT ... FOR UPDATE) fits frequent conflicts but holds locks longer.