Idempotent Endpoint
Problem
Build a POST endpoint that safely handles duplicate requests using idempotency keys.
Background
In production systems, network failures and client retries mean the same request can arrive multiple times. An idempotent endpoint ensures that processing a request multiple times produces the same result as processing it once — no duplicate records, no double charges, no inconsistent state.
This is a fundamental reliability pattern used in payment systems, order processing, and any operation with real-world side effects.
Requirements
- Implement
POST /ordersthat creates an order and stores it in the database - Accept an
Idempotency-Keyheader on every request - If the same idempotency key is sent again, return the original response without creating a duplicate order
- Handle concurrent duplicate requests safely — two identical requests arriving at the same time must not create two orders
- Reject requests that are missing the
Idempotency-Keyheader with400
Your Server
- Start an HTTP server on the port specified by the
PORTenvironment variable (default:3000) - A PostgreSQL database is available at the
DATABASE_URLenvironment variable - You are responsible for creating any tables you need (run your schema setup on startup)
API Contract
`GET /health`
Health check endpoint. Return 200 when your server is ready.
- Response:
200 { "status": "ok" }
`POST /orders`
Create a new order (idempotent).
Request:
- Header:
Idempotency-Key:(required) - Body:
{ "item": "string", "quantity": number }
Response (new order):
- Status:
201 - Body:
{ "id": "", "item": "string", "quantity": number }
Response (duplicate request):
- Status:
200 - Body:
{ "id": "(same ID as original)", "item": "string", "quantity": number }
Response (missing idempotency key):
- Status:
400 - Body:
{ "error": "Idempotency-Key header is required" }
Your solution will be tested against these scenarios plus a hidden set.
- 01POST /orders with a valid idempotency key creates an order and returns 201
- 02Sending the same request twice with the same Idempotency-Key returns the original response with status 200
- 03Different idempotency keys create separate orders with unique IDs
- 04Two identical requests sent at the same time with the same key produce only one order
Hints
Click each hint to reveal it. Take your time — try before you peek.