Sub topic 2
The main kinds of limits
This chapter assumes you know what a Salesforce org, an object, and an edition are. It also assumes you have sent HTTP requests to a REST API and can read basic Apex and SOQL. Here you will build a working model of the limit categories. Later chapters cover how to measure each one in detail.
Why categories matter
Salesforce is a multitenant platform. Many customers share the same servers, databases, and network. Limits stop one org from using more than its share. Each limit protects a different shared resource, so each one fails in a different way and needs a different fix.
If you treat all limits as one thing, you will monitor the wrong number. An integration can have plenty of daily API requests left and still fail because too many slow requests ran at the same moment. A trigger can fail inside a single transaction while the org is far below its daily total.
Three categories cover most of what an integration developer meets:
- The daily API request limit, which counts calls to Salesforce APIs over a rolling 24-hour window.
- Concurrent long-running request limits, which cap how many slow requests can run at once.
- Apex governor limits, which cap what a single Apex transaction can consume.
Salesforce has other limits too, such as storage, bulk job batches, and Platform Event delivery. This tutorial focuses on these three because they are the ones you can measure and alert on with the tools in later chapters.
Scope and time window
Every limit has two properties you should ask about first: what it is counted against, and over what period.
| Category | Counted against | Time window | Typical failure |
|---|---|---|---|
| Daily API requests | The whole org, across all users and apps | Rolling 24 hours | `REQUEST_LIMIT_EXCEEDED` on every API call |
| Concurrent long-running requests | The whole org | The moment of the request | `REQUEST_LIMIT_EXCEEDED` for new long requests |
| Apex governor limits | One Apex transaction | Duration of the transaction | An uncatchable limit exception, and the transaction rolls back |
The scope tells you who is affected. A daily limit breach hits every integration in the org, including ones you do not own. A governor limit breach only hurts the one transaction that crossed it.
The time window tells you how recovery works. Governor limits reset the moment a new transaction starts. Concurrent limits clear as soon as slow requests finish. The daily limit recovers gradually, because the window rolls.
Daily API request limit
The daily API request limit is the total number of API calls your org may make in a 24-hour period. It is a rolling window, not a reset at midnight. Calls you made 23 hours ago still count. Calls you made 25 hours ago no longer do.
The allocation depends on your edition and on the number of user licenses. Salesforce also lets you buy additional API call capacity. Because the exact number varies by org, never hard-code it. Read it from the org, as shown later in this chapter.
Points that cause confusion:
- The limit is shared. A nightly ETL job, a marketing sync, a mobile app, and a developer's script all draw from one pool.
- Different API styles count against the same pool. REST, SOAP, and Bulk API calls all consume it, though Bulk API counts batches rather than individual records, which makes it far cheaper for large data volumes.
- Most calls cost one request regardless of how much data they return. Fetching 1 record and fetching 2,000 records with one query both cost one call. This is why batching is a primary lever for reducing usage.
- The window does not reset all at once. Usage falls as old calls age out of the 24 hours.
Concurrent long-running request limits
The concurrent request limit caps how many long-running API requests your org can have in flight at once. Salesforce treats a request as long-running when it runs for more than a set duration. At the time of writing that threshold is 20 seconds, and the limit on simultaneous long-running requests is 25 for production orgs. Treat these numbers as examples and check the current Salesforce limits documentation, because they change between releases and can differ for sandboxes and developer orgs.
This limit is about parallelism, not volume. Imagine 30 threads each running a slow report query. You might use only 30 of your 15,000 daily requests, yet the slowest ones will be rejected because too many are running at the same time.
Typical causes:
- Many parallel workers calling an inefficient SOQL query that takes a long time.
- A burst of requests that all trigger slow Apex or heavy automation on the Salesforce side.
- A retry storm. Workers see errors, retry immediately, and add more slow requests on top of the ones still running.
The fix is usually to run fewer requests in parallel, make each request faster (for example, add a selective filter to the query), or move large work to the Bulk API.
Apex governor limits
Governor limits apply inside Apex code. They cap the resources one transaction can use. A transaction is one unit of work, such as a trigger firing after a record save, a REST callout into your Apex class, or a Queueable job run.
Some commonly hit per-transaction limits for synchronous Apex are:
| Resource | Synchronous limit |
|---|---|
| SOQL queries | 100 |
| DML statements | 150 |
| Records returned by SOQL | 50,000 |
| CPU time | 10,000 ms |
| Heap size | 6 MB |
Asynchronous Apex (Batch, Queueable, Future) gets higher ceilings for some resources, such as 200 SOQL queries, 12 MB of heap, and 60,000 ms of CPU time. The numbers above reflect values published at the time of writing. Verify them against current documentation.
Governor limits differ from the other two categories in an important way. When you cross one, Apex throws a System.LimitException. You cannot catch it. The whole transaction rolls back. Your code must stay under the ceiling instead of reacting to the error.
A classic cause is a query inside a loop in a trigger:
trigger AccountTrigger on Account (before update) {
for (Account a : Trigger.new) {
// One SOQL query per record. With 101 records, this throws LimitException.
List<Contact> contacts = [SELECT Id FROM Contact WHERE AccountId = :a.Id];
}
}A bulk-safe version queries once and groups the results:
trigger AccountTrigger on Account (before update) {
Map<Id, List<Contact>> contactsByAccount = new Map<Id, List<Contact>>();
for (Contact c : [SELECT Id, AccountId FROM Contact WHERE AccountId IN :Trigger.newMap.keySet()]) {
if (!contactsByAccount.containsKey(c.AccountId)) {
contactsByAccount.put(c.AccountId, new List<Contact>());
}
contactsByAccount.get(c.AccountId).add(c);
}
// Use contactsByAccount for each record in Trigger.new
}The second version uses one query no matter how many records the trigger receives.
How the categories interact
The categories are independent counters, but one action can touch several of them.
- An external system calls a REST endpoint. That consumes one daily API request. If it is slow, it also occupies a concurrent long-running slot.
- That endpoint runs a trigger. The trigger runs inside a transaction with its own governor limits. Hitting a governor limit makes the API call fail, but the call still counted against the daily total.
- An integration that retries failed calls aggressively spends daily requests on each retry and can raise concurrency.
So a failed call is not free. Errors consume quota too.
Reading the limits that matter
For org-wide limits, the REST Limits resource returns the maximum and remaining value for each limit. This request reads it:
curl -s -H "Authorization: Bearer $ACCESS_TOKEN" \
"$INSTANCE_URL/services/data/v60.0/limits"
# The response is a JSON object keyed by limit name. For example:
# { "DailyApiRequests": { "Max": 15000, "Remaining": 14980 }, ... }The numbers in the comment are illustrative. Your org will return its own values.
For per-transaction limits, use the Apex Limits class. It reports both the consumption so far and the ceiling:
System.debug('SOQL queries: ' + Limits.getQueries() + '/' + Limits.getLimitQueries());
System.debug('DML statements: ' + Limits.getDmlStatements() + '/' + Limits.getLimitDmlStatements());
System.debug('CPU time (ms): ' + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime());
System.debug('Heap size (bytes): ' + Limits.getHeapSize() + '/' + Limits.getLimitHeapSize());Notice that the two tools match the two scopes. The REST resource sees the org. The Apex class sees the current transaction. Later chapters cover each in depth.
Common mistakes
- Assuming the daily limit resets at midnight. It is a rolling 24-hour window.
- Monitoring only the daily total. A concurrency problem will not show up there.
- Hard-coding a limit number. Editions, licenses, and purchased add-ons change it.
- Trying to
try/catcha governor limit. You cannot catchSystem.LimitException, so check consumption with theLimitsclass before you reach the ceiling. - Retrying immediately after
REQUEST_LIMIT_EXCEEDED. This burns more quota and adds load.
Exercise
An integration has 14,000 of 15,000 daily API requests remaining, but some of its calls fail with REQUESTLIMITEXCEEDED. Which category of limit is the most likely cause, and what would you change first?
Show answer
Most likely the concurrent long-running request limit, since the daily total is far from exhausted. Reduce the number of parallel workers, make the slow queries faster (for example, with selective filters), or move the heavy work to the Bulk API. Also add backoff to retries so failed calls do not pile on more slow requests.
Exercise
A trigger works when you save one Account but fails with a LimitException when a data load saves 200 Accounts at once. Which category of limit was hit, and how do you fix it?
Show answer
An Apex governor limit, most likely the 100 SOQL queries per transaction limit, because the trigger runs a query inside a loop. Fix it by querying once outside the loop with an IN clause on the record IDs, then grouping results in a Map. You cannot catch the exception, so the code must stay under the limit.
Exercise
At 10:00 you check your daily API usage and it is high. You stop all traffic. Will the remaining count be fully restored at midnight? Explain.
Show answer
No. The daily limit is a rolling 24-hour window, not a midnight reset. Each call stops counting 24 hours after it was made, so the remaining count recovers gradually as old calls age out.