Sub topic 2
Callout limits you must plan around
This chapter assumes you can write a basic callout with Http, HttpRequest, and HttpResponse. It also assumes you know that Apex governor limits apply per transaction and that Queueable Apex runs asynchronously. Retry logic later in this tutorial depends on these limits, so it helps to know them first.
The limits that matter for callouts
Every transaction gets a fixed budget for callouts. Check the current values in the Apex Developer Guide, because Salesforce can change them.
| Limit | Value | What happens when you exceed it |
|---|---|---|
| Callouts per transaction | 100 | `System.LimitException`, which you cannot catch |
| Timeout of one callout | Default 10 seconds, maximum 120 seconds | `System.CalloutException` |
| Cumulative callout time per transaction | 120 seconds | The transaction fails |
| Request or response size | 6 MB synchronous, 12 MB asynchronous | The callout fails |
Three of these limits need a closer look.
The 100-callout cap is shared
The cap counts every Http.send call in the transaction, including calls made by other classes, packages, and triggers that share it. A loop that makes one callout per record will hit the cap as soon as it processes 101 records.
A LimitException cannot be caught. Your retry code never gets a chance to run, and the whole transaction rolls back. You have to check the budget before you send.
Time adds up across callouts
Each callout has its own timeout, but the transaction also has a cumulative ceiling of 120 seconds. Five sequential callouts with a 30-second timeout each can use 150 seconds in the worst case, which is over the ceiling. The timeout you set on each request therefore has to fit inside the transaction's total. The next chapter covers how to choose it.
Waiting time is not CPU time
Time spent waiting on the remote server does not count toward the Apex CPU limit. A slow endpoint therefore will not trip the CPU limit. It still holds the transaction open. Synchronous requests that run for a long time also count against an org-wide limit on concurrent long-running requests. A slow API called from a busy page can block other users.
Checking the budget in code
The Limits class tells you how many callouts you have used and how many remain. Use it to stop before the hard limit instead of crashing into it.
public with sharing class CalloutBudget {
public static Integer remaining() {
return Limits.getLimitCallouts() - Limits.getCallouts();
}
public static Boolean hasRoom(Integer needed) {
return remaining() >= needed;
}
}A caller can then split its work and defer the rest. This sketch sends what fits and returns the records it could not send, so the caller can hand them to a Queueable job.
public with sharing class OrderSync {
public static List<Id> sendWhatFits(List<Id> orderIds) {
List<Id> deferred = new List<Id>();
for (Id orderId : orderIds) {
if (!CalloutBudget.hasRoom(1)) {
deferred.add(orderId);
continue;
}
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Orders_API/orders/' + orderId);
req.setMethod('GET');
req.setTimeout(10000);
new Http().send(req);
}
return deferred;
}
}Why a new transaction helps
Each Queueable job runs in its own transaction with a fresh set of limits. That is why asynchronous retries work. A retry that waits for a new job gets 100 new callouts and a new 120-second window. The retry is also separate from the transaction that failed, so the failure does not roll back the retry.
Common mistakes
- Calling
Http.sendinside a loop without checkingLimits.getCallouts(). - Setting a 120-second timeout on every request, so one slow endpoint uses the whole transaction budget.
- Assuming a
try/catchwill protect you from the callout cap. It will not, because aLimitExceptionis not catchable. - Forgetting that other code in the same transaction, such as a trigger or a flow, also spends callouts.
Exercise
A trigger handler makes one callout per Account in a bulk update of 150 Accounts. It wraps each callout in a try/catch for CalloutException. What happens at the 101st Account, and how would you fix the design?
Show answer
The 101st callout throws a System.LimitException ("Too many callouts: 101"). The catch for CalloutException does not catch it, so the whole transaction rolls back. To fix it, check Limits.getCallouts() against Limits.getLimitCallouts() before each send, and move the remaining records into a Queueable job that runs in a new transaction. Alternatively, batch the work into fewer callouts if the API supports bulk requests.