Pessimistic Locking
What Is Pessimistic Locking?
Pessimistic locking is a database concurrency-control method that locks a row while one transaction works on it, preventing conflicting changes by other transactions. It assumes collisions are likely, so competing transactions must wait, fail, or move past the locked row. A transaction is a group of database actions treated as one unit, helping applications keep important data consistent.
How Pessimistic Locking Works in SQL
A database commonly applies this pattern at the row level. The exact behavior depends on the database engine and isolation level.
- Begin a transaction.
- Read the needed row with a locking command, such as SELECT FOR UPDATE.
- Check business rules and update the row if valid.
- Commit the transaction, or roll it back on error.
- Release the lock so another transaction can proceed.
Common Lock Modes and Waiting Options
Lock commands control what another transaction can do when it encounters protected data.
| Mode or option | Practical behavior | Useful for |
|---|---|---|
| Update lock | Prevents competing updates to selected rows. | Inventory or balance changes. |
| Shared lock | Protects rows from updates while allowing compatible reads. | Read-then-decide workflows. |
| NOWAIT | Returns an error instead of waiting for a lock. | Fast user-facing actions. |
| SKIP LOCKED | Bypasses unavailable rows. | Background job workers. |
Names and exact semantics differ across PostgreSQL, MySQL, and ORM tools such as JPA, so developers should verify their database documentation.
Pessimistic vs. Optimistic Locking
The two main locking approaches handle conflicts at different points.
| Factor | Pessimistic | Optimistic |
|---|---|---|
| Conflict handling | Blocks conflicts before an update. | Detects a conflict during save. |
| Typical mechanism | Database row lock. | Version number check. |
| User experience | May wait or receive a lock error. | May need to refresh and retry. |
| Best fit | Frequent, costly collisions. | Mostly independent edits. |
Benefits of Pessimistic Locking
This approach prioritizes a predictable single-writer path for sensitive updates.
- Prevents lost updates when two requests target the same record.
- Makes high-conflict operations easier to reason about.
- Fits scarce inventory, account transfers, and job-claiming workflows.
Practical Limits and Risks
Pessimistic locking is not bad, but it is a poor fit for long or highly contested transactions.
- Waiting transactions can reduce throughput and cause timeouts.
- Deadlocks can occur when transactions lock rows in different orders.
- Holding a lock during a network call or user think time can stall other work.
When to Use Pessimistic Locking
Choose it when a conflicting update is likely and the cost of an incorrect result is high.
- Use it for known contention hot spots with short transactions.
- Prefer optimistic locking when retries are acceptable and conflicts are rare.
- Lock the smallest possible data set for the shortest possible time.
Designing Concurrent Apps with Emergent
Reliable full-stack apps need deliberate transaction boundaries, concurrent-request tests, and review of generated backend code. When using an AI app builder, specify which actions must be atomic, then test competing requests before release. Emergent can help teams move from app design and development to a working application while keeping database locking choices visible and reviewable.
on Emergent today

