Video in progress
How do you design a bulk API that accepts up to 1000 items in one request for other services? How do you return partial success?
Every item gets its own result: the response has a status, an error code and the client-sent ID for each one. Whether the whole request should be "all or nothing" is something I state explicitly in the contract. A size limit, idempotency and an asynchronous mode for large volumes are part of the contract too.
📖 Answer
You need a bulk endpoint because 1000 separate requests are expensive for the network and the server. But new questions come with it.
Atomicity. There are two options and the contract has to say which one explicitly:
- all-or-nothing: if any item is invalid, nothing is written (400). Often this for financial operations;
- partial: each item is independent. Convenient for things like price updates and stock imports.
The shape of a partial response:
POST /prices:batch
{"items": [{"clientRef": "a1", "sku": "SKU-1", "price": 15000},
{"clientRef": "a2", "sku": "SKU-404", "price": 9000}]}
200 OK
{"results": [
{"clientRef": "a1", "status": "OK"},
{"clientRef": "a2", "status": "ERROR", "code": "SKU_NOT_FOUND"}
],
"summary": {"ok": 1, "failed": 1}}
clientRef matters: the client knows which item failed by its own ID, not by index, and resends only those. Some return 207 Multi-Status. Whatever the code, what matters is that the client doesn't think "I got a 200, so everything is fine", meaning the contract states that the summary has to be checked.
Other decisions:
- A limit: at most 1000
items, at most N MB of body. If exceeded, 413 or 400 with a clear message. A bulk endpoint with no limit will one day arrive with 200,000 items and eat the service's memory. - Idempotency: an
Idempotency-Keyfor the whole batch or a business key per item (an upsert). If the client times out and resends, there must be no duplicates. - Transaction size: writing 1000 items in chunks of 100 rather than one big transaction keeps locks short. In partial mode each chunk is committed separately.
- Time: if a request may take more than 30 seconds, make it an asynchronous job rather than a synchronous bulk call:
202 Accepted+Location: /jobs/123, and the result comes later. - Rate limiting: one bulk request is the same load as 1000 ordinary requests, so count it that way in the limit too.
Sign up to mark studied questions and save your progress.