# HTTP methods: safe vs idempotent

> Explain safe and idempotent HTTP methods. Which methods are which, and why does it matter?

Canonical: https://www.internhack.in/learn/interview/api-design-interview/api-http-methods-idempotency
Last updated: 2026-11-28

Difficulty: Beginner

## Question

Explain safe and idempotent HTTP methods. Which methods are which, and why does it matter?

## Answer

Here is the everyday analogy. A safe operation is like looking at a menu: no matter how many times you look, nothing changes in the restaurant. An idempotent operation is like pressing the lift call button: pressing it five times has the same end effect as pressing it once. Neither is the same as pressing a button that orders a new coffee every time.

Formally, a method is safe if it does not modify server state as the client intends it. GET, HEAD, OPTIONS and TRACE are safe. A method is idempotent if making the same request N times leaves the server in the same state as making it once. GET, HEAD, OPTIONS, PUT and DELETE are idempotent. POST and PATCH are not guaranteed to be idempotent. Every safe method is idempotent, but not the other way around: DELETE and PUT change state, yet repeating them does not change the outcome further.

Important subtlety: idempotency is about server state, not about the response. The first DELETE /orders/42 might return 204 and the second 404, and it is still idempotent because the end state, order gone, is the same. Also, safe does not mean zero side effects like access logs or analytics counters; it means the client is not asking for a state change.

Why does this matter? Retries. Networks drop packets, load balancers time out, mobile clients switch from wifi to 4G. If a request times out, the client does not know whether the server processed it. For idempotent methods, HTTP clients, proxies and browsers can automatically retry without fear. For POST they cannot, and a blind retry might charge a customer twice. This is precisely why payment APIs add idempotency keys to POST.

Safe methods also enable caching and prefetching. Browsers and crawlers assume GET is harmless; that is why a GET /logout or GET /delete?id=5 link is a genuine security hazard, because a prefetcher or an image tag from another site could trigger it. This is one root of CSRF issues.

Practical implications for your code: implement PUT as full replace semantics so repeating it converges. Implement DELETE so that deleting an absent resource is either 404 or 204, decided consistently. Make GET handlers pure reads. Design POST creation endpoints that can accept an idempotency key or a natural unique key. And if you make PATCH idempotent, such as set status to shipped, document it, but avoid non-idempotent patch semantics like increment counter by one when you can use an absolute value instead.

A quick edge case that trips people up: is a GET that increments a view counter still safe? By spec intent, yes, because the client did not request the state change, but be careful that caches may serve the response without hitting your counter at all.

To summarise the table in your head: GET safe and idempotent; HEAD safe and idempotent; PUT idempotent; DELETE idempotent; POST neither; PATCH neither by default; OPTIONS safe and idempotent.

## Code examples

### Idempotent DELETE handler

```
app.delete('/v1/orders/:id', async (req, res) => {
  const deleted = await db.order.deleteMany({ where: { id: req.params.id, userId: req.user.id } });
  // Same end state whether or not it existed; pick one status and be consistent
  if (deleted.count === 0) return res.status(404).json({ title: 'Order not found' });
  res.status(204).end();
});
```

Repeated calls converge to the same state: the order does not exist. The status code may differ between first and later calls.

## Key points

- Safe means read-only intent; idempotent means repeat gives same final state.
- Idempotency describes state, not the response code.
- GET must never mutate state; prefetchers and CSRF make it dangerous.
- POST is neither safe nor idempotent, hence idempotency keys.
- Automatic client retries are only safe on idempotent methods.

## Common follow-up questions

- Is PATCH idempotent?
- Why should GET /logout be avoided?
- How would you make a POST create endpoint safe to retry?

## Interview tips

- Mention the timeout retry scenario; it shows you understand why the property exists.
- Point out that DELETE returning 404 on the second call is still idempotent.

## Concepts covered

HTTP methods, safe, idempotent, retries

Previous: [Resource naming and URL design conventions](https://www.internhack.in/learn/interview/api-design-interview/api-resource-naming) · Next: [PUT vs PATCH vs POST: when to use which](https://www.internhack.in/learn/interview/api-design-interview/api-put-vs-patch-vs-post) · [All of API Design](https://www.internhack.in/learn/interview/api-design-interview)

## More API Design questions

- [Choosing the right HTTP status codes](https://www.internhack.in/learn/interview/api-design-interview/api-status-codes)
- [Designing error responses with RFC 9457 problem details](https://www.internhack.in/learn/interview/api-design-interview/api-error-format-problem-details)
- [Pagination: offset vs cursor (keyset) trade-offs](https://www.internhack.in/learn/interview/api-design-interview/api-offset-vs-cursor-pagination)
- [Filtering, sorting, searching and field selection](https://www.internhack.in/learn/interview/api-design-interview/api-filtering-sorting-fields)
- [API versioning strategies and trade-offs](https://www.internhack.in/learn/interview/api-design-interview/api-versioning)
- [Idempotency keys for payments and retries](https://www.internhack.in/learn/interview/api-design-interview/api-idempotency-keys-payments)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [API Design](https://www.internhack.in/learn/interview/api-design-interview)
