Sub topic 4
Concurrent long-running requests
This chapter covers the concurrent request limit. It is separate from the daily API request limit. It assumes you have made REST calls to Salesforce and read the previous chapters on limit categories and the daily limit.
What the limit measures
The daily limit counts how many calls you make over 24 hours. The concurrent limit counts how many slow calls are running at the same moment.
Salesforce treats an API request that runs for 20 seconds or longer as long-running. An org can have only a limited number of these in progress at once:
| Org type | Concurrent long-running requests |
|---|---|
| Production and sandbox | 25 |
| Developer Edition and trial | 5 |
These numbers come from the Salesforce limits documentation and can change, so check the current values for your org. Requests that finish in under 20 seconds do not count toward this limit. A request counts only while it runs. Once it finishes, it frees its slot.
The limit protects the shared platform. This is the multitenancy reason from the first chapter. A few slow calls can tie up server capacity that other customers need.
What triggers it
Requests usually run long for one of these reasons:
- A SOQL query that filters on a non-selective field over millions of records.
- A request that fires Apex triggers, flows, or workflow rules that do a lot of work.
- Record locking. Parallel updates to the same parent record wait for each other, and the waiting time counts.
- An integration that fires many parallel calls without a ceiling on parallelism.
The last cause is the most common. Each call alone might be fine, but 40 of them in parallel against a slow query overflow the slots.
How the failure looks
When all slots are taken, the next long-running request is rejected. The API returns HTTP 403 with the error code REQUEST_LIMIT_EXCEEDED. The message names the concurrent limit, for example ConcurrentPerOrgLongTxn Limit exceeded. A daily-limit failure uses the same error code with different message text. Read the message to tell them apart.
Monitoring it
The REST Limits resource reports daily usage and several report-related concurrency counters. It does not give a live count of long-running API requests. You have to watch for the symptoms instead:
- Count
REQUEST_LIMIT_EXCEEDEDresponses in your client logs and alert when they appear. - Log how long each request takes. Requests that approach 20 seconds are the ones to fix.
- Use the
ApiTotalUsageevent logs from the Event Monitoring chapter to find which client sends the slow calls.
Staying under the ceiling
Cap your own parallelism well below the limit, and retry with backoff when you are rejected. The script below runs queries through a pool of 10 workers and retries on a limit error.
import os
import random
import time
from concurrent.futures import ThreadPoolExecutor
import requests
INSTANCE_URL = os.environ["INSTANCE_URL"]
TOKEN = os.environ["ACCESS_TOKEN"]
MAX_WORKERS = 10 # well below the 25-slot limit
def run_query(soql, attempts=5):
for attempt in range(attempts):
resp = requests.get(
f"{INSTANCE_URL}/services/data/v60.0/query",
headers={"Authorization": f"Bearer {TOKEN}"},
params={"q": soql},
timeout=120,
)
if resp.status_code == 403 and "REQUEST_LIMIT_EXCEEDED" in resp.text:
# Exponential backoff with jitter, capped at 60 seconds
time.sleep(min(60, 2 ** attempt) + random.random())
continue
resp.raise_for_status()
return resp.json()["totalSize"]
raise RuntimeError("Still rate limited after retries")
queries = [
"SELECT COUNT() FROM Account",
"SELECT COUNT() FROM Contact",
"SELECT COUNT() FROM Opportunity",
]
with ThreadPoolExecutor(max_workers=MAX_WORKERS) as pool:
for soql, total in zip(queries, pool.map(run_query, queries)):
print(f"{soql}: {total}")The backoff is for the rare spike. The worker cap is the real protection. Retrying harder against a saturated org only keeps the slots full.
Fix the slow requests as well:
- Add selective filters or indexed fields to queries.
- Split large jobs into smaller batches.
- Move heavy work to the Bulk API, which runs asynchronously and does not hold a long-running slot.
- Group updates by parent record so parallel jobs do not wait on each other's locks.
Exercise
An integration sends 40 parallel queries, each taking about 30 seconds, to a production org. Some fail with REQUESTLIMITEXCEEDED, yet the daily API usage is only 2%. What explains this, and what are two fixes?
Show answer
Each query runs 20 seconds or longer, so each holds one of the 25 long-running slots. With 40 in parallel, the extra requests are rejected. The daily limit is unrelated. Fixes: cap the worker pool below 25 (for example 10) and retry with backoff, and make the queries faster with selective filters or use the Bulk API.