The Very Hungry Transaction
summary
A small ActiveRecord save grows into a multi-hundred-query transaction as inventory updates, HTTP calls to a fulfillment API, and background-job enqueueing get wrapped together, causing deadlocks, lock timeouts, and eventually exhausts DB connections so the whole site stalls. Fixes are concrete: add indexes, limit items per order, move slow HTTP calls and job enqueueing out of transactions or use after_create_commit (Rails 7.2 adds defaults), prefer sequential cross-database operations to avoid non-atomic nested commits, and use tools like the isolator gem and DB performance tables to find slow transactions.
AI-written summary · about
chapters (16)
- 0:00Introduction
- 1:06Cat and the hungry transaction
- 3:59Simple database transaction
- 6:06Inventory updates in transaction
- 8:15Lock contention in transactions
- 10:00Slow transactions cause contention
- 12:50Move slow work out of transactions
- 16:27Fix delayed job execution
- 17:10External calls in transactions
- 19:16Idle transactions exhaust connections
- 21:44Nested transactions across databases
- 24:11Nested transactions break atomicity
- 26:39Key lessons from Cat
- 29:28Fixing problematic transactions
- 31:58Best practices for transactions
- 33:24Q&A
related talks
32mThe Very Hungry Transaction
33mThe Very Hungry Transaction
20mCRUD! The Consequences of Not Understanding How ActiveRecord ...
29mPutting Rails in a corner: Understanding database isolation
42mA Race To The Bottom: Database Transactions Undermining Your AppSec
40mCan I break this?: Writing resilient “save” methods
40mrails db:migrate:safely
41mConcurrency, Scalability and Transactions -- Myths and Surprises
