Skip to content
IT glossary
BackendUpdated December 3, 2026

Idempotency

Идемпотентность

An operation has this property if repeating it has the same effect as doing it once. For example, turning notifications off twice leaves them off; toggling them twice does not.

What idempotency means

An operation is idempotent if applying it again with the same inputs leaves the relevant state as it was after the first application. Setting a stock count to 10 is idempotent. Adding 10 to the count is not. This comparison assumes no unrelated update changes the stock count between attempts; idempotency does not solve conflicts between different operations.

In a backend system, the classic problem is the lost response. The client times out and cannot tell whether the server committed a change or never received the request. An idempotent operation lets the client retry without creating a second order, posting a second charge, or repeating another intended business effect.

What HTTP promises

GET, PUT, and DELETE are defined to be idempotent. That promise is about the intended effect on the server, not identical responses. DELETE /devices/42 might return 204 No Content on the first call and 404 Not Found on the second. The device is absent after either call. The server can still write a separate access log entry for every attempt.

POST and PATCH do not carry the same guarantee at the method level. A particular PATCH that sets enabled to false can be idempotent; one that toggles enabled is not. A POST endpoint can offer a retry guarantee of its own, often through an idempotency key.

Example: retrying order creation

A client sends POST /orders with an Idempotency-Key header. It chooses one key per intended order and reuses that key if the same request times out. The server scopes the key to the client, records the request parameters, and saves the outcome. If an identical retry arrives after the first attempt completes, the server can return the saved outcome instead of creating another order. If the same key arrives with a different payload, the server rejects it rather than treating it as a retry.

A lookup followed by an insert is not enough: two concurrent requests can both see that the key is missing. If the order and key live in one database, use a uniqueness constraint and commit the order and its recorded outcome in the same transaction. That also closes the gap where the order exists but a crash has erased any record of which request created it. Once the key expires, the service may no longer recognize the retry, so the retention period is part of the API contract.

Beyond HTTP: queues and interview questions

With at-least-once delivery, a message can reach a consumer again even after its first attempt successfully updated the database. For database-only work, the consumer can record the message ID and apply the business change in one transaction, then acknowledge delivery after commit. If the consumer calls an external payment service, that local transaction cannot protect the external charge. The payment service needs its own protection, such as accepting the same idempotency key on retries.

In interviews, expect to reason through a crash after commit or two simultaneous requests using the same key. Identify the effect that must not happen twice, then explain which system enforces that rule. Idempotency does not mean the handler runs only once; it means another run does not add another intended effect.