# How does the Composite API optimize performance by batching multiple REST requests into a single HTTP call?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Composite API, Performance Optimization

Read it on the site: https://www.forcechallenges.com/interview-questions/how-does-the-composite-api-optimize-performance-by-batching-multiple-rest-requests-into-a-single-http-call

## Answer

**Basics:** Creating an Account, a Contact and an Opportunity through the plain REST API takes three HTTP calls, each with network latency and API overhead. A composite request combines them into one POST to `/services/data/vXX.0/composite`, which cuts latency and API usage.

**How it works:**
1. Send `{"allOrNone": true, "compositeRequest": [...]}`.
2. Each subrequest has `method`, `url`, `referenceId` and optionally `body`.
3. Later subrequests use `@{referenceId.field}` (for example `@{newAccount.id}`) to consume earlier output.
4. The response returns a `compositeResponse` array with `httpStatusCode`, `body` and `referenceId` for each.

**Advanced / deep dive:** Know the family:
- Composite: up to 25 subrequests, dependent via reference IDs; up to 5 can be queries.
- Composite Batch (`/composite/batch`): up to 25 independent subrequests, no references or shared transaction.
- sObject Tree (`/composite/tree/<sObject>`): nested parent-child creation, up to 200 records.
- sObject Collections: create, update, delete up to 200 records of one or mixed types in one call.
- Composite Graph (`/composite/graph`): many subrequests organized into independent graphs, for larger dependent workloads (verify current size limits).
Behavior of `allOrNone`: when true, any failure rolls back the entire request and the others show `PROCESSING_HALTED`; when false, successes commit and only dependent subrequests fail. Governor limits and triggers run for the data operations as usual, and a composite request counts as one API call against the daily limit (verify exact accounting in current docs). For high volume, use Bulk API 2.0 instead.

**Code / config example:**
```json
{
  "allOrNone": true,
  "compositeRequest": [
    {"method":"POST","url":"/services/data/v62.0/sobjects/Account","referenceId":"acc",
     "body":{"Name":"Acme"}},
    {"method":"POST","url":"/services/data/v62.0/sobjects/Contact","referenceId":"con",
     "body":{"LastName":"Rao","AccountId":"@{acc.id}"}}
  ]
}
```

**Hidden gotchas & must-remember points:**
- Maximum 25 subrequests; beyond that, split or use Graph/Collections.
- Reference IDs must be unique within the request.
- With `allOrNone` false, parent failure leaves children failing and partial data committed.
- Composite Batch has no cross-references and no transaction.
- Each subrequest still runs triggers and flows, so recursion and limits still matter.

**Best practice:** Use Composite for small dependent operations, Collections or Bulk API for volume, and always inspect every subresponse.

---
Source: ForceChallenges · last updated 2026-11-02
