Deadlocks and lock ordering 00:00:01 Two transfers stop in the middle Two warehouse transfers need the same pair of stock records. One moves parts from shelf A to shelf B. The other moves parts back. Each has a perfectly good reason to lock its source before changing the counts. Transfer one acquires A. Before it reaches B, transfer two acquires B. Each still owns its first lock. No count is wrong and neither process has crashed. Why is this already a dangerous arrangement? One now waits for the owner of B. Two waits for the owner of A. Neither can finish the transaction that would release what the other needs. That closed chain of waiting is a deadlock. 00:00:43 Draw who is waiting for whom Compare an ordinary delay. If transfer two already owned both shelves, one could wait for two, and two could finish without asking for anything held by one. A wait on its own does not prove deadlock. Add the return edge. Now following the owner each transfer needs brings us back to the start. With these exclusive locks held until completion, neither participant can take its next step unaided. An engineer can inspect which transaction holds each lock and which resource each waiter needs. CPU utilization may be low. Adding a faster processor cannot grant an exclusive lock that another blocked transaction still owns. 00:01:26 Break the particular cycle A database with deadlock detection can abort one participant and roll its work back. Suppose it chooses transfer two. That releases B, letting transfer one complete with a consistent pair of shelf counts. Transfer two should retry the complete transaction against current values. Replaying just its last update can discard a check made before the rollback. External notifications need a repeat-safe design of their own. A timeout helps only if the application and database agree what gets cancelled and released. Reporting an error to the caller while leaving a transaction open can keep the original obstacle in place. 00:02:04 Remove the cause of this cycle Give every shelf a stable identifier and acquire the required locks in ascending order. Both directions of transfer now ask for A before B, even though the business meaning of source and destination is reversed. Replay the same arrivals. Two waits for A without holding B. One can acquire B, finish and release both. Then two proceeds. Contention remains, but this pair of transfers cannot produce the earlier cycle. A background importer that takes the same locks in another order can reintroduce the problem. Document the rule at the shared access boundary and verify all callers obey it. 00:02:45 Test transfers in both directions A reproducible test pauses one transfer just after acquiring its first lock and starts the opposite transfer. Check both that one can make progress and that the combined stock count is preserved. Avoid holding both shelf locks during an unrelated network call. Ordering prevents a class of cycles, while shorter lock lifetimes reduce how long perfectly legitimate waiters must stand still. The two transfers wanted the same resources all along. What changed was the order in which they could hold one while waiting for the other. Follow that order when deciding whether waiting can ever end.