Design a Deadlock Detector
🔒 What is a deadlock detector? It is the part of a lock manager, like the one inside a database, that notices when transactions are stuck waiting for each other forever. Transaction 1 holds row A and wants row B; transaction 2 holds row B and wants row A. Neither can move. The detector finds that circle of waiting and breaks it by aborting one of the transactions, so the others can continue.
Most deadlock questions ask you to avoid deadlock, usually by taking locks in a fixed order. This one goes the other way: let transactions take locks in any order, and fix things when they get stuck. That is what real databases do, because they cannot know in advance which rows a query will touch. The interview checks three things: can you model the lock table, can you turn "stuck" into a graph problem, and can you pick a victim and clean up without leaving the lock table in a broken state.
Requirements
"Build a lock manager for a database. Transactions lock resources. When transactions end up waiting on each other in a cycle, detect it and resolve it."
Clarifying Questions
You: What kinds of locks? Shared read locks and exclusive write locks?
Interviewer: Only exclusive locks for now. One holder per resource at a time.
You: Should we prevent deadlocks, or detect and break them?
Interviewer: Detect them. Transactions lock whatever they need, in any order.
You: When should we look for a deadlock? Every time someone starts waiting, or on a timer?
Interviewer: As soon as it forms. We don't want transactions sitting stuck for seconds.
You: Which transaction do we abort?
Interviewer: The one that loses the least work. Use the number of locks it holds as the measure; on a tie, abort the youngest. Transaction ids grow over time, so the highest id is the youngest.
You: What happens to the victim?
Interviewer: It is aborted: it loses all its locks and its place in any queue. Its caller can start a new transaction and retry.
You: In what order do waiters get a lock when it is released?
Interviewer: First come, first served.
You: Many sessions call the lock manager at once, right? Does acquire block?
Interviewer: Yes, many threads. To keep it testable, acquire returns right away and says whether the lock was granted, the transaction is waiting, or it was aborted. A caller that is waiting can check its state.
The fourth answer is the one that makes this a design problem and not only a graph problem. "Which transaction to abort" is a policy, and policies change, so we will keep it in one small method.
💡 Interview tip: Ask "prevent or detect?" first. Prevention (lock ordering, timestamps) and detection (a wait-for graph) are completely different designs, and starting on the wrong one wastes the interview.
Final Requirements
- Transactions acquire exclusive locks on named resources. A transaction starts with its first
acquireand ends withrelease(commit) or by being aborted. - A free resource is granted at once. A transaction asking again for a lock it holds is granted again with no change.
- A transaction asking for a lock held by another one waits in that resource's FIFO queue.
- When a new wait closes a cycle of waiting transactions, the manager detects it immediately and aborts one transaction in the cycle: the one holding the fewest locks, and on a tie the one with the highest id.
- An aborted or committed transaction releases all its locks, and each released lock goes to the first waiter in its queue.
- Callers can ask who holds a resource, a transaction's state, and whom a transaction is waiting for.
- Many threads use the manager at the same time.
Out of scope
- Shared (read) locks and lock upgrades (Extensibility).
- Deadlocks across several machines (Extensibility).
- Undoing a transaction's writes on abort. That is the database's job; we only handle locks.
- Lock timeouts as a separate feature.