Database.Advanced.Is MVCC implementation of optimisti lock ?

Short Answer:

MVCC is a form of optimistic concurrency control, but not all optimistic locking is MVCC.


🧠 Detailed Comparison

FeatureMVCCOptimistic Locking (manual)
TypeSystem-level concurrency controlApplication or ORM-level concurrency control
Uses versions?✅ Yes — creates versions for each write✅ Yes — usually uses a version number or timestamp
Conflict detectionImplicit — via visibility rules in DB engineExplicit — you check version before writing
Locks used?❌ No locks for reads❌ No locks — check happens only at write time
When conflicts are handledAt commit time or during snapshot evaluationWhen updating the row and version doesn’t match

🧩 Example

🔹 MVCC (e.g., in PostgreSQL):

  • Transaction A starts and reads snapshot.
  • Transaction B updates the row and commits.
  • A doesn’t see B’s changes until it finishes or restarts.
  • If A tries to write based on old data, conflict may arise.

🔹 Optimistic Locking (custom or ORM-based):

  • Row has a version or updated_at column.
  • You read: version = 1
  • You write: UPDATE ... WHERE id = ? AND version = 1
  • If version was incremented by someone else, 0 rows updatedconflict.

🏗️ MVCC = Optimistic at Engine Level

  • MVCC handles versioning and snapshot isolation internally (transparent to you).
  • It works great for systems with many reads and few write conflicts.
  • MVCC is automated, while optimistic locking often requires manual setup in app logic.

✅ Summary:

MVCC is a built-in, low-level implementation of optimistic concurrency control, while optimistic locking usually refers to a higher-level, manual strategy implemented in application logic (e.g., via Hibernate, Spring Data, etc.).

This entry was posted in Без рубрики. Bookmark the permalink.