← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Optimistic Locking

concurrency Intermediate

TL;DR

Optimistic locking detects conflicts at commit time using a version number — no locks held during the transaction, high throughput for low-contention scenarios.

Explanation

Pattern: read record with version number, modify, update WHERE version = read_version, check rows affected = 1. If 0 rows updated: conflict — retry or error. The version column increments on every update. No locks held between read and write — high throughput. Best for: low-contention scenarios (most updates succeed without conflict). Implementations: Doctrine ORM @Version annotation, JPA @Version, Hibernate. Database: UPDATE ... WHERE id = ? AND version = ?. Redis: WATCH/MULTI/EXEC (optimistic transaction). Contrast with pessimistic locking (SELECT FOR UPDATE — locks row immediately).

Common Misconception

Optimistic locking prevents all conflicts — it detects them at commit time and requires application-level retry logic. Conflicts still occur but are handled gracefully.

Why It Matters

Optimistic locking enables high-throughput concurrent updates without holding DB row locks — essential for scenarios like inventory management where most updates don't conflict.

Common Mistakes

  • Not incrementing the version on every update — defeats the mechanism.
  • Not handling the 0-rows-updated case — silent data loss.
  • Using optimistic locking for high-contention scenarios — many retries degrade performance.

Code Examples

✗ Vulnerable
// No conflict detection:
SELECT qty FROM inventory WHERE id = 1;
-- Another process also reads qty = 10
UPDATE inventory SET qty = 9 WHERE id = 1; -- Both processes set 9, losing one decrement
✓ Fixed
-- Read:
SELECT qty, version FROM inventory WHERE id = 1;
-- (qty=10, version=5)

-- Write with version check:
UPDATE inventory
SET qty = 9, version = version + 1
WHERE id = 1 AND version = 5;
-- 0 rows updated? Another process got there first — retry or error

Added 23 Mar 2026
Views 36
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S
No pings yet today
Amazonbot 1 Perplexity 1
Amazonbot 10 Perplexity 9 Unknown AI 3 SEMrush 3 Ahrefs 2 Google 2 Majestic 1
crawler 28 crawler_json 1 pre-tracking 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Add a version column. Check rows-affected after UPDATE ... WHERE version = N. If 0: retry or throw ConcurrentModificationException.
📦 Applies To
web cli queue-worker Laravel Symfony Doctrine
🔗 Prerequisites
🔍 Detection Hints
version|optimistic
Auto-detectable: ✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: File Tests: Update
CWE-362

✓ schema.org compliant