HTTP methods: safe vs idempotent

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

Common follow-up questions

Interview tips

Concepts covered

HTTP methods, safe, idempotent, retries

More API Design questions