Sub topic 3
Replay IDs, Retention, and Reliable Platform Event Subscribers
Assume you know basic Apex triggers, DML, custom fields, and governor limits. This chapter focuses on recovering events and choosing an eventing design.
Replay IDs and the Retention Window
A replay ID identifies an event position in the event bus. A subscriber can store the latest processed replay ID and request events after that position. Replay IDs are opaque values. Do not treat them as sequential numbers.
Salesforce retains platform events for a limited window, commonly 72 hours. A subscriber can recover missed events only while those events remain available. If the stored replay ID is older than the retention window, the subscriber must rebuild its state from another source, such as Salesforce records.
Resume Checkpoints and Retries
A platform event trigger processes a batch of events. A resume checkpoint records progress within that batch. If processing fails, throwing EventBus.RetryableException asks Salesforce to retry from the checkpoint.
trigger OrderShippedTrigger on Order_Shipped__e (after insert) {
for (Order_Shipped__e eventRecord : Trigger.new) {
try {
// Perform an idempotent update or insert here.
System.debug(eventRecord.Order_Number__c);
EventBus.TriggerContext.currentContext()
.setResumeCheckpoint(eventRecord.ReplayId);
} catch (Exception error) {
throw new EventBus.RetryableException(
'Retrying event: ' + error.getMessage()
);
}
}
}Make processing idempotent. Idempotent processing produces the same result when Salesforce delivers an event more than once.
Testing Publishers and Subscribers
Publish events between Test.startTest() and Test.stopTest(). Salesforce delivers the events to subscriber triggers when stopTest() runs.
Test.startTest();
Database.SaveResult result = EventBus.publish(
new Order_Shipped__e(
Order_Number__c = '00012345',
Status__c = 'Shipped'
)
);
Test.stopTest();
System.assert(result.isSuccess());
System.assertEquals(
1,
[SELECT COUNT() FROM Task
WHERE Subject = 'Follow up on order 00012345']
);External Subscribers and Pub/Sub API
External applications can subscribe with Salesforce Pub/Sub API. The API uses gRPC and supports event schemas, messages, and replay options. The application should persist replay IDs, acknowledge processed messages, and handle retention gaps with a deliberate recovery process.
Limits and Monitoring
Platform event allocations limit how many events an organization can publish and deliver. Monitor event usage, failed deliveries, trigger errors, and subscriber lag. Bulkify publishers and subscribers, and avoid publishing unnecessary events.
Choosing an Eventing Option
| Use case | Suitable option |
|---|---|
| Loosely coupled Salesforce or external subscribers | Platform Events |
| Immediate synchronous response | Apex callout or REST request |
| Data replication with record-change details | Change Data Capture |
| Simple in-transaction automation | Flow or Apex trigger |
Exercise
Why should a subscriber save a replay ID only after successful processing?
Show answer
Saving it afterward prevents a failed event from being skipped during recovery.