When would you choose optimistic versus pessimistic locking for a high-concurrency web application?

Last updated 3 July 2026

  • Optimistic locking holds no lock: it checks a version at write time (UPDATE ... WHERE version = ?) and retries if 0 rows change. Use it when conflicts are rare, the usual web-app default.

  • Pessimistic locking grabs a lock up front (SELECT ... FOR UPDATE). Use it when conflicts are frequent or retries are costly, but it reduces concurrency and risks deadlocks.

  • Decide by conflict probability and retry cost, and never hold DB locks across user think-time.

  • https://vladmihalcea.com/optimistic-vs-pessimistic-locking/
  • https://vladmihalcea.com/a-beginners-guide-to-java-persistence-locking/
  • https://medium.com/@mriduaayu123/master-database-concurrency-pessimistic-vs-optimistic-locking-1476b133381f
All SQL interview questions