✅ Short Answer:
MVCC is a form of optimistic concurrency control, but not all optimistic locking is MVCC.
🧠 Detailed Comparison
Feature | MVCC | Optimistic Locking (manual) |
---|---|---|
Type | System-level concurrency control | Application or ORM-level concurrency control |
Uses versions? | ✅ Yes — creates versions for each write | ✅ Yes — usually uses a version number or timestamp |
Conflict detection | Implicit — via visibility rules in DB engine | Explicit — you check version before writing |
Locks used? | ❌ No locks for reads | ❌ No locks — check happens only at write time |
When conflicts are handled | At commit time or during snapshot evaluation | When 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
orupdated_at
column. - You read:
version = 1
- You write:
UPDATE ... WHERE id = ? AND version = 1
- If
version
was incremented by someone else, 0 rows updated → conflict.
🏗️ 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.).