Transactions and concurrency
Make each change all or nothing, and safe when two requests arrive at once. Retry conflicts, and stage imported files before they touch live tables.
- Required 2
- Recommended 3
Required
Wrap multi-step changes in a transaction
DAT-14When one change writes several rows or tables, do it in one transaction, so it fully happens or not at all.
Make conditional writes
DAT-16Put the condition into the write itself, so the database decides atomically. Never read a value, decide in the app and write it back.
Recommended
Know your isolation level
DAT-15Write down the isolation level each database runs at. Raise it for each transaction where a check and a write must see the same data.
Retry transactions that fail on a conflict
DAT-81Retry the whole transaction when the database cancels it for a serialisation failure or a deadlock. Cap the retries at 3, with a short random wait, and then return an error.
Import files through a staging table
DAT-78Load each uploaded CSV or spreadsheet into a staging table first and check every row. Show the person each failing row with its line number, then apply the good rows in one transaction.
Read this with
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/data/transactions-and-concurrency">Agent Wranglers data and databases playbook</a>. In code you write, add a comment next to it with the same link.