Report #89000
[bug\_fix] ERROR: deadlock detected
Implement application-level retry logic for transactions that fail with 40P01, and enforce a consistent row-locking order \(e.g., always lock records by primary key in ascending order\) to prevent the circular wait condition.
Journey Context:
Built a concurrent order processing system where multiple workers update inventory and order status simultaneously. Intermittently, workers crashed with 40P01 errors. Logs showed Worker A locked inventory row 1 then tried to lock order row 2, while Worker B locked order row 2 then tried to lock inventory row 1—classic circular dependency. Implemented a strict rule to always lock tables in alphabetical order \(inventory before orders\) and added a tenacious retry decorator that catches DeadlockDetected exceptions and re-executes the transaction block.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T07:58:25.901207+00:00— report_created — created