# How do asynchronous batch integration patterns prevent synchronous timeout exceptions during large data loads?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Batch Integrations, Asynchronous Processing

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-asynchronous-batch-integration-patterns-prevent-synchronous-timeout-exceptions-during-large-data-loads

## Answer

**Basics:** Synchronous requests must finish quickly and within limits (10,000 ms CPU, 6 MB heap, 100 SOQL queries). Large loads exceed those, so you accept the data, return immediately, and process in the background in manageable slices.

**How it works:**
1. **Inbound bulk**: Bulk API 2.0 (or 1.0) accepts large CSV payloads and processes them asynchronously. Bulk API 1.0 batches are up to 10,000 records or 10 MB.
2. **Batch Apex**: `Database.executeBatch(job, scope)` with scope 1-2000 (default 200); each chunk is its own transaction with fresh limits (async: 200 SOQL, 12 MB heap, 60,000 ms CPU). QueryLocator can cover up to 50 million records.
3. **Queueable**: chained jobs for smaller workloads (one child per execution).
4. **Outbound**: Batch Apex with `Database.AllowsCallouts` sends chunks to the external system.
5. Track state with `Database.Stateful` and report failures.

**Advanced / deep dive:** Up to 5 batch jobs run concurrently; others wait in the Flex Queue (up to 100 Holding jobs). Smaller scope sizes help callout-heavy batches (100 callouts per transaction). Parallelism increases throughput but risks row locks, so group by parent. Use a staging object so intake is fast and processing is replayable. Bulk API results give per-record success/failure files. For streaming-like continuous load, consider Platform Events with trigger subscribers (batch up to 2,000 events). Monitor in Setup > Apex Jobs and Bulk Data Load Jobs.

**Code / config example:**
```apex
public class PushToErpBatch implements Database.Batchable<SObject>, Database.AllowsCallouts, Database.Stateful {
    public Integer failed = 0;
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT Id, Name FROM Account WHERE Sync_Status__c = \'Pending\'');
    }
    public void execute(Database.BatchableContext bc, List<Account> scope) {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('callout:ERP/bulk'); req.setMethod('POST');
        req.setBody(JSON.serialize(scope));
        HttpResponse res = new Http().send(req);       // one callout per chunk
        if (res.getStatusCode() >= 300) failed += scope.size();
        else { for (Account a : scope) a.Sync_Status__c = 'Done'; update scope; }
    }
    public void finish(Database.BatchableContext bc) { /* notify if failed > 0 */ }
}
// Database.executeBatch(new PushToErpBatch(), 100);
```

**Hidden gotchas & must-remember points:**
- Callouts in batch need `Database.AllowsCallouts`.
- Instance state is lost between chunks unless `Database.Stateful`.
- Scope 200 with one callout per record exceeds 100 callouts; lower the scope.
- Flex Queue holds 100 jobs; the 101st fails to enqueue.
- Parallel loads cause `UNABLE_TO_LOCK_ROW`.
- Async limits are higher but not unlimited.

**Best practice:** Accept fast, stage, process in right-sized async chunks, and report per-record outcomes.

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