Implement a robust REST API method
Design and Implement an Idempotent REST API for Resource Creation
Context
You are tasked with designing and implementing a create endpoint for a commerce-like service. For this problem, assume the resource being created is an Order, and clients will interact with the API via POST /v1/orders. The API must be robust, safe to retry, and free from race conditions.
Requirements
- Endpoint and Semantics:
- Define the REST endpoint for creating an order.
- Specify any required HTTP headers.
- Request Schema:
- Define the JSON request schema, including a sample request.
- Outline validation and sanitization rules for incoming data.
- Response Schema:
- Define success responses, including a sample response.
- Specify relevant HTTP headers (e.g.,
Locationheader for created resources).
- HTTP Status Codes:
- Enumerate appropriate success and error status codes and describe when each should be used.
- Idempotency:
- The API must support an
Idempotency-Keyheader. - Define the expected behavior for duplicate requests:
- Same
Idempotency-Keyand same request body. - Same
Idempotency-Keybut a different request body.
- Same
- The API must support an
- Error Handling and Logging:
- Define a consistent error response format.
- Describe structured logging practices, including the use of correlation IDs and PII redaction.
- Authentication and Authorization:
- Propose a suitable scheme (e.g., OAuth2/JWT) and specify required scopes or roles.
- Rate Limiting:
- Describe the chosen rate limiting strategy (algorithm, limits, relevant headers).
- Explain how rate limiting interacts with idempotent retries.
- Data Model:
- Outline the necessary database tables (e.g.,
orders,order_items, an idempotency store). - Specify key constraints and indexes for these tables.
- Outline the necessary database tables (e.g.,
- Concurrency:
- Explain how race conditions will be prevented.
- Describe how concurrent requests using the same
Idempotency-Keywill be handled.
- Testing:
- Describe the approach to unit and integration testing, specifically covering concurrency and failure scenarios.
Assumptions
Candidates should make minimal, explicit assumptions as needed and keep the design pragmatic and production-oriented.
AI interview prep
Core dilemma
Ensuring strict 'exactly-once' processing semantics for resource creation in a distributed, high-concurrency environment, while maintaining API responsiveness, resilience, and preventing race conditions.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement a robust REST API method
Design and Implement an Idempotent REST API for Resource Creation
Context
You are tasked with designing and implementing a create endpoint for a commerce-like service. For this problem, assume the resource being created is an Order, and clients will interact with the API via POST /v1/orders. The API must be robust, safe to retry, and free from race conditions.
Requirements
- Endpoint and Semantics:
- Define the REST endpoint for creating an order.
- Specify any required HTTP headers.
- Request Schema:
- Define the JSON request schema, including a sample request.
- Outline validation and sanitization rules for incoming data.
- Response Schema:
- Define success responses, including a sample response.
- Specify relevant HTTP headers (e.g.,
Locationheader for created resources).
- HTTP Status Codes:
- Enumerate appropriate success and error status codes and describe when each should be used.
- Idempotency:
- The API must support an
Idempotency-Keyheader. - Define the expected behavior for duplicate requests:
- Same
Idempotency-Keyand same request body. - Same
Idempotency-Keybut a different request body.
- Same
- The API must support an
- Error Handling and Logging:
- Define a consistent error response format.
- Describe structured logging practices, including the use of correlation IDs and PII redaction.
- Authentication and Authorization:
- Propose a suitable scheme (e.g., OAuth2/JWT) and specify required scopes or roles.
- Rate Limiting:
- Describe the chosen rate limiting strategy (algorithm, limits, relevant headers).
- Explain how rate limiting interacts with idempotent retries.
- Data Model:
- Outline the necessary database tables (e.g.,
orders,order_items, an idempotency store). - Specify key constraints and indexes for these tables.
- Outline the necessary database tables (e.g.,
- Concurrency:
- Explain how race conditions will be prevented.
- Describe how concurrent requests using the same
Idempotency-Keywill be handled.
- Testing:
- Describe the approach to unit and integration testing, specifically covering concurrency and failure scenarios.
Assumptions
Candidates should make minimal, explicit assumptions as needed and keep the design pragmatic and production-oriented.