Bulk and batch endpoints: partial failure handling

Difficulty: Intermediate

Question

How would you design an endpoint to create or update many resources in one request, and how do you handle partial failures?

Answer

The motivation is straightforward. Uploading 5,000 products with 5,000 separate POST requests wastes round trips, hits rate limits, and takes forever. A bulk endpoint lets the client send many items in one call. It is the difference between posting 5,000 letters one at a time and sending a single parcel, but a parcel raises new questions: what if item 3,412 is damaged?

First decide the shape. Common options: POST /products/bulk or /products:batchCreate with a body of { items: [...] }, PATCH on the collection with an array, or a generic /batch endpoint accepting heterogeneous sub-requests (Google and Microsoft Graph support this, each sub-request having its own method, URL and body). Keep it explicit and documented, and set a maximum batch size, say 100 to 1000 items, plus a maximum body size.

Then decide the atomicity model, the crux of the interview answer. All-or-nothing (transactional): if any item fails, nothing is applied and you return an error listing the problems. This is simple for clients to reason about and right for tightly related changes such as transferring funds between accounts, but a single bad row blocks everything. Partial success (best effort): each item is processed independently, and the response reports per-item outcomes. This suits imports and independent records but forces clients to handle mixed results. State clearly which model each endpoint uses, or let clients choose with a parameter like atomic=true.

Reporting partial results. Use 207 Multi-Status (from WebDAV) or simply 200 with a results array where each element has an index or client reference id, a status code, and either the created resource or a problem details object. Some teams return 200 if all succeeded, 207 if mixed and 4xx if the whole request was invalid, such as malformed JSON or exceeding the size limit. Crucially, results must be order-preserving or keyed by a client-supplied reference so the client can map outcomes back to inputs.

Idempotency and retries: if a bulk call times out, the client can't know what succeeded. Support an Idempotency-Key for the whole request and/or per-item natural keys (SKU, external_id) with upsert semantics, so re-sending the same batch is harmless and only completes missing items. Upsert via PUT-style semantics on unique external ids is one of the most useful features for integrators.

Performance and safety: process in chunks, use multi-row inserts, keep transactions small to avoid long locks, and apply validation before touching the database. Rate limit by item count and not request count, otherwise a batch of 1000 bypasses your limits. For very large imports or long processing time, combine with the async pattern: accept a file or array, return 202 with a job id, and report per-row errors in a downloadable result file.

Authorization applies per item: a user may be allowed to update some of the referenced resources and not others, so the per-item response should carry 403 or 404 as appropriate.

When not to build bulk: if a client mostly needs fewer round trips for reads, prefer filtering with IN-style parameters like ids=1,2,3, or use HTTP/2 multiplexing, which reduces per-request overhead.

Code examples

Partial-success bulk create

POST /v1/products:batchCreate HTTP/1.1
Content-Type: application/json

{ "items": [
  { "ref": "a1", "sku": "KB-1", "price": 199900 },
  { "ref": "a2", "sku": "",     "price": -5 }
] }

HTTP/1.1 207 Multi-Status
Content-Type: application/json

{ "results": [
  { "ref": "a1", "status": 201, "resource": { "id": "prd_1", "sku": "KB-1" } },
  { "ref": "a2", "status": 422, "error": { "title": "Validation failed", "errors": [ { "pointer": "/sku", "reason": "required" } ] } }
] }

Each item carries its own status, and the client-supplied ref maps results back to inputs.

Key points

Common follow-up questions

Interview tips

Concepts covered

bulk operations, batch, 207 Multi-Status, partial failure

More API Design questions