Sub topic 9
Replay IDs and the retention window
This chapter assumes you know how change events are named and structured, and that you have seen a subscriber such as the empApi component from the previous chapter. Here you will learn how a subscriber resumes after a disconnect without losing events.
What a replay ID is
Every change event gets a replay ID when it is published to the event bus. The replay ID is an opaque value that increases with each event on a channel. A subscriber stores the last replay ID it processed. After a disconnect, it sends that value back to ask for everything that came after it.
Treat the value as a bookmark. Do not parse it, do not assume consecutive numbers, and do not compare IDs from different channels.
The retention window
The event bus keeps change events for 3 days. A subscriber can replay any event inside that window. Events older than the window are gone, and no replay ID can bring them back.
This limit shapes your design. A client that is offline for 4 days cannot catch up from the bus alone. It needs a separate way to reconcile, which the next chapter covers.
Choosing a replay value
When a client subscribes, it passes a replay value. The value tells the bus where to start.
| Replay value | Behavior |
|---|---|
| -1 | Deliver only new events published after the subscription starts. |
| -2 | Deliver all events still in the retention window, then new events. |
| A stored replay ID | Deliver events published after that ID, then new events. |
The empApi example from the previous chapter used -1. That is fine for a demo, but it loses any event published while the component is not subscribed.
import { subscribe } from 'lightning/empApi';
const CHANNEL = '/data/AccountChangeEvent';
export function subscribeFrom(lastReplayId, handler) {
// Use the stored ID if you have one, otherwise start with new events only.
const replayId = lastReplayId !== undefined ? lastReplayId : -1;
return subscribe(CHANNEL, replayId, (message) => {
handler(message);
// Persist this value only after the handler succeeds.
const latest = message.data.event.replayId;
console.log('Processed replay ID', latest);
});
}Where to store the replay ID
Save the replay ID after you finish processing an event, not before. If you save first and the handler fails, the event is skipped on the next resume.
Storage depends on the client:
- An external service can use a database row or a file.
- A Lightning component has no durable storage of its own. It can keep the value in a custom object or custom setting, or accept that it only needs live events.
- A server-side subscriber should store one replay ID per channel.
Because resuming sends events after the stored ID, you may see an event twice if a crash happens between processing and saving. Make handlers idempotent. Use the recordIds and transactionKey values in the header to detect repeats.
Common errors
- Subscribing with -1 after every restart. The client silently misses events published while it was down.
- Using -2 on every start. The client reprocesses up to 3 days of events each time. Use it only for a first load or a deliberate rebuild.
- Storing a replay ID older than the window. The bus cannot honor it. Plan to reconcile instead of assuming the resume worked.
- Sharing one stored ID across channels. Each channel has its own sequence.
Exercise
A subscriber crashed after processing the event with replay ID 250 but before saving it. On restart it subscribes with its last saved ID, 240. What happens, and how should the handler be written?
Show answer
The bus redelivers the events after 240, including the one already processed as 250. The handler should be idempotent, for example by checking the recordIds and transactionKey in the header, so repeated processing causes no duplicate effects. Saving the replay ID only after processing succeeds keeps the window for duplicates small and avoids skipped events.