API Fundamentals Course
API Fundamentals
/
Advanced

Idempotency

Definition

A mathematical and computer science property where an operation can be applied multiple times without changing the result beyond the initial application.

Explain Like I'm New

If you hit an elevator button 1 time, it comes to your floor. If you smash the elevator button 100 times in a row, it still just comes to your floor. The button is Idempotent. The end result is exactly the same no matter how many times you pressed it.

Real World Example

Stripe Payments. If a user clicks 'Pay', but their WiFi drops, they panic and click 'Pay' again. If the `POST /charge` API is NOT idempotent, the user gets charged $100 twice. If the API IS idempotent, the server recognizes the duplicate request and only charges them once.

Common Use Cases

  • •Payment gateways
  • •Distributed retries
  • •Network reliability

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • Is the HTTP `DELETE` method intended to be Idempotent?

intermediate

  • How do payment APIs implement Idempotency on `POST` requests (which are normally not idempotent)?

Flash Cards

Question

Is DELETE idempotent?

Click to reveal answer
Answer

Yes. If you say `DELETE /users/5`, User 5 is destroyed. If you send `DELETE /users/5` again, User 5 is still destroyed. The database state remains the same.

Question

How to make POST idempotent?

Click to reveal answer
Answer

By using an `Idempotency-Key` header. The frontend generates a unique random ID (UUID) for the specific checkout attempt. If the user clicks 'Pay' twice, both requests have the exact same Key. The backend sees the duplicate Key, processes the first one, and ignores the second one.