Api Design hard
Your payment API runs on 10 servers behind a load balancer. Two retries with the same Idempotency-Key hit different servers simultaneously. How do you prevent double-charging?
- A Utilize a centralized, distributed key-value store (like Redis or DynamoDB) using atomic, conditional writes (SETNX) to track and lock the Idempotency-Key across all regional server nodes.
- B Configure 'Session Affinity' at the load balancer level to ensure that retried requests originating from the same client IP are consistently routed to the identical application server instance.
- C Enforce a system-wide distributed lock manager by setting the primary database's isolation level to SERIALIZABLE, forcing every incoming request to be processed in a globally ordered sequence.
- D Rely on the client application to implement a locally-persisted request log and perform an internal deduplication check before attempting any secondary network retry for a failed transaction.
Answer: A. Utilize a centralized, distributed key-value store (like Redis or DynamoDB) using atomic, conditional writes (SETNX) to track and lock the Idempotency-Key across all regional server nodes.
In a distributed system, a request might be routed to any server. Local memory (or sticky sessions) is insufficient for idempotency because the first request might hit Server A (stored key there) and the retry might hit Server B (no key). A **centralized, atomic store** (like Redis with `SET NX` or DynamoDB with conditional writes) ensures that all servers share the same 'memory' of processed request keys, preventing race conditions and double-processing.
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?
- A client retries a failed 'POST /payments' request due to network timeout. The server receives the request twice. Without idempotency handling, what can happen?
- 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?