Report #7430
[bug\_fix] ERROR: could not serialize access due to read/write dependencies among transactions \(40001\) \[Serializable Isolation Failure\]
Implement an application-level retry loop specifically for 40001 serialization failures \(required by SQL standard for SERIALIZABLE\). Reduce transaction duration and scope. Root cause: Postgres uses Serializable Snapshot Isolation \(SSI\) to detect rw-dependencies \(dangerous structures\) that could lead to anomalies; when detected, it aborts one transaction to guarantee serializability.
Journey Context:
Inventory system using SERIALIZABLE isolation to prevent overselling. Under Black Friday load, 5% of checkout transactions fail with 'could not serialize access'. Developers initially blame deadlocks, but error code is 40001, not 40P01. They learn that SSI tracks read/write dependencies between concurrent serializable transactions. When two transactions read the same data and one writes, creating a rw-dependency cycle, Postgres aborts one. The fix is wrapping the checkout logic in a retry decorator that catches SerializationFailure \(psycopg2.errors.SerializationFailure\) and retries with exponential backoff, which is the mandated pattern for SERIALIZABLE isolation.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T02:42:55.954579+00:00— report_created — created