Inefficient Apex platform event triggers cause the trigger event processing rate to not keep up with the event publishing rate. To make sure that your triggers run efficiently and avoid delays in event processing, use best practices for writing and configuring your triggers.
Consider one of these tips depending on the trigger conditions.
Condition
Recommendation
The trigger batch size is small and the event publishing rate is high.
The trigger batch size depends on the event publishing rate and the trigger’s business logic. We recommend that you choose the largest batch size that enables the trigger to keep up with the event publishing rate and stay within Apex governor limits. Reducing the trigger batch size when the event publishing rate is high can slow down event processing. Slow event processing causes a lag between published events and processed events. If the trigger batch size is lower than the default, try increasing it to help the trigger process higher volumes of events and catch up with the event publishing rate.
To change the batch size, see Adjust the Platform Event Trigger Batch Size in Trailhead. After changing the batch size, validate it in a test environment and make sure that the trigger runs efficiently with no issues.
Apex Code Best Practices for Platform Event Triggers
Check out these best practices for writing performant Apex triggers that subscribe to platform events and change events and reduce trigger execution time.
Sometimes multiple processes try to get exclusive locks to the same records when performing DML operations on the records. This behavior can happen when using parallel subscriptions based on the partition key selected. To void record locks and contention when using parallel subscriptions, see Choosing a Partition Key.
Caching Data with Platform Cache
If the trigger fetches record data through repetitive queries and the data doesn’t change between trigger executions, consider using Platform Cache to cache the data. Retrieving cached data instead of querying data every time improves the trigger’s throughput. See Platform Cache in the Apex Developer Guide.
Downstream operations that are triggered by a Salesforce record change can result in longer trigger execution times. For example, an account record update can trigger a flow or an object trigger to start. Make sure that the downstream operations don’t slow down the trigger execution.
Apex Code Analysis
Use AI and machine learning models to scan Apex code and get code recommendations. See ApexGuru in Trailhead.
Using Parallel Subscriptions
Use parallel subscriptions if your event publishing rate is high and you’ve implemented best practices for trigger batch size and efficient trigger code but the trigger processing rate is still slow. To speed up trigger execution, configure parallel subscriptions so that the trigger runs in simultaneous parallel executions. Parallel subscriptions apply only to triggers that are subscribed to custom high-volume platform events. See Platform Event Processing at Scale with Parallel Subscriptions for Apex Triggers.
When you configure parallel subscriptions, for the best performance and to avoid record contention and locking, it’s important that you choose the right partition key for your implementation. See Choosing a Partition Key.