Sub topic 3
Setting and reasoning about timeouts
This chapter assumes you can write a basic Apex callout with Http, HttpRequest, and HttpResponse. It also assumes you know the difference between synchronous and asynchronous Apex. By the end you will be able to set a timeout deliberately and explain how it fits inside the per-transaction limits.
What a timeout is
A callout timeout is the longest time Apex waits for the remote server to respond. When the time runs out, Apex stops waiting and throws a System.CalloutException. Apex does not return an HttpResponse in that case, so there is no status code to inspect.
Two facts shape everything else in this chapter:
- The default timeout is 10 seconds.
- The maximum for a single callout is 120 seconds.
HttpRequest.setTimeout takes milliseconds, not seconds. Passing 30 gives you a 30 ms timeout, which is a common mistake.
Setting an explicit timeout
Relying on the default hides a decision. An explicit value documents what you expect from the remote service.
public with sharing class OrderApiClient {
public class ApiException extends Exception {}
public static HttpResponse send(String path) {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Orders_API' + path);
req.setMethod('GET');
req.setTimeout(30000); // milliseconds; the default is 10000
try {
return new Http().send(req);
} catch (System.CalloutException e) {
// Thrown for timeouts and connection failures
throw new ApiException('Callout failed: ' + e.getMessage(), e);
}
}
}The catch block wraps the platform exception in a domain-specific one. Callers then handle one exception type that your class owns, and they do not depend on platform messages.
How the timeout relates to transaction limits
The per-callout timeout is only one of the limits. The total time spent in callouts during one transaction is also capped at 120 seconds. A single transaction can make up to 100 callouts, but the timeouts add up against that 120-second budget.
| Setting | Value | Scope |
|---|---|---|
| Default timeout | 10,000 ms | One callout |
| Maximum timeout | 120,000 ms | One callout |
| Cumulative callout time | 120 seconds | One transaction |
This has a practical consequence. If you set each timeout to the maximum, one slow callout can use the whole budget. Four callouts with a 30-second timeout can already reach 120 seconds in the worst case. Choose timeouts so the worst case across all callouts in the transaction still fits.
Long synchronous callouts have a second cost. Salesforce also limits how many long-running requests can run at the same time in an org. If a slow service keeps user-facing requests waiting, those requests can block other users.
Choosing a value
A timeout is a trade-off between waiting too little and waiting too long.
- Too short: healthy but slow responses fail, and you may trigger retries that add load to a service that is already struggling.
- Too long: a hung service holds your transaction, your user, and your cumulative budget.
A reasonable starting point is to measure the service's normal latency. Then set the timeout to a few times the 99th-percentile response time, and never above what the calling context can afford. A Visualforce page or Lightning controller should usually use a short timeout. A background job can tolerate a longer one.
A small helper keeps values inside the allowed range:
public with sharing class CalloutTimeouts {
private static final Integer MAX_MS = 120000;
public static Integer clamp(Integer requestedMs) {
return Math.max(1, Math.min(requestedMs, MAX_MS));
}
}You would then call req.setTimeout(CalloutTimeouts.clamp(configuredMs)), where configuredMs comes from custom metadata. That lets an admin tune the value without a deployment.
A timeout does not mean the request failed
The most important point about timeouts is that your side gives up, but the remote side may not. The server may have received the request, completed the work, and sent a response that arrived too late.
If you retry a POST after a timeout, you may create the order twice. Later chapters cover how to make retries safe. For now, treat a timeout as an unknown outcome, not as a confirmed failure.
Exercises
Exercise
A transaction makes five callouts, and each uses setTimeout(30000). Can the worst case succeed within Salesforce limits? Explain.
Show answer
No. Five callouts at 30 seconds each total 150 seconds in the worst case, which exceeds the 120-second cumulative callout limit for a transaction. Lower the per-callout timeouts, reduce the number of callouts, or move some of the work to asynchronous Apex.
Exercise
After a CalloutException caused by a timeout on a POST, can you conclude the server did not create the record? Why or why not?
Show answer
No. The server may have finished processing after Apex stopped waiting, so the outcome is unknown. A retry could create a duplicate unless the request is idempotent.