← Lessons

quiz vs the machine

Gold1420

System Design

Idempotent Order Creation

Making a retried order request create exactly one order.

4 min read · core · beat Gold to climb

The duplicate order problem

A buyer taps pay, the network drops the response, and the client retries. Without protection you create two orders and two charges for one intent.

Idempotency keys

  • The client generates a unique idempotency key for the checkout attempt and sends it with every retry.
  • The server stores the key with the result of the first successful creation.
  • A retry with the same key returns the stored result instead of creating again.

Storage and expiry

  • Keep an idempotency table mapping key to order id and response.
  • Use a unique constraint on the key so a concurrent duplicate fails fast.
  • Expire keys after the attempt window since the buyer will not retry forever.

Key idea

Attach an idempotency key to each order attempt and store the first result under a unique key, so any retry returns the same order instead of creating a duplicate.

Check yourself

Answer to earn rating on the learn ladder.

1. What does an idempotency key prevent?

2. Why put a unique constraint on the idempotency key?