Skip to content
Preptima
hardScenarioDesignMidSeniorStaff

A user submits the form twice and you get two identical orders. Why did the check-then-insert not prevent it, and how do you fix it in the database?

6 min readUpdated 2026-08-15Target archetype: Big Tech, Enterprise Captive, Product Startup

Answer

Short answer

Two transactions can each run the SELECT before either commits, so neither sees the other and both inserts succeed. The fix is a serialisation point the database owns - a unique index on a natural or client-supplied key - plus a defined answer for whoever loses the race.

Was this answer helpful?

Likely follow-ups

  • The retry arrives while the first request is still in flight. What does the second connection do, and for how long?
  • What do you return to the second caller so a mobile client that lost the first response still gets the order id?
  • There is no natural key and the client sends no request id. What do you use as a serialisation point?
  • How long do you retain idempotency keys, and what breaks on the day you expire one?

Related questions

Further reading