# Idempotency

> An HTTP guarantee where making an identical request multiple times has the same effect as making it once.

Subject: Internet | Lesson 16 of 35 | 3 min read
Interactive lesson: https://codewithanimation.in/internet/idempotency
Previous: [RESTful Principles](https://codewithanimation.in/internet/restful-principles.md) | Next: [Same-Origin Policy](https://codewithanimation.in/internet/same-origin-policy.md)

## What it is

An HTTP method is **idempotent** if an identical request can be made once or several times in a row with the same effect while leaving the server in the same state.

In other words, a method is idempotent if it has no additional effect on the server's data if it is called more than once with the same input parameters.

## Why it matters

Idempotency is crucial for safe network retries (e.g., when dealing with connection drops, timeouts, or failures).

It prevents disastrous duplicate operations, such as avoiding double charges on a user's credit card, or creating duplicate records if a user impatiently clicks a 'Submit' button twice.

## How it works

By design, the HTTP standard categorizes its methods into idempotent and non-idempotent buckets.

**Idempotent methods:** `GET`, `HEAD`, `PUT`, `DELETE`, `OPTIONS`, `TRACE`.

**Non-idempotent methods:** `POST`, `PATCH` (PATCH can be idempotent depending on implementation, but is not guaranteed to be).

## Common mistake: Common mistake

Thinking `DELETE` isn't idempotent because it returns a `404 Not Found` the second time you run it. Even though the response status code changes from `200` to `404`, the ultimate state of the server is exactly the same (the resource does not exist). Thus, the method is fully idempotent.

## Tip: Pro tip

Use an **Idempotency-Key** header for non-idempotent operations like `POST`. Modern APIs (like Stripe) use this model: you send a unique UUID with your request. If the connection drops and you retry the `POST` with the same key, the server knows not to charge the card again and simply returns the original successful response.

## Try it: Try it

Compare a non-idempotent Coin Slot (`POST`) to an idempotent Pedestal (`PUT`). Notice how the server state changes with multiple taps!

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/idempotency

## Check yourself

### Question 1

Which of the following HTTP methods is NOT guaranteed to be idempotent by default?

- A. GET
- B. PUT
- C. DELETE
- D. POST

Correct answer: D

POST is designed to create a new resource or process data. Executing the same POST request multiple times will typically result in multiple new resources being created.

- Why not A: GET only retrieves data without changing server state, so multiple GETs are identical.
- Why not B: PUT replaces a resource completely. Multiple identical PUTs leave the same replaced resource state.
- Why not C: Deleting a resource once or multiple times leaves the resource in the same state-deleted.

### Question 2

Why is the DELETE method considered idempotent, even if a second identical request to the same URL returns a 404 Not Found?

- A. Because 404 is defined as a success status code in idempotent operations.
- B. Because idempotency only applies to methods that return a 200 OK status.
- C. Because the client caches the first response and ignores the 404.
- D. Because the final state of the server remains the same after the first request (the resource is gone).

Correct answer: D

Idempotency guarantees that the side effects on the server are the same for multiple identical requests. After the first DELETE, the resource is removed. Subsequent DELETE requests leave the resource removed, meaning the server state hasn't changed further.

- Why not A: 404 is a client error, not a success code.
- Why not B: Idempotency is defined by the server's state, not the HTTP response status code.
- Why not C: Caching is an entirely separate concept from idempotency.

### Question 3

How can a non-idempotent operation like POST be made safely retriable in payment APIs?

- A. By changing the HTTP method to GET when retrying the request.
- B. By relying on the browser to automatically prevent duplicate POST requests.
- C. By sending a DELETE request immediately before retrying the POST.
- D. By including a unique 'Idempotency-Key' in the request headers.

Correct answer: D

An Idempotency-Key (usually a UUID) allows the server to recognize duplicate requests (e.g., retries after a network timeout) and return the cached original response instead of executing the sensitive operation again.

- Why not A: You cannot use GET to perform state-changing operations like processing a payment.
- Why not B: Browsers do not automatically prevent duplicate POST requests upon retry without explicit implementation.
- Why not C: This would not prevent a duplicate operation if the first request succeeded but the response was lost in transit.

## Remember this

- Idempotency means multiple identical requests yield the same server state.
- GET, PUT, and DELETE are naturally idempotent.
- POST is non-idempotent and requires care (like Idempotency Keys) for retries.
