QA Interview Hub · Knowledge base · Quizzes · Українська

QA Fundamentals › Manual API testing

What is idempotency and why test it?

An idempotent operation has the same server effect repeated as done once, so a retry creates no duplicate. Per RFC 9110, GET, PUT and DELETE are idempotent and POST is not (nor PATCH, per RFC 5789). The effect counts, not the response: a repeated DELETE returning 404 is fine. POST in payments relies on an Idempotency-Key. Tests cover retries, the same key with another body, concurrency and database state.

QA Fundamentals · Manual API testing

A user pressed "Pay" twice. How many debits will there be?

A client app sent a payment, the network flickered, no response. The user presses "Pay" again, and the mobile client automatically repeats the request after a timeout. The server may have processed the first request completely, and only the response was lost. Without protection this is two debits for one purchase. This lesson is about what idempotency is, which HTTP methods have it by definition, how it is provided for POST and what exactly to check.

How it works and why. The HTTP specification (RFC 9110) defines two different concepts. A method is safe if it does not imply changing server state: GET, HEAD, OPTIONS. A method is idempotent if the effect on the server of several identical requests is the same as of one: PUT and DELETE, as well as all safe methods. POST is not idempotent: two identical POSTs create two resources. PATCH does not guarantee idempotency either: "set status to paid" can be repeated, but "increase the balance by 10" cannot. Importantly, idempotency concerns the effect on the server, not the response: a repeated DELETE may return 404 instead of 204, and that is fine, the resource is already gone.

Why it matters. A client cannot know whether a request arrived when no response came. So network libraries, gateways and users themselves repeat requests. For idempotent methods a retry is safe by default. For payments, order creation and sending emails it has to be made safe: the client adds a unique key to the POST (the Idempotency-Key header, a common practice described by an IETF draft), and the server remembers the pair "key → result". A repeated request with the same key does not perform the operation but returns the stored response.

A retry after a lost response

ClientServerPOST, key Kdebit (once)response lostPOST, key K (retry)key K is knownthe same response
Figure 1. The server performed the debit once. For the retry with the same key it answered with the stored result.

Example: from two debits to one. Without a key, a repeated POST /payments created two records, and a database query showed it: two rows with the same amount and account. After the team added key support, the check looks like this:

POST /api/payments HTTP/1.1
Idempotency-Key: 7c1e4b5a-3f0d-4a39-9b1e-2d6f0a8c11aa
Content-Type: application/json

{"account": "acc-1", "amount": 250.00, "currency": "UAH"}

HTTP/1.1 201 Created
{"id": "p-9001", "status": "pending"}

# a retry with the same key and the same body
HTTP/1.1 201 Created
{"id": "p-9001", "status": "pending"}

# the same key, but amount = 999.00
HTTP/1.1 4xx   (we expect a refusal, not a second debit)

How to read it. The first and second requests return the same id, so the payment was created once. The third check is the least obvious: the same key with a different body means the client reused a key by mistake, and the server must refuse with an explicit 4xx error rather than silently perform a second operation or return the old response for a different body. The exact code depends on the API (the IETF draft suggests 422), so take it from the specification. For the final conclusion we do not rely on the response: the database must hold one row with this key, and the balance must be debited once.

The same retry with and without a key

No key

Two debits

Each POST creates a new payment. A double click or a retry after a timeout produces a duplicate, and the money is debited twice.

With a key

One debit

The server recognises the key and returns the same result. There is no duplicate even if the client retried several times.

Figure 2. A key turns a dangerous retry into a safe one. This is confirmed by the state in the database, not only by the response.

Limits and alternatives. A key does not solve everything. Key scope: it is usually bound to a client or user, otherwise a coincidence with someone else's key would return someone else's response. Keys do not live forever, they get an expiry (say, a day), and after it a retry will be executed again: worth knowing and checking. The hardest check is concurrent requests with the same key: if the server first checks and then writes, two parallel requests may both consider the key new. Reliable protection lives at the database level (a unique constraint on the key) or in locking, and QA checks it by sending two requests simultaneously. There are other approaches too: natural keys (a unique order number), resource versioning (If-Match with an ETag) and PUT instead of POST when the client sets the identifier. Idempotency is worth having in a message queue as well: a consumer may receive one event twice.

Common mistakes.

  • Thinking idempotent means "returns the same response": it is about the effect, not the response. A repeated DELETE with 404 does not break the rule.
  • Checking only the response to the retry: it may be right while two records sit in the data. Look at the state.
  • Not checking the same key with a different body: a client mistake reaches production silently.
  • Testing the retry only sequentially: the race of two simultaneous requests breaks exactly where a sequential test sees nothing.
  • Treating PUT and PATCH alike: PUT replaces the resource completely, while PATCH depends on how the server implemented the change.
  • Forgetting a GET with side effects: a method that should be safe changes data, and every page prefetch or crawler becomes an operation trigger.

Check yourself

DELETE /orders/42 returns 204 the first time and 404 the second. Is that a violation of idempotency?

Answer

No. Idempotency is about the effect on the server: after the first and after the second request the state is the same, order 42 does not exist. A difference in the response does not violate the definition. It would be a bug if the second DELETE removed something else or returned 500.

Two parallel POST /payments requests with the same Idempotency-Key created two payments. Where is the defect most likely and how do you confirm it?

Answer

The server first checks whether the key is known and then writes, without atomicity: both requests passed the check before the first one stored the key. To confirm: send two requests at once and run an SQL check for the number of rows with this key. The fix is at the database level: a unique constraint on the key or locking.

In an interview. Junior: An idempotent operation gives the same effect when repeated. GET, PUT and DELETE are idempotent, POST is not. I check that a repeated request, for example after a double click or a retry after a timeout, does not create a duplicate. Middle/Senior: I distinguish safe from idempotent and look at the effect, not the response. For POST in payments I check Idempotency-Key: a retry with the same body gives the same response and one record in the database, the same key with a different body is rejected, and two simultaneous requests do not create a duplicate. I also ask about the scope and the lifetime of a key. I confirm with the state in the database and with reconciliation, because the API response may be right while a duplicate sits in the data.

More articles in this chapter