Scrivio Interview library
DesignAdvanced7 min read · updated 13 November 2026

An order workflow calls a legacy shipping partner's SOAP API to create shipments. The API accepts no idempotency key and timeouts are common. Last month, retries after timeouts created 230 duplicate shipments.

Design the integration so that retries are safe even though the partner API is not idempotent.

Persist the intent before calling the partner: a shipment request record per order line with a unique id and a state machine (PENDING, SENT, CONFIRMED, UNKNOWN), and send that id in a field the partner stores and can search by. On a timeout, mark the record UNKNOWN and look it up at the partner before any retry; if no lookup exists, either retry and automatically cancel the rare duplicate or hold UNKNOWN records for reconciliation, but never retry blindly. Allow only one call in flight per order, reconcile daily against the partner's shipment list, and ask for idempotency keys in the next contract.

Why this question is asked

Most integration advice says "use idempotency keys", but legacy partners often offer none, and their APIs time out regularly. A timeout is the hardest outcome in a distributed call, because we do not know whether the shipment was created. Interviewers ask this because it tests whether we can make retries safe by design when the other side will not help. In this answer, we will see why naive retries created 230 duplicates, design a state machine that records intent, use our own reference to check before retrying, decide what to do when no lookup exists, prevent concurrent attempts, and add reconciliation.

1Why Timeouts Create Duplicates

When a call to the shipping API times out, there are three possibilities: the request never reached the partner, it reached them and failed, or it reached them, the shipment was created, and the response was lost. From our side, all three look the same. The old code assumed the first and retried, so every time the third case happened, a second shipment was created. With common timeouts, that produced 230 duplicates in a month, each one a parcel shipped twice or a manual cancellation.

Since the partner API is not idempotent, we cannot make the call itself safe to repeat. What we can do is make sure we never repeat it without first finding out whether the earlier attempt succeeded.

2Record the Intent Before Calling

Before calling the partner, we write a shipment request row with its own unique id and a state. The row is created in the same transaction as the order line becoming ready to ship, so it cannot be lost or duplicated: a unique constraint on order_line_id ensures there is exactly one request per line.

SQL
CREATE TABLE shipment_request (
id uuid PRIMARY KEY, -- our reference, also sent to the partner
order_line_id bigint NOT NULL UNIQUE, -- one request per order line
state text NOT NULL, -- PENDING, SENT, CONFIRMED, UNKNOWN, FAILED
partner_ship_id text, -- filled once the partner confirms
attempts int NOT NULL DEFAULT 0,
updated_at timestamptz NOT NULL DEFAULT now()
);
Step throughShipment request lifecycle
  1. PENDINGintent stored with our reference
  2. SENTSOAP call in flight, attempts + 1
  3. Partner answered?
    YesCONFIRMED with partner idNoTimeout: move to UNKNOWN
  4. UNKNOWN: found by our reference?
    YesCONFIRMED, no retryNoSafe to resend: back to SENT
  5. Daily reconciliationcatches anything still UNKNOWN

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

The state machine tells us at any moment what we know. PENDING means we have not called yet. SENT means a call is in progress. CONFIRMED means the partner returned a shipment id. UNKNOWN means a call timed out and we do not know the outcome. FAILED means the partner clearly rejected it, for example with a validation error, which is safe to fix and resend.

3Send Our Reference and Check Before Retrying

The key move is to send our request id in a field the partner stores and lets us search by. Most shipping APIs have one: a customer reference, an order number or a consignment reference. On a timeout, the record moves to UNKNOWN, and before any retry we query the partner by that reference. If a shipment with our reference exists, we record its id and move to CONFIRMED without creating anything. Only if the partner confirms that no such shipment exists do we resend.

The lookup should wait a little after the timeout, for example 30 seconds, because a slow partner may still be processing the original request, and a lookup that runs too early could report "not found" just before the shipment appears. Using a reference that is unique per order line, not per attempt, means that even a duplicate that slips through can be recognised later as belonging to the same order line.

4When the Partner Offers No Lookup

Some legacy APIs have no search at all. Then we must choose, and the choice is a business one. One option is to retry, accept that occasionally a duplicate shipment is created, and cancel it automatically: since we sent our reference on both, the daily report or the partner's response shows two shipments for one reference, and we cancel the second through their API. The other option is to hold UNKNOWN records and resolve them against the partner's daily shipment report before acting, which avoids duplicates but delays some shipments by up to a day.

