Sub topic 11
Handling failures with resume checkpoints and retries
This chapter assumes you can write Apex triggers, know how governor limits and transactions work, and have already written a platform event trigger. If you haven't, read the chapter on subscribing with an Apex trigger first.
Why event triggers need their own failure handling
A platform event trigger runs in its own transaction, separate from the code that published the event. The publisher has already finished, so an exception in the trigger can't roll anything back for the publisher. It also doesn't stop the event from being delivered.
By default, if your trigger throws an unhandled exception, the platform doesn't run the batch again. The events in that batch are not reprocessed, and the failure may go unnoticed. To get a second attempt, you must ask for one.
The platform gives you two tools:
EventBus.RetryableExceptiontells the platform to run the trigger again with the same batch.- A resume checkpoint tells the platform which event you processed last. The retry then starts after that event.
Retrying with EventBus.RetryableException
Throw EventBus.RetryableException when the failure is likely to be temporary. Examples are a row lock or a callout to a service that is briefly unavailable. The platform then retries the trigger.
Retries don't continue forever. The platform retries up to 9 times, after which the trigger stops and the batch is dropped. Treat the retry as a limited chance to get past a temporary problem.
The platform waits between attempts. The delay is not under your control, so don't design around a particular interval.
Adding a resume checkpoint
A retry without a checkpoint starts at the first event of the batch. Events you already handled would be processed again, which can create duplicate records.
Call setResumeCheckpoint with the ReplayId of the event you just finished. On retry, Trigger.new begins with the event after that checkpoint.
trigger OrderShippedTrigger on Order_Shipped__e (after insert) {
for (Order_Shipped__e evt : Trigger.new) {
try {
// Process one event here.
} catch (Exception e) {
throw new EventBus.RetryableException('Retrying event: ' + e.getMessage());
}
// Record progress so a retry resumes after this event.
EventBus.TriggerContext.currentContext().setResumeCheckpoint(evt.ReplayId);
}
}Set the checkpoint after the event is fully processed. If you set it before the work, a failure would skip that event on retry.
Choosing what to retry
Not every error deserves a retry. A retry repeats the same input, so a permanent error fails again until the retries run out.
| Failure | Retry? | Reason |
|---|---|---|
| Record lock on a related record | Yes | The lock is usually released soon. |
| Callout to a service that timed out | Yes | The service may recover. |
| Required field is missing in the event | No | The same event will fail every time. |
| Validation rule rejects the data | No | The data doesn't change between attempts. |
For permanent errors, catch the exception and log it. You could write the failed event's details to a custom log object, so someone can review it later.
Common mistakes
- Processing events in a way that isn't safe to repeat. Even with checkpoints, a retry can cause an event to be handled more than once if the failure happened after your work but before the checkpoint. Make the work idempotent where you can. Idempotent means that doing it twice gives the same result as doing it once.
- Setting the checkpoint inside a catch block or before the work is done.
- Retrying every exception, including ones that can never succeed.
- Putting all events into one DML statement at the end. If that statement fails, you have no per-event progress to record. Either handle failures per event, or accept that the whole batch retries.
Exercise
A trigger processes 200 events and throws EventBus.RetryableException on event 120. You set a resume checkpoint after each successful event. Where does the retry start, and what happens if you hadn't set any checkpoint?
Show answer
With checkpoints, the retry starts at event 120 (the event after the last checkpoint, set after event 119). Without a checkpoint, the retry starts again at event 1 of the batch, so events already handled may be processed twice.