Api Design easy
A client retries a failed 'POST /payments' request due to network timeout. The server receives the request twice. Without idempotency handling, what can happen?
- A The server automatically detecting the duplicate IP and rejecting the second request.
- B The server creating a second payment resource, resulting in a double-charge.
- C The database rolling back both transactions due to a primary key collision.
- D The load balancer caching the first response and returning it to the client.
Answer: B. The server creating a second payment resource, resulting in a double-charge.
The POST method is not idempotent by default, meaning multiple identical requests result in multiple resources being created. If a client retries a POST request (e.g., due to a timeout where the server actually processed the first request), it can lead to duplicate transactions. To prevent this, APIs often implement idempotency keys, allowing the server to recognize and deduplicate retries.
One system design question a day, with spaced repetition so the ones you miss come back.
Get the appRelated questions
- You're designing a REST API for a blog. Which endpoint best represents 'get all comments for post 123'?
- A client wants to update only the 'status' field of an order, leaving other fields unchanged. Which HTTP method is most appropriate?
- A client calls POST /users with valid data, and the user is created. What status code should the server return?
- An API returns 10,000 products. Returning all in one response is impractical. Which pagination approach is simple and stateless?
- Your API serves both mobile apps and third-party integrations. Which authentication method is most appropriate?
- Service A calls Service B, which is failing. Without protection, A keeps sending requests, cascading the failure. What pattern prevents this?