Deadlocks and lock order 00:00:00 Make a prediction You will draw a circular wait and remove one necessary condition using a global lock order. Transfer A holds account twelve and wants twenty-four. Transfer B holds twenty-four and wants twelve. Which transfer can go next without changing the design? Neither. Each waits for a lock that only the other can release. Make both acquire the lower account identifier first, irrespective of transfer direction, and the cycle cannot form for those locks. 00:00:31 Window cleaning Two window cleaners need the same ladder and bucket. One grabs the ladder while the other takes the bucket. Agree to take the ladder first, then the bucket. The second cleaner now waits while holding neither. Deadlock depends on a cycle of resources held while waiting. A shared order works only when every code path acquiring these same locks follows it. A deadlock is a group of threads in which each waits for something that only another one of the group can give. Nobody can move, so nobody ever will. With locks, the group is a circle: thread 1 waits for a lock of thread 2, thread 2 for one of thread 3, and the last for one of thread 1. Because finer locks are the usual way to more speed, and the second lock brings the first deadlock. A design with one lock cannot have this problem. Every design with two must say its order. 00:01:27 Order a transfer's two locks One transfer moves money from account twelve to twenty-four. A second transfer moves money in the opposite direction. Taking the source lock first allows each to hold one lock while waiting for the other. Give both transfers the same rule: acquire the lower account identifier first, then the higher. Release both reliably. The second transfer now waits before holding the second lock, so no circular wait forms. A transfer to the same account needs explicit handling as well. Start two opposite transfers and pause the first after it obtains account twelve. The second must wait for twelve before it can hold twenty-four. Release the first and the second can finish. Reverse the order in only one code path and both can again hold what the other needs. 00:02:15 A callback can wait on its own caller An account service holds a non-reentrant mutex while adding ten to a running total. Before unlocking, it notifies a listener. The listener asks the same service for the new total, which requires that same mutex. There is only one thread here, yet the callback cannot finish and its caller cannot release the guard. Register a listener that immediately asks the service for its new total. If the service invokes that listener with a non-reentrant mutex held, the same thread waits for its own lock. Snapshot the event under the mutex, release it and then invoke the listener. Changing the relative order of account locks cannot repair this case. Give that single mutex the highest lock rank in the program. The call still hangs because the thread tries to acquire its own non-reentrant mutex twice. A lock-order review catches inconsistent ordering between locks; it also needs a separate check for calling unknown code while a guard is held. Use bounded try-lock and retry carefully when a global order is impossible. It can avoid indefinite holding but introduces backoff, failure handling and possible starvation. PostgreSQL detects a circular wait and aborts one of the involved transactions so another can progress. Linux also has a validator that can learn dependency order between lock classes and flag inconsistent patterns before a particular execution actually hangs. Prevention and detection answer different questions, and either needs a plan for the operation that fails. 00:03:49 Try an interview follow-up How would you keep a large code base free of deadlocks? I would make it easy to follow one lock order and difficult to create a second one. A shared helper acquires ranked locks consistently, and a test flags inversions. I also look for callbacks or slow external calls inside a lock. Finally I capture thread dumps when progress stalls so that a remaining wait cycle can be diagnosed. One worker waits while other jobs keep running and taking the resource first. Is that necessarily a deadlock? No. The other workers continue making progress and there may be no circle of waiting. Look for the cycle in a deadlock. For the repeatedly bypassed worker, test the queueing policy for starvation. 00:04:42 What to take with you A deadlock is a circle of waiting. Four conditions make it possible, and each cure removes one. The default cure is a global order: sort the locks by a key that never changes, take them upwards, and put that rule into one helper. If you cannot sort, wait with a limit, release what you hold, pause for a random time and give up after a few tries. If work can be undone, as in a database, a detector may find the circle and stop a victim.