Back to Prompts
Coding Assistant

Protect a Shared Resource from Race Conditions

Start here. The most common race condition fix From the Race Conditions AI Coding Building Block.

Prompt
My [web app / API / backend] has a feature where users can [redeem a coupon / claim a reward / reserve a seat / purchase a limited item]. Right now, if two users hit this at the same time, they can both succeed even though only one should.

Handle race conditions for this feature:
- Use locks or atomic database operations so only one request can read-check-write at a time
- If a second request arrives while the first is processing, it should wait or get a clean "not available" response
- Make sure the fix works under real traffic, not just single-user testing
- Add clear error messages when a resource is no longer available

My stack: [your language and framework here, e.g. Node + PostgreSQL, Python + Django, Ruby on Rails]

I'm learning, so explain each part simply.
0 views 0 copies