Locks and Deadlocks

Detection and prevention.

Overview

Database locks and deadlocks are the concurrency reality of any system that runs multiple transactions in parallel. Database engines detect deadlocks and abort one transaction to break the cycle; the application’s job is to acquire locks in consistent order to prevent deadlocks where possible, and to retry transparently when the engine aborts a transaction. The discipline is structural: consistent lock ordering prevents deadlocks at the design level rather than relying on retry to paper over them.

The approach

The practical approach is consistent lock order across transactions (deadlocks become impossible by design), retry on deadlock with exponential backoff (the engine’s deadlock signal triggers application-level retry), monitor lock-wait time as a leading signal of contention, use SELECT FOR UPDATE deliberately where row-level locking matters, and document per-table lock patterns committed to the schema documentation.

Why this compounds

Lock discipline compounds across services and tables. Each correct lock-ordering pattern prevents deadlocks for the lifetime of the codebase; each documented pattern survives team turnover; the team builds intuition for concurrent database design that pays off on every new feature. Without the discipline, deadlocks recur and the team treats retry as a substitute for design.

Lock and deadlock discipline is a database discipline that pays off across years. Nova AI Ops integrates with database telemetry, surfaces lock patterns, and supports the team’s database engineering discipline.