Apex Trigger Best Practices

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. Fine-Tuning the Trigger Batch Size
The trigger batch size is okay, but the trigger doesn’t catch up with the event publishing rate. Apex Code Best Practices for Platform Event Triggers
The trigger code is efficient and batch size is okay, but the trigger doesn’t catch up with the event publishing rate. Using Parallel Subscriptions

Fine-Tuning the Trigger Batch Size

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.

Bulkification of Apex Triggers

Consider bulkifying SOQL and DML operations in Apex triggers. See Bulk Apex Triggers in Trailhead and Trigger and Bulk Request Best Practices in the Apex Developer Guide.

Record Locking and Contention

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.

Resilient Platform Event Triggers

Write Apex triggers that are resilient to errors and resume after exceptions, such as limit exceptions or uncaught exceptions, occur during event processing. See Apply Best Practices for Writing Platform Event Triggers in the Platform Events Debugging module in Trailhead.

Automation Triggered by Record Changes

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.

Important