Three defects show up repeatedly in systems that handle money, and all three share a property: the test suite is green.
A coupon with a hundred-use limit is redeemed a hundred and six times. An invoice sequence has a gap, or a duplicate. A seller balance is paid out twice.
Each has a unit test. Each test passes. And each bug is real.
Why the tests pass
Most test suites replace the database with an in-memory fake, for speed. An in-memory fake almost always serialises operations by construction — one call finishes before the next begins.
That is precisely the condition under which these bugs do not occur. The test is not wrong about its assertion; it is testing a world where the race cannot happen.
The same applies to a test that runs two operations "concurrently" by calling them one after another with an async keyword. Unless real parallel requests hit a real database, the interleaving that causes the defect never occurs.
What a real concurrency test looks like
Boot the actual application against an actual database — a container is fine, and cheap. Then:
- Arrange the state: a coupon with one use remaining, or a product with one unit in stock.
- Fire N genuinely parallel requests that all try to consume it.
- Assert on the aggregate outcome: exactly one succeeded, or the total consumed never exceeded the limit.
- Repeat. A race that appears one time in twenty is still a race, so run the test enough times to be meaningful.
The assertion is the important part. Do not assert which request won — that is legitimately nondeterministic. Assert the invariant that must hold regardless: the sum is correct.
Which paths deserve this
Not everything. The cost is real — these tests are slower and need infrastructure. Reserve them for code where a race costs money or credibility:
- Stock reservation — the classic overselling bug
- Coupon and voucher redemption — usage limits
- Gapless numbering — invoices, receipts, anything a tax authority reads
- Payouts and settlements — double payment
- Anything with a limit — quotas, capacity, seats
The fix these tests validate
Almost always the same shape: take a row lock inside an explicit transaction, so the second request waits and then sees the updated state. Not a lock in application memory — that only holds within one process, and production usually runs several.
In a marketplace we built, coupon redemption, seller payouts and gapless invoice numbering each take a row lock in an explicit transaction, and each is covered by a concurrency test that runs real parallel requests against a real PostgreSQL instance. That platform has 845 automated tests; these are among the few that need containers, and they are the ones that would have caught the defects that matter.
A standard worth adopting
One rule we hold to: a test earns its place only if it fails when the fix it guards is reverted. For concurrency tests this is easy to check — remove the lock, run the test, and watch it go red. If it stays green, the test is decoration.
A property-based bonus
For settlement in particular, one invariant is worth testing directly against arbitrary sequences of entries: the sum of seller balances plus commission must equal captured revenue. Generating random sequences and asserting that identity found two real defects for us before production.





Comments
No comments yet. Be the first.