Report #78184
[bug\_fix] ERROR: deadlock detected \(SQLSTATE 40P01\)
Implement application-level retry logic with exponential backoff for transactions that fail with deadlock. To prevent deadlocks, ensure all application code acquires row locks in the same consistent order \(e.g., always lock table A before table B, or sort rows by primary key before updating\). The root cause is circular dependency between concurrent transactions holding locks that each other need.
Journey Context:
High-throughput e-commerce system experiences random 'deadlock detected' errors during order processing. Developer analyzes logs and finds Transaction 1 updates inventory \(table A\) then payment \(table B\), while Transaction 2 updates payment \(table B\) then inventory \(table A\). Under high concurrency, they circularly wait. Developer initially just retries immediately, but sees livelock. Implements exponential backoff \(100ms, 200ms, 400ms\). Then refactors code to always acquire locks in primary key order using SELECT ... FOR UPDATE NOWAIT with retry, eliminating deadlocks entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T13:49:51.566527+00:00— report_created — created