Skip to the content.

1. Pessimistic Locking

🔑 Concept:

🛠 How it works:

  1. Transaction A reads a row → places a lock.
  2. Transaction B tries to read/update the same row → blocked until A commits/rolls back.
  3. Once A finishes, B can proceed.

✅ Advantages:

⚠️ Disadvantages:

🖥 Example (SQL):

-- Transaction A
SELECT * FROM accounts WHERE id = 1 FOR UPDATE;

-- Transaction B waits until A commits
UPDATE accounts SET balance = balance + 100 WHERE id = 1;

2. Optimistic Locking

🔑 Concept:

🛠 How it works:

  1. Transaction A reads row with version = 1.
  2. Transaction B reads the same row (version = 1).
  3. Transaction A updates → increments version = 2.
  4. Transaction B tries to update → version mismatch → fails/rolls back.

✅ Advantages:

⚠️ Disadvantages:

🖥 Example (SQL):

-- Table has a 'version' column
UPDATE accounts
SET balance = balance + 100, version = version + 1
WHERE id = 1 AND version = 1;

3. Comparison Table

Feature Pessimistic Locking Optimistic Locking
Assumption Conflicts are likely Conflicts are rare
Locking Locks data before read/write No locks; checks on update
Concurrency Low (blocked by locks) High (no blocking)
Risk of Deadlock High Low
Use Case High-contention scenarios Low-contention scenarios
Complexity Easy to implement Needs versioning & retry logic

💡 Rule of Thumb:


1. Pessimistic Locking

Time →
Transaction A:  Read row X  ──> Lock row X ──> Update row X ──> Commit ──> Unlock
Transaction B:  Wait (row X locked) ────────────────────────────> Read/Update row X

Explanation:


2. Optimistic Locking

Time →
Transaction A:  Read row X (version=1) ──> Compute ──> Update row X (version=2)
Transaction B:  Read row X (version=1) ──> Compute ──> Update row X (version mismatch!) ──> Retry

Explanation:


💡 Key Visual Difference:

Pessimistic:   [LOCKED]   → others wait
Optimistic:    [NO LOCK]  → conflicts detected at update