# How do developers configure HTTP callout timeouts to prevent transactions from hanging indefinitely on slow external servers?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Callout Timeouts, Performance Control

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-developers-configure-http-callout-timeouts-to-prevent-transactions-from-hanging-indefinitely-on-slow

## Answer

**Basics:** An HTTP callout waits for an external server. Without a limit, a slow server could tie up your transaction. Apex lets you set a per-request timeout; when it expires you get a CalloutException (typically "Read timed out"). The unit is milliseconds, not seconds.

**How it works:**
1. Build the HttpRequest, set endpoint (preferably a Named Credential), method, headers and body.
2. Call req.setTimeout(ms).
3. Send with Http.send; catch CalloutException and inspect HttpResponse status for non-timeout errors.
4. Decide on retry, queue or fail.

**Advanced / deep dive:** The default is 10 seconds and the maximum is 120 seconds per callout, and the cumulative time of all callouts in a transaction is also capped at 120 seconds. A transaction may make up to 100 callouts. Long waits also count toward the org's concurrent long-running request limit (requests running over 5 seconds can exhaust the concurrent Apex request limit, with a documented per-org cap; verify the exact number), so set timeouts as short as the business allows. Timeouts in a synchronous UI context block the user; move slow calls to Queueable with Database.AllowsCallouts, or use Continuation for Visualforce/Aura/LWC calls to long APIs, which are non-blocking on the server and do not count the same way. A timeout does not roll back the remote operation, so use idempotency keys and a status field so a retry does not double-create. Retry with backoff only through async jobs (no sleeping in Apex). Also note request and response size limits (6 MB sync, 12 MB async).

**Code / config example:**
```apex
public with sharing class InventoryClient {
    public static String getStock(String sku) {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('callout:Inventory_API/stock/' + EncodingUtil.urlEncode(sku, 'UTF-8'));
        req.setMethod('GET');
        req.setTimeout(8000); // milliseconds (max 120000)
        try {
            HttpResponse res = new Http().send(req);
            if (res.getStatusCode() == 200) return res.getBody();
            throw new CalloutException('Unexpected status ' + res.getStatusCode());
        } catch (CalloutException e) {
            // log, mark record for retry via Queueable, do not swallow
            throw e;
        }
    }
}
```

**Hidden gotchas & must-remember points:**
- setTimeout takes milliseconds; 120 means 120 ms.
- Max 120 seconds per callout and 120 seconds cumulative.
- DML before a callout in the same transaction throws "uncommitted work pending".
- A timeout does not mean the remote system did nothing.
- 100 callouts per transaction.
- Test methods cannot make real callouts; use HttpCalloutMock.

**Best practice:** Use a short timeout aligned to the user experience, make operations idempotent, and retry only in async jobs.

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