Database Deadlock
What Is a Database Deadlock?
A database deadlock is a transaction conflict that stops two or more database operations because each is waiting for another to release a lock. Neither operation can finish until the other does. A transaction is a group of changes the database treats as one unit, while a lock temporarily protects data from conflicting changes.
A short lock wait is normal. For example, one update may wait briefly while another finishes. A deadlock is different because the waiting forms a circle, so it cannot resolve without database intervention.
How a Database Deadlock Happens
Consider two transactions updating the same two rows in opposite order.
- Transaction A locks Row 1 and then requests Row 2.
- Transaction B locks Row 2 and then requests Row 1.
- Each row is exclusively held by one transaction, which is mutual exclusion.
- Both transactions hold one lock while waiting for another, cannot have locks forcibly removed, and form a circular wait.
These four conditions, mutual exclusion, hold and wait, no preemption, and circular wait, must exist together for a deadlock to occur.
Database Lock vs. Deadlock
Locks protect data consistency. A deadlock is a specific failure state involving locks.
| Situation | Cause | Outcome and app response |
|---|---|---|
| Normal lock wait | One transaction is still working. | The waiting transaction usually continues after release. |
| Deadlock | Transactions wait on each other in a cycle. | The database rolls back one transaction. Retry safe work. |
| Lock timeout | A wait exceeds the configured time limit. | The database returns an error. Review the slow or blocked work. |
How Databases Detect and Resolve Deadlocks
Modern database engines look for cycles in which transactions are waiting on one another.
- The database identifies a circular dependency between active transactions.
- It selects a deadlock victim, often the transaction that is cheaper to roll back.
- It rolls back that transaction and releases its locks.
- The application receives an error and retries only work that is safe to repeat.
- Developers review deadlock logs or reports to find the conflicting statements and lock order.
Retries should be bounded and use a short backoff. Idempotent operations, which produce the same result if repeated, are the safest candidates for retrying.
How to Avoid Database Deadlocks
Deadlocks cannot always be eliminated, but careful transaction design makes them far less likely.
- Access the same tables and rows in a consistent order across the application.
- Keep transactions short and avoid waiting for user input inside them.
- Add appropriate indexes so queries lock fewer rows for less time.
- Choose an isolation level that matches the consistency the feature actually needs.
- Use bounded retry logic with backoff, rather than unlimited automatic retries.
Database Deadlocks in Apps Built With Emergent
Deadlock-aware design matters when a full-stack app handles simultaneous edits, payments, bookings, or inventory changes. Emergent helps teams build data-backed workflows, starting with database setup and clear transaction boundaries. Builders can also compare a no-code database approach with application needs, then use an AI app builder to implement safe retries and test concurrent user flows.
on Emergent today

