Deadlocks in Databases

College Depth 71 in the knowledge graph I know this Set as goal
deadlock wait-for graph deadlock detection deadlock prevention victim selection

Core Idea

A database deadlock occurs when two or more transactions form a cycle in the wait-for graph — each holds a lock the next one needs, so none can proceed. Databases detect deadlocks by periodically checking the wait-for graph for cycles, then aborting one transaction (the victim, chosen by age, cost, or priority) to break the cycle. Prevention strategies include acquiring locks in a fixed global order or using timestamp-based protocols (wait-die: older waits, younger aborts; wound-wait: older aborts younger) that preclude cycle formation.

How It's Best Learned

Reproduce a deadlock experimentally: open two sessions, have each lock a different row, then have each attempt to lock the row held by the other. Observe which session is chosen as the victim and retried.

Common Misconceptions

Explainer

You already know from studying two-phase locking that transactions acquire locks before accessing data and hold them until commit. You also know the four Coffman conditions required for deadlock: mutual exclusion, hold and wait, no preemption, and circular wait. Database deadlocks are what happens when all four conditions are met simultaneously in a live system — and unlike the theoretical treatment, they happen routinely in production.

Picture two transactions running concurrently. Transaction A locks row 1, then tries to lock row 2. Transaction B locks row 2, then tries to lock row 1. Each is holding a lock the other needs, and neither will release its lock until it finishes — but neither can finish because it's waiting. This circular dependency is a deadlock, and no amount of waiting will resolve it. The system must intervene. The standard mechanism is a wait-for graph: a directed graph where nodes are transactions and an edge from T1 to T2 means "T1 is waiting for a lock held by T2." If this graph contains a cycle, a deadlock exists.

Once a deadlock is detected, the database must choose a victim — one transaction to abort so the others can proceed. The victim's locks are released, its changes are rolled back, and it typically retries automatically or returns an error to the application. Victim selection policies vary: some systems abort the youngest transaction (least work lost), others consider the cost of rollback (number of rows modified), and some use priority schemes. The key insight is that deadlock handling is not a bug fix but a normal part of database operation — your application code should expect occasional deadlock-induced aborts and implement retry logic.

Prevention strategies avoid deadlocks entirely by breaking one of the four Coffman conditions. The most practical approach is eliminating circular wait by imposing a global lock ordering — if all transactions always lock rows in the same order (say, by primary key), cycles cannot form. Timestamp-based protocols offer another option: in wait-die, an older transaction waits for a younger one, but a younger transaction is aborted rather than waiting for an older one. In wound-wait, the older transaction forces the younger to abort. Both schemes guarantee no cycles because the wait direction is always consistent with timestamp order. Prevention eliminates deadlocks but can increase aborts; detection allows maximum concurrency but requires periodic graph analysis. Most production databases use detection with victim selection, since deadlocks are rare enough that the occasional abort is cheaper than the overhead of strict prevention.

Practice Questions 5 questions

Prerequisite Chain

Counting to 10Counting to 20Understanding ZeroThe Number ZeroCounting to FiveOne-to-One CorrespondenceCombining Small Groups Within 5Addition Within 10Addition Within 20Two-Digit Addition Without RegroupingTwo-Digit Addition with RegroupingAddition Within 100Repeated Addition as MultiplicationMultiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Two-Digit by One-Digit DivisionDivision with RemaindersRemainders and Quotients in DivisionDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueReading and Writing DecimalsComparing and Ordering DecimalsAdding and Subtracting DecimalsMultiplying DecimalsDividing DecimalsDividing FractionsMixed Number ArithmeticOrder of OperationsOperators and ExpressionsArithmetic Operators and Operator PrecedenceComparison Operators and Boolean TestsLogical Operators and Boolean AlgebraBoolean Algebra and Fundamental LawsCombinational Circuit DesignFlip-Flops and LatchesBinary Counters: Design and AnalysisBinary ArithmeticFixed-Point Number RepresentationTwo's Complement RepresentationOverflow and Underflow DetectionBinary Adders: Half-Adders and Full-AddersFull Adder and Carry PropagationCarry Lookahead Adder DesignHalf Adder Circuit DesignMultiplication Circuit DesignSequential Circuit DesignRegisters and Register FilesInstruction Set Architecture (ISA)Kernel Architecture and OS StructureSystem Calls and User/Kernel ModeProcesses and the Process Control BlockProcess Creation: fork() and exec()Process Termination and Resource CleanupProcess States and State TransitionsThreads and ConcurrencyThe Critical Section Problem and Race ConditionsMutual Exclusion and LocksSemaphoresDeadlock: Conditions and ModelingDeadlocks in Databases

Longest path: 72 steps · 278 total prerequisite topics

Prerequisites (2)

Leads To (0)

No topics depend on this one yet.