Deadlock: Two or more threads wait forever for locks the others hold — no thread can proceed. A deadlock happens when two or more threads each hold a lock the other needs, and all of them wait forever. Thread A holds lock 1 and waits for lock 2, while thread B holds lock 2 and waits for lock 1.
Neither can continue, and nothing breaks the tie by itself. In application code this can freeze a process. In databases, the engine usually detects the deadlock and aborts one of the transactions with an error.
What should you keep in mind with Deadlock?
The classic prevention is consistent ordering. If every part of the code always takes locks in the same order, for example always the account with the lower ID first, a cycle cannot form. Keep locks and transactions short, and avoid waiting on external services while holding a lock, because that stretches the window in which a deadlock can occur.
Use when code runs things at the same time - threads, async/await, queues and workers, parallel requests, cron jobs, webhooks that retry - or when a bug only happens "sometimes" or under load. Covers race conditions, deadlocks, mutexes/locks, atomic operations, idempotency, message queues and backpressure, with Laravel and database examples.
Master error handling patterns across languages including exceptions, Result types, error propagation, and graceful degradation to build resilient applications. Use when implementing error handling, designing APIs, or improving application reliability.