wasAsked.

Concurrency Handling — Race Condition Scenario: Can you give me a specific scenario where you handled concurrency — what was the race condition or resource contention you were protecting against?

Dodge Construction Network · Senior Developer · technical

Published

Asked in 2026 · System Design

Community answers

Posted by wasAsked editorial collection

Interviewers want a concrete scenario: what could go wrong, how you detected or anticipated it, and how you protected against it. Use a real example from your work. A typical strong answer: Scenario: overselling limited inventory "In our booking platform, each tour slot had limited seats. During promotions, many users tried to book the last seats at the same time. The original code read the available seat count, checked it in C#, then saved the reduced count: var slot = await db.Slots.FindAsync(id); if (slot.Available >= qty) { slot.Available -= qty; await db.SaveChangesAsync(); } Two requests could both read Available = 1, both pass the check, and both save — the lost update race condition — so we sold more seats than existed." How we fixed it 1. Atomic conditional update: UPDATE Slots SET Available = Available - @qty WHERE Id = @id AND Available >= @qty; If no rows were affected, the slot was full. In EF Core 7+ this can be done with ExecuteUpdateAsync and a Where condition, then checking the affected row count. 2. Optimistic concurrency on other fields of the booking record with a rowversion column, catching DbUpdateConcurrencyException and retrying or showing a friendly message. 3. Idempotency keys on the booking request, so double clicks and client retries did not create duplicate bookings. 4. Seat holds with expiry: seats were reserved for 10 minutes during payment and released by a background job if payment did not complete. How we verified it - An integration test that started 50 concurrent booking requests against a real SQL Server (Testcontainers) for a slot with 10 seats and asserted exactly 10 succeeded. - Monitoring for negative availability or more bookings than capacity, with an alert. Other good concurrency scenarios to mention if relevant - Duplicate message processing in queue consumers (fixed with an inbox table and unique constraints). - Two administrators editing the same record (optimistic concurrency with ETags and a merge UI). - Shared mutable state in a singleton service causing data from one request to leak into another (fixed by making the service stateless or using thread-safe structures). - Token refresh races where several requests refreshed at once (fixed with a SemaphoreSlim so only one refresh runs).

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics