{"success":true,"name":"never-call-an-external-service-inside-a-database-transaction","description":"Mixing an API call or email send into a transaction's steps","category":"ai-brimind","path":"ai-brimind/never-call-an-external-service-inside-a-database-transaction/SKILL.md","file":"SKILL.md","content":"---\nname: never-call-an-external-service-inside-a-database-transaction\ndescription: \"Mixing an API call or email send into a transaction's steps\"\n---\n\n# Never Call an External Service Inside a Database Transaction\n\n## When to use\nA database transaction's body contains a network call — payment API, email, webhook — or a transaction stays open while waiting on anything external.\n\n## Example request\npayment API called mid-transaction → the API succeeds, the transaction later rolls back; money moved, record vanished. email sent inside the transaction → rollback can't unsend it; unrollbackable side effect. slow external call while the transaction holds locks → every other request queues behind it; the DB held hostage. — each welded the undoable to the unundoable.\n\n## Instructions\nAct as a notary who never signs the contract while a courier is still on the road. A transaction's power is that everything inside it can be undone as one unit — but an external call cannot be rolled back: the email is sent, the payment is captured, the webhook fired, regardless of what your database decides afterward, so mixing them creates the worst bugs — half-realities where the outside world and your records disagree. Structure the flow in phases instead: do the local database work in a short transaction, commit, then perform the external call, then record its outcome in a second step; and design for the crash between phases — a stored intent ('payment pending') that a retry or a periodic sweeper can find and complete, so nothing falls into the gap permanently. The lock angle matters too: a transaction waiting on a slow network holds its locks the whole time, strangling unrelated requests. If the business truly needs 'both or neither' with an external party, that is not a transaction problem — it's an explicit reconciliation design; say so. Only proceed once every transaction contains only database work, and every external effect has a recorded before-state and after-state around it.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #773\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}