Sub topic 10
Replay IDs and the retention window
This chapter assumes you can write basic Apex triggers and Lightning Web Components. It also assumes you know how to define a platform event and publish it. Here you learn how a subscriber can pick up events it missed, and how long Salesforce keeps those events.
What a replay ID is
Every platform event message that Salesforce publishes gets a replay ID. A replay ID is an opaque number that identifies the event's position in the event stream. Later events have larger replay IDs. You can compare two IDs to see which event came first, but you should not assume the IDs are consecutive and you should not do arithmetic on them.
A subscriber can store the replay ID of the last event it processed. After a disconnect, it can ask the event bus to resend everything published after that ID.
The retention window
The event bus does not keep events forever. Salesforce stores published high-volume platform events for 72 hours (3 days). This period is the retention window. Within it, a subscriber can replay events. After it, the events are gone and no replay ID can bring them back.
Check the current retention period for your event type in the Salesforce documentation, because it can differ by event type and edition.
Replay options
When a client subscribes, it chooses where to start reading:
| Replay ID | Meaning |
|---|---|
| `-1` | Receive only new events published after the subscription starts. |
| `-2` | Receive all events still in the retention window, then new events. |
| A specific ID | Receive events published after that ID, then new events. |
The specific ID is the one you saved from the last event you handled.
Replay in a Lightning Web Component
The empApi example earlier in this tutorial subscribes with -1. That means a component that was closed while events were published sees none of them. To resume, pass a stored replay ID instead.
import { LightningElement } from 'lwc';
import { subscribe, unsubscribe, onError } from 'lightning/empApi';
export default class OrderShippedReplay extends LightningElement {
channelName = '/event/Order_Shipped__e';
subscription = null;
connectedCallback() {
onError((error) => {
console.error('empApi error', JSON.stringify(error));
});
// Read the last processed replay ID, or fall back to -1 (new events only).
const saved = window.sessionStorage.getItem('orderShippedReplayId');
const replayId = saved !== null ? Number(saved) : -1;
subscribe(this.channelName, replayId, (message) => {
const payload = message.data.payload;
console.log('Order shipped: ' + payload.Order_Number__c);
// Save the position so the next subscription resumes after this event.
window.sessionStorage.setItem(
'orderShippedReplayId',
String(message.data.event.replayId)
);
}).then((response) => {
this.subscription = response;
});
}
disconnectedCallback() {
if (this.subscription) {
unsubscribe(this.subscription, () => {});
}
}
}The replay ID of each message is in message.data.event.replayId. This example uses sessionStorage, which lasts only for the browser tab. For a durable position you would store the ID on a record or in another persistent place.
Replay IDs in Apex triggers
Apex trigger subscribers read the replay ID from the ReplayId field of each event in Trigger.new. You do not store it to resume after a disconnect, because Salesforce tracks the trigger's position for you. You use ReplayId to set a resume checkpoint after a failure, which a later chapter covers.
Common errors
- Replaying past the window. If you pass an ID older than the retention window, you receive only what is still retained. You do not get an error that tells you events were lost, so compare the first replay ID you receive with the one you stored.
- Storing the ID before processing. If you save the ID first and then fail, the event is skipped on resume. Save it after the work succeeds.
- Treating
-2as free. Replaying everything retained can deliver a large number of old events. Make your handler safe to run twice for the same event. - Assuming replay IDs are portable. An ID from one event type or org means nothing for another.
Exercise
A component subscribes with replay ID -1, and its page is closed for two hours while events are published. When the page is reopened with the same code, does it receive those events? What change would let it receive them?
Show answer
No. Replay ID -1 delivers only events published after the subscription starts. To receive the missed events, the component must save the replay ID of the last event it processed and pass that ID when it subscribes again. The events must still be inside the retention window, which is 72 hours here, so a two-hour gap is fine.