Sub topic 14
Allocations, limits, and monitoring
This chapter assumes you know Apex governor limits, have published events with EventBus.publish, and have written an event trigger. It covers the limits that apply to platform events and how to watch your usage.
Two kinds of limits
Platform events are subject to two separate sets of limits.
- Apex governor limits apply inside one transaction, as for any Apex code.
- Org allocations apply across the whole org over time. They depend on your edition and any add-on licenses.
The exact numbers change between releases and editions. Check the Platform Events Developer Guide for your org's values instead of hard-coding figures from memory.
Limits inside a transaction
A call to EventBus.publish counts as one DML statement. Each event in the call also counts as one DML row. Publishing 200 events in one call costs one statement and 200 rows. Publishing them in a loop costs 200 statements and can hit the DML statement limit.
Publish a list of events in a single call:
List<Order_Shipped__e> events = new List<Order_Shipped__e>();
for (Integer i = 0; i < 50; i++) {
events.add(new Order_Shipped__e(
Order_Number__c = 'ORD-' + i,
Status__c = 'Shipped'
));
}
List<Database.SaveResult> results = EventBus.publish(events);
for (Database.SaveResult sr : results) {
if (!sr.isSuccess()) {
for (Database.Error err : sr.getErrors()) {
System.debug('Publish failed: ' + err.getStatusCode() + ' ' + err.getMessage());
}
}
}
System.debug('DML statements used: ' + Limits.getDmlStatements());Events published with the publish immediately behavior have their own counters, Limits.getPublishImmediateDML() and Limits.getLimitPublishImmediateDML(). Use them if you publish immediately and need to stay under the cap.
A single event message also has a size limit, so keep payload fields small and send record IDs instead of whole records.
Allocations across the org
Org-wide allocations cap two things:
- The number of events you can publish per hour.
- The number of event deliveries to subscribers in 24 hours. This applies to CometD clients such as
empApiand to Pub/Sub API clients.
Apex triggers and flows are not counted against the 24-hour delivery allocation in the same way, but they still run under governor limits.
If you exceed a publishing allocation, EventBus.publish returns a SaveResult with isSuccess() set to false. It does not throw an exception. If you skip the isSuccess() check, events can be lost without any error.
Monitoring usage
Three tools help you see how close you are to the limits.
The /limits REST resource reports your remaining allocations, including platform event entries:
GET /services/data/v60.0/limits
Authorization: Bearer <access_token>Look for entries whose names begin with HourlyPublishedPlatformEvents or DailyDeliveredPlatformEvents. Each entry has Max and Remaining values.
The EventBusSubscriber object shows the state of each trigger subscriber:
List<EventBusSubscriber> subs = [
SELECT Name, Type, Position, Retries, LastError
FROM EventBusSubscriber
WHERE Topic = 'Order_Shipped__e'
];
for (EventBusSubscriber s : subs) {
System.debug(s.Name + ' position=' + s.Position + ' retries=' + s.Retries + ' error=' + s.LastError);
}A Retries value that keeps rising, or a LastError that stays set, means the trigger is failing. Compare Position with the newest event's replay ID to see how far the subscriber lags behind.
Setup and Event Monitoring. The platform event usage pages in Setup show published and delivered counts over time. Use them to spot trends before you hit a cap.
Common mistakes
- Calling
EventBus.publishinside a loop instead of passing a list. - Ignoring
SaveResultvalues, so quota failures go unnoticed. - Adding many
empApisubscribers, which multiplies deliveries against the daily allocation. One delivery is counted for each subscriber. - Assuming a failing trigger will stop on its own. It keeps retrying, and
RetriesinEventBusSubscribershows this.
Exercise
Your code publishes 100 events by calling EventBus.publish once for each event inside a for loop. Which governor limit is most at risk, and how do you fix it?
Show answer
The DML statement limit is at risk, because each publish call counts as one DML statement. Build a List of the 100 events and call EventBus.publish once with the list. This uses one statement for the call and 100 DML rows.
Exercise
A trigger subscriber seems stuck. Which object do you query to check, and which fields tell you it is failing?
Show answer
Query EventBusSubscriber filtered by Topic. A rising Retries value and a populated LastError show that the trigger is failing. Position shows the last replay ID processed.