Concurrency and races · Chapter 3 of 15
The lost update
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Two requests at once
- 3 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of Concurrency and races are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
repro.js: lose a credit on purpose
Write reproduce() in repro.js. Make a wallets table with createWalletDb from wallet-db.js, with ₹100 in one wallet (any id, say { priya: 100 }). Send two credits to that wallet at the same time with credit(db, id, amount) from wallet.js: 50 (the cashback) and 20 (the refund). Wait for both, and resolve to the wallet's balance in the table, db.balance(id).
The check runs your reproduce() twice: beside the original wallet.js it must resolve to 120 (the ₹50 of cashback lost), and beside a fixed wallet.js it must resolve to 170. Credits made one after another never race, so start both before waiting for either. No real waiting: the stand-in answers on the next turn of the event loop, and reproduce() must finish within 2 seconds.
- Task 2
wallet.js: every credit lands
Fix credit(db, userId, amount) in wallet.js so that no credit is lost when others land at the same time. Use compare-and-set: read the wallet with db.get (it gives { balance, version }), work out the new balance, and write it with db.setIf(userId, next, version), which writes only if the version is still the one you read and resolves to true or false. When it resolves to false, another write got in first: read again and try again, at most 5 tries in all, and then reject with an Error 'wallet <id>: too many conflicts'. Never use db.set.
credit resolves to the balance it wrote and logs wallet <id>: <+amount or -amount>, balance ₹<balance> once, when its write lands; keep that line as it is. A debit is a negative amount, the same way. The check uses its own table and wallets, sends two and then five credits at once, and gives you a table where every setIf loses.
- Task 3
wallet.test.js: a test that would have caught it
Write a vitest test file, wallet.test.js, that would have caught the lost update. The check runs your file three times: beside the original wallet.js, where at least one test must fail; beside the fixed wallet.js, where every test must pass; and beside a wrong fix that uses compare-and-set but gives up after one conflict, where at least one test must fail.
So use the stand-in from wallet-db.js, send two or more credits to one wallet at the same time, and expect the final balance to hold every one of them. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.js: lose a credit on purpose
- wallet.js: every credit lands
- wallet.test.js: a test that would have caught it
More in Two requests at once
Build The lost update yourself
Tapri's backend grew busy, and Kabir keeps handing you incidents that only happen when two things run at once. Each is a race interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Concurrency and races are free. This one comes with the all labs plan.