Top 3 Optimistic locking Interview Questions
Browse 3 real-world technical and behavioral interview questions about Optimistic locking. Review scenarios, edge cases, and architectural best practices.
A flash sale decrements stock from many threads. Do you go optimistic or pessimistic, and how do you prove nothing oversold?
For one stock counter, optimistic locking with a compare-and-set loop fits: read, refuse at zero, and publish the decrement only if nobody changed the value meanwhile. A lock suits a larger critical section. Prove it by arguing each sale consumes one unit, plus a stress test seen to fail on the broken code.
Nobody called save, but the UPDATE still went to the database. Explain how that happened.
Hibernate dirty checking tracks a managed JPA entity through the persistence context. At flush the provider compares the entity against its loaded snapshot and writes an UPDATE for whatever differs, so mutating a managed object inside a transaction is itself the instruction to persist. Use this spring data jpa answer to show the decision, trade-off, and evidence rather than a memorised definition.
Two users edit the same record and both save. What does EF Core do about it?
EF Core optimistic concurrency uses a rowversion or concurrency token so a stale update affects zero rows instead of overwriting another user's change. The application must then merge, retry or ask the user.