Sub topic 13
Allocations and limits
This chapter covers the quotas that apply to Change Data Capture (CDC) and how they affect the design of a subscriber. It assumes you can already enable CDC for an object, subscribe with empApi or an external client, and write an Apex trigger on a change event. It also assumes you know the basics of Apex governor limits.
Three kinds of limit
CDC limits fall into three groups. Keep them apart when you plan an integration, because each group fails in a different way.
- Selection limits decide how many objects can produce change events.
- Delivery allocations decide how many events an org can send to external subscribers in a 24-hour period.
- Retention and runtime limits decide how long events stay available and how much work one trigger batch can do.
Selection limits: how many objects
The Change Data Capture page in Setup lets you move objects into the Selected Entities list. Without an add-on license, an org can select up to five entities, counting standard and custom objects together. Some standard objects are not available for CDC at all, and you can't select them.
Selection limits matter most when a team wants "CDC on everything". The usual answers are:
- Select only the objects that downstream systems need.
- Buy the add-on license if more than five are needed.
- Use a different pattern, such as scheduled queries, for low-value objects.
Selecting an object for a custom channel does not avoid the limit. A custom channel only groups events from entities you have already enabled. It is covered in the previous chapter.
Delivery allocations: how many events to clients
Every change event delivered to a CometD or Pub/Sub API client counts against a rolling 24-hour delivery allocation. empApi in a Lightning web component uses CometD, so it counts too. The allocation depends on the edition. It is measured in events, not in subscribers or records.
Two details change how you size an integration:
- Events are counted per delivery. If three external clients subscribe to the same channel, each event is delivered three times, and each delivery counts against the allocation. Check how your edition counts this before you add subscribers.
- One record change can mean one event, but one transaction can mean many. A bulk update of 10,000 Accounts produces change events for all 10,000 records, even though they come from one DML statement. A nightly data load can use up a daily allocation.
Apex triggers on change events are a different case. They run inside the platform. The delivery allocation applies to external delivery over CometD and Pub/Sub API, so a trigger subscriber doesn't consume it the same way. Triggers still have their own constraints, described below.
When the allocation is exhausted, delivery to clients stops until usage falls back under the limit as the 24-hour window moves forward. The events are still published to the bus and stay available for the retention period. A client that reconnects later can resume from its last replay ID and receive them, as long as they have not expired.
Checking your usage
You can read usage with the org limits API. The following Apex reads the daily delivered event counter. It checks for null because the set of limit names can differ between orgs and API versions.
Map<String, System.OrgLimit> orgLimits = OrgLimits.getMap();
System.OrgLimit delivered = orgLimits.get('DailyDeliveredPlatformEvents');
if (delivered != null) {
System.debug('Delivered events in the last 24 hours: ' + delivered.getValue());
System.debug('Daily allocation: ' + delivered.getLimit());
} else {
System.debug('DailyDeliveredPlatformEvents is not reported in this org.');
}The same data is available from the /services/data/vXX.X/limits REST resource, so a monitoring job outside Salesforce can alert at, say, 70 percent of the allocation instead of waiting for delivery to stop.
Concurrent clients
CometD and Pub/Sub API connections are also capped in number. The cap is shared with platform event subscribers, so a busy org can run into it even when CDC is a small part of its traffic. Each open empApi subscription holds a client connection, and each browser tab with a component that subscribes can hold one.
A common mistake is putting a subscribe call in a component that renders many times on a page, such as a list row. Every instance opens its own subscription. Subscribe once, in a parent component or a utility module, and pass the data down.
Retention: how long events stay
Change events stay on the event bus for three days (72 hours). A subscriber can request events from the retention window by passing a replay ID. Anything older is gone, and no replay ID can bring it back.
This sets the maximum outage a subscriber can tolerate. If an external consumer is down for more than three days, it can't catch up from the bus. It must reload the data another way, for example with a bulk query filtered on LastModifiedDate, and then resume from the newest event.
Runtime limits for Apex triggers
A trigger on AccountChangeEvent runs in its own transaction. That transaction has the normal governor limits, such as 100 SOQL queries, 150 DML statements, and 10 seconds of CPU time for synchronous work. Some points are specific to event triggers:
- Batch size. Events are delivered to the trigger in batches. The default batch size can reach 2,000 events. Write the trigger so it runs the same number of queries and DML statements whether
Trigger.newhas 1 event or 2,000. The trigger in the "Reacting to changes with Apex triggers" chapter does this by collecting Tasks in a list and inserting once. - Running user. Change event triggers run as the Automated Process user by default, not as the user who changed the record. Sharing, field-level security, and
CreatedByIdon records you create reflect that user. You can configure a different running user for the trigger. - Retries. If a trigger throws an uncaught
EventBus.RetryableException, the platform retries the batch later. It does this a limited number of times and then stops. Use it only for temporary failures such as a record lock.
The following example shows the safe way to use a retry. Any other exception fails the batch and the events are not redelivered to the trigger.
trigger AccountChangeRetryTrigger on AccountChangeEvent (after insert) {
Set<Id> accountIds = new Set<Id>();
for (AccountChangeEvent event : Trigger.new) {
EventBus.ChangeEventHeader header = event.ChangeEventHeader;
if (header.changeType == 'UPDATE') {
for (String recordId : header.recordIds) {
accountIds.add(recordId);
}
}
}
if (accountIds.isEmpty()) {
return;
}
try {
List<Account> accounts = [
SELECT Id, Name FROM Account WHERE Id IN :accountIds FOR UPDATE
];
// Process the locked accounts here.
} catch (QueryException e) {
throw new EventBus.RetryableException('Accounts locked, retrying: ' + e.getMessage());
}
}Which limit applies to which consumer
Different consumers hit different limits. The diagram shows the path of one change event.
The table summarizes the same information with the typical remedy for each limit.
| Limit | Applies to | What happens at the limit | Typical remedy |
|---|---|---|---|
| Selected entities (five by default) | Setup | You can't select more objects | Select fewer objects, or buy the add-on |
| Daily delivery allocation | CometD and Pub/Sub API clients | Delivery pauses until usage drops | Filter with custom channels, reduce the number of subscribers, raise the allocation |
| Concurrent clients | CometD and Pub/Sub API connections | New connections are refused | Share one subscription per page or per service |
| Retention (three days) | Event bus | Older events are unavailable | Reconcile with a bulk query, then resume |
| Governor limits | Apex triggers | The trigger transaction fails | Bulkify, and cut queries and DML per batch |
Design choices that reduce consumption
You can lower delivery usage without losing data:
- Filter at the source. Use a custom channel with a filter expression, where available, so the platform drops events your system does not need. Saving a delivery is cheaper than discarding an event in your client.
- Use one subscriber per system. Let a single integration service subscribe once, then fan out inside your own infrastructure.
- Avoid noisy fields. Updates from rollup summaries, formula-driven workflows, or batch jobs that rewrite unchanged values produce events you may not care about. Check
changedFieldsearly and ignore events that touch only system fields. - Throttle bulk loads. Run large loads at off-peak times, or load them in slices, so they don't use the full allocation at once.
Exercise
A nightly Apex batch job updates 60,000 Account records. Two external services subscribe to /data/AccountChangeEvent through the Pub/Sub API, and both are online. Roughly how many deliveries count against the daily delivery allocation, and what happens to a service that is offline for five days?
Show answer
About 120,000 deliveries, because each of the 60,000 events is delivered to each of the two subscribers, and each delivery counts. If that exceeds the org's allocation, delivery pauses until usage drops, and the events stay on the bus for three days. A service that is offline for five days has lost the oldest events, because retention is three days. It must reconcile with a bulk query on LastModifiedDate and then resume from the newest replay ID.
Exercise
Name two reasons a trigger on AccountChangeEvent could fail with a limit error even though the org is well under its daily delivery allocation.
Show answer
The delivery allocation does not apply to Apex triggers, so the failure comes from governor limits in the trigger transaction. Examples are a SOQL query or DML statement inside a loop over a batch of up to 2,000 events, which exceeds the query or DML limit, and CPU time use that grows with the batch size.