Interview frequency: …
Short answer
Send multiple simultaneous requests with Promise.all to perform conflicting operations (such as booking the same seat or withdrawing a balance twice), and check that only one succeeds.
Explanation
Ordinary sequential tests do not expose race conditions. To test database locks or distributed transactions, send a batch of concurrent requests with Promise.all against the same resource. Verify that only one returns 200, the others fail with a 409 Conflict or 422, and the final balance is consistent and accurate.
Code example
const results = await Promise.all([
withdraw(userId, 100),
withdraw(userId, 100)
]);Common mistakes
- Running the concurrent operations in an ordinary for loop, which executes them sequentially and fails to expose race conditions.