2.3 Idempotency Flashcards
What is idempotency?
Idempotency essentially means that the effect of a successfully performed request on a server resource is independent of the number of times it is executed.
The property of an operation that ensures performing the same action multiple times produces the same outcome as doing it once.
Idempotent HTTP methods include GET, PUT, DELETE, HEAD, OPTIONS and TRACE and by utilizing them, developers are able to take care of retries and errors to ensure that the APIs being created do not fluctuate in performance and are consistent to use at any one time.
Regardless of whether you want to provide users with the possibility of changing their profiles, cancel orders, or delete records, you can use idempotent APIs to guarantee the success and consistency of your actions.
How does idempotency apply to APIs?
A client can send the same request multiple times without causing unintended consequences, such as duplicate entries or repeated side effects.
What happens if a user experiences a timeout during an online payment API call?
The payment API is called again as part of the retry mechanism, potentially leading to multiple charges without idempotency.
What is the impact of idempotency on user account registrations?
Without idempotency, duplicate user accounts might be created if the registration form is submitted multiple times.
Why is idempotency critical for reliability and consistency?
It prevents undesired duplication or data corruption when clients retry requests due to network issues.
What is an Idempotent REST API?
An API that ensures the same request made multiple times has the same impact as making it just once.
What are safe methods in REST APIs?
Methods that do not change server state and are non-intrusive, primarily used for retrieving information.
List examples of safe methods.
If we follow the REST principles in designing our APIs, we will have automatically idempotent REST APIs for GET, PUT, DELETE, HEAD, OPTIONS, and TRACE methods. Only POST and PATCH APIs will not be idempotent.
POST and PATCH are NOT idempotent.
GET, PUT, DELETE, HEAD, OPTIONS and TRACE are idempotent.
- GET
- HEAD
- OPTIONS
- TRACE
What does the GET method do?
Retrieves data from the resource without making any changes to its contents.
What is the purpose of the HEAD method?
Returns only the headers of a resource, used to query metadata without downloading content.
What does the OPTIONS method do?
Gets the supported HTTP verbs for a given URL.
What is the DELETE method used for?
Cancels the resource at the specified URL.
What are idempotent methods?
Methods where performing the same action consecutively produces the same result as only one action.
What does the PUT method do?
Inserts the contents of the request payload into the field of the target URL.
How do idempotency keys work?
They allow non-idempotent methods, like POST, to behave idempotently by identifying and discarding duplicate requests.
What is the function of the POST method?
Used to create resources or perform operations that cannot be considered idempotent.
What is application-level idempotency?
Creating localized code logic within the application to maintain idempotent qualities across different HTTP methods.
Why are idempotent APIs important?
They ensure reliability, consistency, predictability, robustness, simplified client logic, and improved user experience.
What reliability do idempotent APIs provide?
Repeated requests do not cause different responses, aiding stability in distributed systems.
How do idempotent APIs ensure consistency?
They do not alter the state of the server, avoiding undesired impacts on data.
What advantage do idempotent APIs offer developers?
They simplify error correction and minimize bugs in client applications.
What is the benefit of idempotent APIs for user experience?
They guarantee operations complete regardless of network conditions or retries.
What are some idempotent HTTP methods?
2.2. HTTP GET, HEAD, OPTIONS and TRACE
GET, HEAD, OPTIONS and TRACE methods NEVER change the resource state on the server. They are purely for retrieving the resource representation or metadata at that point in time.
So invoking multiple requests will not have any write operation on the server, so GET, HEAD, OPTIONS, and TRACE are idempotent.
2.3. HTTP PUT
Generally – not necessarily – PUT APIs are used to update the resource state. If you execute a PUT API N times, the very first request will update the resource; the other N-1 requests will just overwrite the same resource state again and again – effectively not changing anything.
Hence, PUT is idempotent.
2.4 HTTP DELETE
2.4.1. Delete with the resource identifier
When you execute N similar DELETE requests, the first request will delete the resource and the response will be 200 (OK) or 204 (No Content).
Other N-1 requests will return 404 (Not Found).
Clearly, the response is different from the first request, but there is no change of state for any resource on the server-side because the original resource is already deleted.
So, DELETE is idempotent.
- GET
- PUT
- DELETE
- HEAD
- OPTIONS
- TRACE
What is a consequence of non-idempotent methods?
They can lead to redundancy and negative outcomes if retried without proper handling.