Sub topic 11
Handling REQUEST_LIMIT_EXCEEDED and retries
This chapter assumes you can make authenticated REST calls to Salesforce, read JSON error bodies, and write basic Python. You should also know the difference between the daily API request limit and concurrent request limits from earlier chapters.
What the error looks like
When an org passes a limit, the API rejects the call with HTTP status 403. The body is a JSON array with the error code REQUEST_LIMIT_EXCEEDED:
[
{
"message": "TotalRequests Limit exceeded.",
"errorCode": "REQUEST_LIMIT_EXCEEDED"
}
]The same code covers more than one limit. The message text tells you which limit you hit:
| Message starts with | Limit hit | Does waiting a few seconds help? |
|---|---|---|
| `TotalRequests` | Daily (rolling 24-hour) API requests | No. Capacity returns slowly as old requests age out of the window. |
| `ConcurrentPerOrgLongTxn` | Concurrent long-running requests | Yes. Slots free up when running requests finish. |
Do not treat both cases the same way. Retrying a daily-limit error in a tight loop only creates more failed calls and delays recovery.
Decide before you retry
The decision depends on the message:
Exponential backoff with jitter
Exponential backoff doubles the wait after each failure: 1 second, 2, 4, 8, and so on. Jitter adds a random amount to each wait. Without jitter, many workers that failed together retry together and collide again.
import os
import random
import time
import requests
instance_url = os.environ["INSTANCE_URL"]
token = os.environ["ACCESS_TOKEN"]
class DailyLimitExceeded(Exception):
"""Raised when the daily API request limit is used up."""
def call_with_retry(path, max_attempts=5, base_delay=1.0, max_delay=30.0):
url = f"{instance_url}/services/data/v60.0/{path}"
headers = {"Authorization": f"Bearer {token}"}
for attempt in range(max_attempts):
resp = requests.get(url, headers=headers, timeout=30)
if resp.status_code == 403 and "REQUEST_LIMIT_EXCEEDED" in resp.text:
if "TotalRequests" in resp.text:
# Daily limit: retrying now will not help.
raise DailyLimitExceeded(resp.text)
# Concurrent limit: wait and try again.
delay = min(max_delay, base_delay * 2 ** attempt)
time.sleep(delay + random.uniform(0, delay / 2))
continue
resp.raise_for_status()
return resp.json()
raise RuntimeError(f"Still limited after {max_attempts} attempts")
if __name__ == "__main__":
print(call_with_retry("limits")["DailyApiRequests"])The function reads the message to separate the two cases. It caps the delay with max_delay and the attempts with max_attempts, so a stuck call fails loudly instead of looping forever.
Common mistakes
- Retrying every 403. A 403 can also mean missing permissions. Only retry when the body contains
REQUEST_LIMIT_EXCEEDED. - Retrying without a cap. Always set a maximum number of attempts and a maximum delay.
- Retrying the daily limit. Pause the integration and raise an alert instead.
- Ignoring non-idempotent calls. A POST that created a record may have succeeded before the error reached you. Use an external ID with upsert, or check for the record first, so a retry does not create duplicates.
- Retrying in parallel. If ten threads all back off and retry, concurrency stays high. Lower the worker count when you see these errors.
Connect retries to monitoring
Log each limit error with its message, the time, and the client that made the call. These records show whether you are meeting the concurrent limit in short bursts or draining the daily budget. To fix either problem, apply the consumption reductions from the next chapter and the alerts described later in this tutorial.
Exercise
Your integration receives a 403 with errorCode REQUESTLIMITEXCEEDED and the message "TotalRequests Limit exceeded." Should it retry with exponential backoff? What should it do instead?
Show answer
No. This message means the daily API request limit is used up, and waiting seconds or minutes will not restore capacity. The integration should stop sending calls, raise an alert, and pause until usage drops. It can resume later, ideally after checking remaining requests with the REST Limits resource. Backoff with jitter is for the concurrent request limit (ConcurrentPerOrgLongTxn).