Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

Define the API guideline for HTTP method use and idempotency across your company's services, including how clients safely retry POST requests.

The guideline maps each method to one purpose: GET reads, POST creates or runs commands, PUT replaces fully, PATCH changes part in a declared patch format, DELETE removes. PUT and DELETE must be truly idempotent, including DELETE on an already deleted resource, and every POST with side effects such as a payment requires an Idempotency-Key whose result the server stores for a retention period. Clients get explicit retry rules, which methods and status codes they may retry and with what backoff, and OPTIONS, HEAD and CORS behaviour is documented and covered by contract tests.

Background to this question

Most outages caused by retries are not exotic: a payment charged twice because a timeout was retried, an order created twice because a mobile app resent a POST, a DELETE that fails on the second attempt and leaves a client in an error loop. A company-wide guideline turns these into solved problems, because every service behaves the same way and every client can follow one set of rules. In this answer, we will write the method rules, make PUT and DELETE idempotent in practice, design idempotency keys for POST, define client retry rules, and decide how to test all of it.

1One Meaning per Method

The guideline starts by giving each HTTP method one job, so a reader can predict an endpoint's behaviour from its method alone. GET reads and never changes business state. POST creates a resource in a collection, as in POST /orders, or runs a command that is not a simple state change, as in POST /orders/42/refunds. PUT replaces the whole resource with the representation sent, so missing fields are reset. PATCH changes part of a resource and must declare its format: JSON Merge Patch (application/merge-patch+json) for simple field updates, or JSON Patch (application/json-patch+json) when operations such as removing an array element are needed. DELETE removes the resource.

Each rule also fixes the expected status codes, so clients do not guess: 201 Created with a Location header for creation, 200 or 204 for updates, 204 for deletion, 409 for a conflict with the current state, and 412 when an If-Match precondition fails.

Method rules in the guideline
MethodPurposeIdempotentRetry by client
GET, HEADreadyesyes
PUTfull replacementyes, must beyes
PATCHpartial change, declared formatonly if absoluteonly with If-Match
DELETEremoveyes, must beyes
POSTcreate or commandnoonly with Idempotency-Key

2Making PUT and DELETE Truly Idempotent

The protocol says PUT and DELETE are idempotent, but our code decides whether they really are. A PUT that also appends an audit entry visible to the client, or sends an email each time, has a different effect when repeated. The guideline says: side effects of a PUT must depend only on the change in state, so a repeat that changes nothing triggers nothing.

DELETE needs one explicit rule for the second call. If the first DELETE succeeded but the response was lost, the client retries and the resource is already gone. Both 204 and 404 keep the server state the same; what matters is that clients know which one to expect. Many companies choose 204 for an already deleted resource the caller owned, so a retry looks exactly like the first success, and 404 only for ids that never existed. Whatever we choose, it is written down once.

Java
@DeleteMapping("/orders/{id}")
ResponseEntity<Void> delete(@PathVariable long id) {
orders.deleteIfPresent(id); // no exception when it is already gone
return ResponseEntity.noContent().build();
}
// DELETE /orders/42 -> 204
// DELETE /orders/42 (retry) -> 204, same effect, same answer

3Idempotency Keys for POST

POST cannot be made idempotent by the method itself, so the guideline adds a mechanism. For POST operations with side effects, such as payments, orders and outgoing messages, clients must send an Idempotency-Key header with a fresh random value, for example a UUID, per logical operation, and reuse the same value on every retry of that operation.

The server keeps a shared table of keys. When a request arrives, it inserts the key with a hash of the request body and a status of in progress; a unique constraint guarantees only one instance wins. When the operation finishes, it stores the status code and response body. A retry with the same key and body gets the stored response replayed. A retry while the first is still running gets 409 so two copies never run in parallel. The same key with a different body gets 422, because that is a client bug. Keys expire after a fixed retention period, for example 24 hours, which the guideline states so clients know how long retries stay safe.

Step throughHandling a POST with an Idempotency-Key
  1. Receive POST with keyhash the request body
  2. Key already stored?
    YesCompare stored hashNoInsert key, run operation
  3. Same body hash?
    YesReplay or 409 if still runningNo422: key reused with new body
  4. Store status and responsekept for 24 hours

