Scrivio Interview library
ScenarioAdvanced5 min read · updated 12 November 2026

Several teams retry calls between services with different policies, and a recent incident created duplicate shipments because the shipping service treats every POST as new. What cross-team standard would you set for idempotency?

The standard says every state-changing API documents whether and how it is idempotent (natural key, idempotency key or conditional request). Clients generate one key per business operation and reuse it on every retry and redelivery, servers deduplicate within a documented window, and event consumers deduplicate by event id. A shared library implements it and is tested with duplicate-delivery and chaos tests, and retry policies are aligned so only idempotent operations are retried, with limits and backoff.

Why this question is asked

Duplicate shipments are an expensive kind of bug: goods leave the warehouse twice, customers are confused and someone has to recover a parcel. The incident in the question is typical: the order service retried a timed-out call, as its policy allowed, and the shipping service created a second shipment, as its design allowed. Each team did something reasonable; the system failed between them. Interviewers ask this scenario to see whether a candidate fixes the system rather than one service. In this answer, we will replay the incident, set the rules for servers, clients and consumers, provide the shared tooling, and align retry policies.

1Replaying the Incident

The order service calls POST /shipments with a 2-second timeout and up to three retries. On a busy evening, the shipping service takes 2.5 seconds to respond because its database is slow. The order service times out and retries. The shipping service, which treats every POST as a new shipment, processes both requests. A queue redelivery later adds a third.

Step throughHow one order became two shipments
Order serviceShipping serviceWarehouse
POST /shipments (order 5512)
create shipment S-1
timeout after 2 s
retry POST /shipments (order 5512)
create shipment S-2
201 S-1 (too late, ignored)
201 S-2

7 messages in order. Play them, or step through them one at a time.

Both services followed their own rules; the contract between them said nothing about duplicates.

2Rule 1: Every State-Changing API Says How It Is Idempotent

The first rule is documentation that cannot be skipped. Every POST, PATCH or other state-changing operation states in its OpenAPI description whether it is idempotent and by which mechanism. There are three common mechanisms, and the standard explains when each fits.

A natural key uses a business identifier that is already unique for the operation: one shipment per order line, enforced by a unique constraint on order_line_id, so a duplicate request finds the existing shipment. An idempotency key is a client-generated token in a header, for operations that have no natural key, such as a payment. A conditional request uses ETags and If-Match, so a repeated update fails if the resource has changed in between. The API lint checks that each state-changing operation declares one of these, or explicitly says it is not idempotent and must not be retried.

Idempotency mechanisms in the standard
MechanismExampleFits when
Natural keyunique order_line_id per shipmenta business id identifies the operation
Idempotency keyIdempotency-Key: uuid per operationno natural key, e.g. payments
Conditional requestIf-Match: "v7" on PUT or PATCHupdates to an existing resource
Event id dedupprocessed_events(event_id) tablemessage consumers

3Rule 2: One Key per Business Operation, Reused Everywhere

A key only helps if the client keeps it stable. The rule is that the key belongs to the business operation, ship order line 5512-1, not to the HTTP attempt. So the client creates it once, stores it with the operation (for example in the order's outbox row), and sends the same key on every retry, after a restart, and when the operation is triggered again by a redelivered message. A common bug is generating a fresh UUID inside the retry loop, which makes every attempt look new.

Java
// wrong: a new key per attempt, so retries are not recognised
retry(() -> shipping.create(order, UUID.randomUUID().toString()));
// right: the key is derived from the business operation and stored with it
String key = "ship-" + order.id() + "-" + line.id(); // or a UUID saved in the outbox row
retry(() -> shipping.create(order, key)); // same key on every attempt and redelivery

Notice that a deterministic key derived from business ids also survives message redelivery without any extra storage, because the redelivered message carries the same ids.

4Rule 3: Servers and Consumers Deduplicate Within a Window

On the server side, the standard says a service must deduplicate within a documented window, at least 24 hours or longer than the longest client retry and redelivery period. Within the window, a repeated request returns the original result. Event consumers follow the same idea with the event's id: before acting on an event, the consumer records the id in a processed_events table in the same transaction as its own changes, and skips any event whose id is already there. Since message brokers deliver at least once, this is the only way to get an effect that happens exactly once.

5Rule 4: A Shared Library, Tested With Duplicates

Asking every team to implement this from scratch guarantees inconsistent results. The platform team provides a library: a Spring component that claims and replays idempotency keys, a consumer helper that deduplicates by event id, and client helpers that attach stable keys. It is tested the way failures actually happen: integration tests that send every request twice and in parallel, consumer tests that redeliver the same message, and chaos tests in a staging environment that inject timeouts and duplicate deliveries between real services, checking that the number of shipments equals the number of orders.

6Rule 5: Align Retry Policies

Finally, the retry side. The standard says clients retry only operations documented as idempotent, with a limited number of attempts (for example 3), exponential backoff with jitter, and an overall deadline. Timeouts are set so that the caller's timeout is longer than the callee's typical processing time, which reduces needless retries. Retries happen in one layer only; if the HTTP client, the service mesh and the application all retry three times, one failure can become 27 requests. Non-idempotent operations are not retried automatically at all; they go to a manual or compensating path.

7Follow-up questions

Why not just make the shipping service check for an existing shipment for the order?

That is the natural-key mechanism and is a good fix for this endpoint, but it only works when a business key exists and the check is enforced by a unique constraint rather than a read followed by an insert, which races under concurrency. The standard covers the other cases and makes the choice explicit for every API.

How long should the deduplication window be?

Longer than the longest time a duplicate can arrive: client retry periods, message broker redelivery and replay windows, and manual reprocessing. Twenty-four hours is a common minimum; consumers of replayable topics may need days.

Who owns the library?

A platform team owns and maintains it, but its rules are agreed with the service teams, and every team that adopts it contributes failure cases back as tests.

8Conclusion

In this answer, we turned a duplicate-shipment incident into a cross-team standard. Every state-changing API documents its idempotency mechanism, clients use one key per business operation across retries and redeliveries, servers deduplicate within a documented window and consumers by event id. A shared library tested with duplicate and chaos tests implements it, and aligned retry policies retry only idempotent operations, in one layer, with limits and backoff.

What to say in the interviewAbout 80 seconds aloud

Open with

I would treat the incident as a contract failure between teams rather than a bug in one service, and set a standard with a few rules. First, every state-changing API must document whether it is idempotent and how: a natural business key with a unique constraint, like one shipment per order line, an idempotency key header, or a conditional request with If-Match.

Then, if they want more

The API linter checks that this is declared. Second, clients generate one key per business operation, not per attempt, store it with the operation, and reuse it on every retry and when a message is redelivered; deriving it from business ids makes that easy.

Third, servers deduplicate within a documented window, at least 24 hours or longer than any retry or redelivery, returning the original result, and event consumers record processed event ids in the same transaction as their changes.

Fourth, a shared library implements key handling and consumer deduplication, and we test it with duplicate and parallel requests, redelivered messages and chaos tests that inject timeouts. Fifth, we align retry policies: only retry operations documented as idempotent, in one layer only, with a few attempts, backoff with jitter and an overall deadline.

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 "Several teams retry calls between services with different policies, and a recent...": https://scrivio-interview-library.netlify.app/spring-boot/rest-api-design/17168c4e40/