Senior
What is idempotency? Can you explain with examples of HTTP methods?
sobes.tech AI
Answer from AI
Idempotency is a property of an operation where performing it multiple times yields the same result as performing it once.
In the context of HTTP methods:
- GET — is idempotent, as a request to retrieve a resource does not change the server's state, and repeated requests return the same result.
- PUT — is idempotent, because updating a resource with the same data multiple times results in the same state.
- DELETE — is idempotent, as deleting a resource multiple times will not change the result after the first deletion (the resource is already deleted).
- POST — is not idempotent, as each request usually creates a new resource or changes the server's state, and repeated requests can lead to different results (e.g., multiple records created).
Example:
PUT /users/123
{
"name": "Ivan"
}
If this request is performed multiple times, the user with id 123 will have the name "Ivan" — the state does not change after the first request.
Thus, idempotency is important for the reliability and predictability of APIs, especially when repeated requests are made due to network failures.