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

Pessimistic Locking

concurrency Intermediate

TL;DR

Pessimistic locking acquires an exclusive lock immediately on read — preventing any concurrent modification. Right for high-contention scenarios but reduces throughput.

Explanation

SELECT ... FOR UPDATE locks the selected rows until the transaction commits or rolls back. No other transaction can modify those rows. High-contention scenario: inventory reservation, seat booking, financial transfers. MySQL: SELECT ... FOR UPDATE, SELECT ... FOR SHARE (read lock). PostgreSQL: similar plus SKIP LOCKED (skip already-locked rows — useful for queue workers). Trade-offs: prevents races completely but serialises updates, reducing throughput. Long transactions increase deadlock risk. FOR SKIP LOCKED is excellent for queue workers — each worker takes uncontested rows.

Common Misconception

Pessimistic locking is always safer than optimistic — it's safer for high-contention but creates deadlock risk with multiple locks and reduces throughput.

Why It Matters

Pessimistic locking is the right choice when conflicts are frequent and retries are expensive — seat booking, financial transactions, and inventory reservation.

Common Mistakes

  • Using pessimistic locking for low-contention scenarios — unnecessary performance cost.
  • Holding pessimistic locks across slow I/O (HTTP calls, file writes) — deadlock risk.
  • Not using SKIP LOCKED for queue workers — causes lock contention between workers.

Code Examples

✗ Vulnerable
-- Long-running pessimistic lock across slow operation:
START TRANSACTION;
SELECT * FROM payments FOR UPDATE;
-- Calls external payment API (2 seconds) while holding lock
UPDATE payments SET status = 'processed';
✓ Fixed
-- Queue worker with SKIP LOCKED:
START TRANSACTION;
SELECT * FROM jobs
WHERE status = 'pending'
LIMIT 1
FOR UPDATE SKIP LOCKED; -- Each worker gets a different row

-- Seat booking:
START TRANSACTION;
SELECT * FROM seats WHERE id = ? FOR UPDATE;
-- Check if available, then book — all under lock

Added 23 Mar 2026
Views 31
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 2 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 1 ping W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Perplexity 8 Amazonbot 7 Google 4 Unknown AI 3 Ahrefs 2 ChatGPT 1 Meta AI 1
crawler 23 crawler_json 1 pre-tracking 2
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Use SELECT FOR UPDATE for high-contention row reservations. Use SKIP LOCKED for queue workers. Keep transactions short — release locks quickly.
📦 Applies To
web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
FOR UPDATE|lockForUpdate
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