Api Design hard
Your API creates an order (POST), which triggers async processing (inventory, payment). GET /orders/123 right after POST might return 'pending' even though payment already succeeded in the backend. How do you handle this?
- A Explicitly document the state-machine transitions and provide clear status codes, while offering a Server-Sent Events (SSE) or WebSocket stream for real-time state notifications.
- B Enforce a strictly synchronous execution model that blocks the initial POST request until the entire downstream transaction chain has been successfully committed and verified.
- C Temporarily suppress the visibility of the new resource until a background consensus check confirms that all regional replicas have achieved a uniform and consistent data state.
- D Implement a 'Optimistic Completion' strategy where the API returns a fake 'Completed' status to the client, while silently handling any asynchronous failures in a dead-letter queue.
Answer: A. Explicitly document the state-machine transitions and provide clear status codes, while offering a Server-Sent Events (SSE) or WebSocket stream for real-time state notifications.
In an eventually consistent system, there is a delay between a write and when it becomes visible to reads. The API should be transparent about this state (e.g., returning `status: 'pending'` or `202 Accepted`). Real-time updates can be provided via **Webhooks** or **Server-Sent Events (SSE)**, preventing the need for the client to poll aggressively.
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?