Stackbook Logo
distribution-communicationestablished · medium operational burden

Bulk and Batch APIs

Also known as: batch-endpoint, bulk-operations

Intent

Let clients submit or fetch many items in a single request to cut round trips and per-call overhead.

Problem

Clients needing to create, update or fetch hundreds of items make a call per item, paying latency, auth and rate-limit costs each time. Server-side per-request overhead and chatty traffic degrade throughput.

Forces

  • •Network round trips dominate small operations
  • •Items may succeed or fail independently
  • •Request size and processing time must be bounded
  • •Atomic all-or-nothing is not always wanted

Solution

Overview

A bulk or batch API processes many items per request. It reduces network round trips and fixed per-request costs such as authentication, TLS and rate limit accounting, and lets the server apply set-based optimisations.

How it works

The client sends an array; the server validates, processes and reports per-item results.

POST /contacts:batchCreate
{ "items": [ {"ref":"a1","email":"x@e.com"}, {"ref":"a2","email":"bad"} ] }

207 Multi-Status
{ "results": [
    {"ref":"a1","status":201,"id":"c9"},
    {"ref":"a2","status":422,"error":"invalid email"} ] }

The ref field is a client-chosen id that correlates results and makes retries idempotent. Cap items per call, for instance 100, and total body size. For very large imports, return 202 Accepted with a job URL; the client polls the job for progress and a result file. For reads, accept a list of ids on a batch get endpoint.

Pitfalls

  • Ambiguous atomicity. State clearly whether a batch is transactional; most large batches should not be.
  • Returning 200 for a batch that partly failed without per-item detail.
  • No size or time limits, allowing one request to monopolise a worker.
  • Missing idempotency, so a retried timeout duplicates the successful items.
  • Rate limiting by request count only; account by item count as well.

Key takeaways

  • Bound batch size and report outcome per item.
  • Use client refs and idempotency keys for safe retries.
  • Go asynchronous with job resources for large volumes.
  • Document atomicity semantics explicitly.
  • Measure it in production: track latency, error rates and volume for this pattern, and review the numbers regularly so that limits and timeouts stay matched to real traffic.

✓ When to Use

  • •Importing, syncing or updating many records
  • •Fetching many resources by id
  • •Clients on high-latency networks

✗ When Not to Use

  • •Single-item interactive operations
  • •Operations needing strict per-item ordering with rollback complexity
  • •When a streaming or file-upload interface is more natural

Pros

  • +Fewer round trips and lower overhead
  • +Better throughput and rate-limit efficiency
  • +Server can optimise bulk writes

Cons

  • −Partial failure semantics add complexity
  • −Large requests risk timeouts and memory use
  • −Harder to cache and observe per item

Cost Profile

Infrastructure

Low - endpoint and validation

Operational

Medium - limits, async job handling

Cognitive

Medium - error semantics design

Failure Modes

  • Partial success mishandled by clients that only check the status code

  • Oversized batch exhausts memory or times out

  • Retried batch duplicates items without idempotency

Real-World Examples

Alternatives

    Related Patterns

    • idempotent-receiver

    Competency Domains

    distribution communicationdata state