Which one fits depends on what a duplicate costs compared with a delay. For cheap items with fast cancellation, retry and cancel is fine; for expensive freight, holding is better. What we never do again is retry blindly with no way to detect the duplicate.

5One Call in Flight per Order

Duplicates also come from concurrency: a retry job and a user clicking "ship again" can both call the partner for the same order line. We allow only one call in flight per order. The simplest way is to claim the row with a conditional update before calling, so that only one worker can move it from PENDING or UNKNOWN to SENT.

SQL
UPDATE shipment_request SET state = 'SENT', attempts = attempts + 1, updated_at = now()
WHERE id = 'b7e1...' AND state IN ('PENDING', 'UNKNOWN') AND updated_at < now() - interval '30 seconds'
RETURNING id; -- 1 row: we own this attempt; 0 rows: someone else is calling, or it is confirmed

Notice that UNKNOWN records only get here after the lookup in step 2 has found nothing. A keyed queue, where all messages for one order go to one consumer, achieves the same thing for asynchronous workers.

6Reconcile and Fix the Contract

Finally, a daily reconciliation compares our CONFIRMED and UNKNOWN records with the partner's shipment list for the day. Two partner shipments with the same reference are duplicates and get cancelled through the partner's API; partner shipments we have no record of point to a bug; UNKNOWN records that do appear in the list become CONFIRMED. A metric for duplicates found per day shows whether the design is working, and should drop from 230 a month to near zero.

The lasting fix is commercial: in the next contract revision we ask the partner to support an idempotency key, or at least a search by reference with a guaranteed delay. Bringing the reconciliation numbers to that conversation makes the case concrete.

7Follow-up questions

How would you report the remaining risk to the business?

In business terms, with numbers. I would say how many shipment calls we make a month, how many time out, and how many of those we can now resolve automatically by reference lookup, and then state the residual risk plainly, for example that without a partner lookup we expect a handful of duplicate shipments a month, each cancelled within a day, at an estimated cost per case. I would present the options, retry and cancel versus hold for a day, with what each costs in duplicates and delays, recommend one, and say what would remove the risk entirely: an idempotency key in the partner contract. That lets the business choose the trade-off and gives them a reason to push the partner.

Why not just increase the timeout?

A longer timeout reduces how often we are left in doubt, but some calls will always time out, and very long timeouts tie up threads and slow the workflow. It is a useful tuning step, not a solution.

8Conclusion

In this answer, we made retries safe against a non-idempotent SOAP API. We persisted each shipment request with our own unique reference and a state machine before calling, moved timed-out calls to UNKNOWN and looked them up by reference before any retry, chose between retry-and-cancel and hold-for-reconciliation when no lookup exists, allowed only one call in flight per order line with a conditional claim, and reconciled daily against the partner's list while pushing for idempotency keys in the next contract.

What to say in the interviewAbout 100 seconds aloud

Open with

The duplicates come from treating a timeout as a failure. A timeout means we don't know: the shipment may well have been created and only the response lost, so a blind retry creates a second one.

Then, if they want more

I would first persist intent: before calling, write a shipment request row per order line with our own unique id, a unique constraint on the order line, and a state machine of PENDING, SENT, CONFIRMED and UNKNOWN. We send our id in a field the partner stores and can search by, like the customer reference.

When a call times out, the record goes to UNKNOWN, and before any retry we wait briefly and query the partner by that reference. If the shipment exists, we record it as confirmed; only if it definitely doesn't do we resend.

If the partner has no lookup, it's a business decision: retry and automatically cancel the rare duplicate, which we can spot because both carry our reference, or hold UNKNOWN records and resolve them against the partner's daily report. Either way we never retry blindly.

I'd also allow only one call in flight per order, by claiming the row with a conditional update from PENDING or UNKNOWN to SENT, or by a keyed queue, so concurrent retries can't overlap.

Finally, a daily reconciliation against the partner's shipment list cancels duplicates through their API and tracks the rate, and I'd ask for idempotency keys in the next contract revision, using those numbers to make the case.

About this answer

Last updated
13 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 official documentation for the version you use. 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 "Design the integration so that retries are safe even though the partner API is not...": https://scrivio-interview-library.netlify.app/architecture/distributed-systems/9ac8e088d7/