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:
- 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.
- 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. - Queueable: chained jobs for smaller workloads (one child per execution).
- Outbound: Batch Apex with
Database.AllowsCalloutssends chunks to the external system. - Track state with
Database.Statefuland 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:
public class PushToErpBatch .Batchable<SObject>, Database.AllowsCallouts, Database.Stateful {
;
Database.QueryLocator {
Database.getQueryLocator();
}
{
();
req.setEndpoint(); req.setMethod();
req.setBody(JSON.serialize(scope));
().send(req);
(res.getStatusCode() >= ) failed += scope.size();
{ (Account a : scope) a.Sync_Status__c = ; update scope; }
}
{ }
}