{"success":true,"name":"make-multi-step-writes-all-or-nothing-use-a-transaction-not-","description":"Writing backend code that changes two or more records in one operation","category":"ai-brimind","path":"ai-brimind/make-multi-step-writes-all-or-nothing-use-a-transaction-not-/SKILL.md","file":"SKILL.md","content":"---\nname: make-multi-step-writes-all-or-nothing-use-a-transaction-not-\ndescription: \"Writing backend code that changes two or more records in one operation\"\n---\n\n# Make Multi-Step Writes All-or-Nothing: Use a Transaction, Not Hope\n\n## When to use\nWriting backend code that changes two or more records in one operation\n\n## Example request\nCreate the order, then update the stock → server crashes between the two, an order exists for stock that was never reserved; the two writes needed to be one atomic unit. Money transfer subtracts from account A, then the add to account B throws → money vanished; this is the textbook case for a database transaction. Registering a user creates the account, then the profile insert fails → half-users pollute the DB and break logins; 'all succeed or all roll back' was the missing rule. Only one single write happens → no transaction needed; atomicity is a multi-write problem, do not add ceremony to a single insert.\n\n## Instructions\nAct as a backend engineer who never lets the database land in a half-finished state — because the crash always happens at the worst possible line. When writing any operation that touches data, do this: (1) Count the writes. One write → move on, this skill does not apply. Two or more writes that only make sense together → they are one logical unit and must be atomic. (2) Say out loud what the database looks like if the process dies exactly between write one and write two. If that picture is broken data (order without stock change, half a transfer, user without profile), you have proven you need a transaction. (3) Use the real mechanism your stack provides — a database transaction (session/withTransaction in MongoDB with a replica set, BEGIN/COMMIT in SQL, the transaction API of your ORM) — so that an error anywhere rolls back everything. Do not simulate it with try/catch plus manual 'undo' writes; hand-rolled undo can itself fail and is exactly the wheel transactions were invented to replace. (4) Make sure any error inside the unit actually aborts it: a swallowed exception inside a transaction commits garbage with confidence. (5) If your setup truly cannot use transactions (e.g. standalone Mongo without replica set), say so explicitly, then order the writes so a crash leaves the LEAST harmful state and make the operation detectable and repairable — a conscious fallback, not an accident. Only mark the code done once you can state which mechanism guarantees all-or-nothing and describe what the DB contains after a crash at every step: the answer must always be 'a clean state', never 'it depends'.\n\n## Metadata\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #190\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}