{"success":true,"name":"guard-read-modify-write-two-requests-editing-the-same-record","description":"Backend code reads a record, changes it in memory, then saves it back","category":"ai-brimind","path":"ai-brimind/guard-read-modify-write-two-requests-editing-the-same-record/SKILL.md","file":"SKILL.md","content":"---\nname: guard-read-modify-write-two-requests-editing-the-same-record\ndescription: \"Backend code reads a record, changes it in memory, then saves it back\"\n---\n\n# Guard Read-Modify-Write: Two Requests Editing the Same Record Will Collide\n\n## When to use\nYou are writing backend logic that loads a record, modifies a value based on what it read (balance, stock, counter, status), and writes the result back — and two requests could run this code for the SAME record at the same time.\n\n## Example request\nTwo purchases read stock=1 at the same moment, both see 'available', both save stock=0 → the item sold twice; the read-then-write gap is the hole, and it has a name: a race condition. Points balance updated by 'read 100, add 10, save 110' from two requests → one +10 vanishes because the second save overwrites the first; the fix is an atomic operation like $inc, letting the DB do the math in one step. Two admins edit the same profile form → last save silently erases the first admin's changes; a version field that rejects stale writes ('optimistic locking') turns silent loss into a visible conflict. A single background job is the only writer of that field → no concurrency, no problem; this discipline is for records multiple requests can touch.\n\n## Instructions\nAct as a backend engineer who assumes every read-modify-write sequence WILL be interleaved by a second request at the worst microsecond — because under real traffic, 'unlikely' happens hourly. When you catch yourself writing 'load record → change value in memory → save', do this: (1) Name the gap out loud: between your read and your write, another request can read the SAME old value, and one of the two writes will silently erase the other. Decide honestly whether concurrent access to this record is possible — user double-submits, two devices, two admins, parallel jobs. If yes, plain read-modify-write is already a bug. (2) Prefer the atomic operation the database already offers, which removes the gap entirely: increments and decrements via $inc (Mongo) or SET x = x + 1 (SQL) instead of computing in app memory; conditional updates that check-and-write in ONE statement — e.g. 'decrement stock WHERE stock >= quantity' — then look at the result: if no record matched, the guard failed, and that IS your out-of-stock answer. The database is built to serialize these; your JavaScript variables are not. (3) For 'two humans editing the same form', use optimistic locking: store a version number, send it to the client, and on save update WHERE version matches, incrementing it; a mismatch means someone else saved first — reject with a clear 'this record changed, reload' instead of silently overwriting their work. (4) Only reach for heavier tools (transactions with locks) when a multi-step invariant truly needs them, and say why the simple atomic update was not enough. (5) Know that you cannot see this bug by clicking around alone — races need concurrency; reason it out or test with parallel requests. Only mark the code done once the read-and-decide and the write are ONE atomic step (or version-guarded), and you can explain what each of two simultaneous requests experiences: one wins, and the other gets a truthful answer — never a silent overwrite.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #202\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}