Make a Purchase API Call Safe to Retry With Idempotency Keys
Last updated: November 29, 2026
Quick Overview
A client charges a customer through a remote payment API, and timeouts sometimes lead to the customer being charged twice when the client retries. Explain the failure and implement a retry policy and a server-side mechanism that make retries safe.
Microsoft
November 29, 20260
0
0 solved
A client charges a customer through a remote payment API, and timeouts sometimes lead to the customer being charged twice when the client retries. Explain the failure and implement a retry policy and a server-side mechanism that make retries safe.
Microsoft Store, Xbox purchases, and Azure resource creation all face the same distributed systems problem: a timeout does not tell you whether the operation happened. Interviewers check that you understand at-least-once delivery, idempotency keys, and backoff with jitter, which the Azure SDK retry policies implement.
What the Interviewer Expects
- Explains that a timeout is ambiguous: the request may have succeeded
- Introduces a client-generated idempotency key reused across attempts
- Stores the key and result atomically with the side effect on the server
- Retries only transient errors, with capped exponential backoff and jitter
- Bounds total attempts or total time and surfaces final failure clearly
- Discusses key expiry and concurrent duplicate requests
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- Two retries with the same key arrive at the same time on different servers: what happens?
- How long should the server keep idempotency keys, and what if a retry arrives after expiry?
- Why does jitter matter when thousands of clients fail at the same moment?
- Which HTTP status codes would you treat as retryable?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
A charge must happen at most once per user intent, even if the client retries after timeouts, connection resets, or 503s. Non-transient errors (declined card) must not be retried. Retries must not sta...
High-Level Design
The double charge happens because the first request succeeded on the server but the response was lost; the client saw a timeout and sent a brand new request. The network gives at-least-once delivery, ...