4 steps in order. Play them, or step through them one at a time.

4Retry Rules for Clients

Servers being idempotent is only half the guideline; clients need to know when to retry. The rule I would write: retry only idempotent requests, meaning GET, HEAD, PUT, DELETE, and POST with an Idempotency-Key. Retry only on errors that mean try again later: connection failures and timeouts, 502, 503 and 504, and 429 Too Many Requests while honouring Retry-After. Never retry other 4xx responses such as 400, 401, 404, 409 or 422, because the same request will fail the same way.

Retries use exponential backoff with jitter, such as waiting about 200 ms, 400 ms and 800 ms with a random spread, and stop after 3 attempts or a total time budget. Jitter matters because without it, thousands of clients that failed at the same moment all retry at the same moment and knock the service over again. The shared HTTP client configuration, for example a Resilience4j retry in an internal starter, implements these rules so teams do not hand-code them.

5Documenting and Testing OPTIONS, HEAD and CORS

Spring MVC answers HEAD and OPTIONS automatically, and the guideline should say we rely on that and never map business behaviour to them. HEAD must return the same headers as GET. OPTIONS returns an Allow header. CORS rules, meaning which browser origins may call which APIs, are owned centrally and configured in the security chain, not per controller.

Contract tests make the guideline real. For each service, a test suite checks that a repeated PUT and DELETE return the same result, that a POST replayed with the same Idempotency-Key does not create a second resource, that an unsupported method returns 405 with Allow, that HEAD matches GET's headers, and that a preflight from an approved origin succeeds while one from another origin fails. A linter on the OpenAPI description checks that POST endpoints with side effects declare the Idempotency-Key header.

6Follow-up questions

Why not make clients generate ids and use PUT for creation instead?

That is a valid alternative: PUT /orders/{client-generated-id} is naturally idempotent, so no idempotency table is needed. It works well when clients can generate good unique ids and when creation is a pure state change. For commands such as charging a card, where the operation has external effects and a response worth replaying, an Idempotency-Key on POST is clearer.

Should PATCH requests be retried?

Only when the patch is absolute, such as setting a status, or when it carries If-Match with the ETag it was based on. A relative patch like incrementing a counter would apply twice. With If-Match, a retry after the first succeeded fails with 412, which tells the client to reload instead of applying twice.

Who owns the idempotency storage?

Each service that exposes such POST endpoints owns its own key table, inside the same database as the data it changes, so storing the key and the result can be done in the same transaction. A shared library provides the code, but not a shared central store, which would become a single point of failure.

7Conclusion

In this answer, we wrote an HTTP method guideline: one meaning per method with PATCH in a declared format, PUT and DELETE made truly idempotent including a fixed answer for deleting an already deleted resource, Idempotency-Key headers with stored results for POST operations with side effects, client retry rules by method and status code with exponential backoff and jitter, and documented OPTIONS, HEAD and CORS behaviour checked by contract tests.

What to say in the interviewAbout 80 seconds aloud

Open with

I would write the guideline in three parts. First, method semantics: GET reads, POST creates or runs a command, PUT fully replaces, PATCH partially updates with a declared format, JSON Merge Patch or JSON Patch, and DELETE removes, each with fixed status codes.

Then, if they want more

Second, idempotency. PUT and DELETE must be idempotent in practice, not just in theory, so a repeated PUT triggers no extra side effects, and a DELETE of something already deleted gives a defined answer, I would usually choose 204, so retries look like the first success. For POST with side effects like payments, clients must send an Idempotency-Key.

The service stores the key with a request hash in its own database under a unique constraint, replays the stored response on a retry, returns 409 while the first call is running and 422 if the key comes back with a different body, and keeps keys for something like 24 hours.

Third, client retry rules: only retry idempotent requests or keyed POSTs, only on timeouts, 502, 503, 504 and 429 with Retry-After, with exponential backoff, jitter and about three attempts, provided by a shared client library. Finally, OPTIONS, HEAD and CORS are documented and every service runs contract tests for all of this.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Define the API guideline for HTTP method use and idempotency across your company's...": https://scrivio-interview-library.netlify.app/spring-boot/rest-controllers/666887f0fa/