# How do ReplayID values ensure reliable message replaying and fault recovery during event subscriber disconnects?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, ReplayID, Fault Recovery

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-replayid-values-ensure-reliable-message-replaying-and-fault-recovery-during-event-subscriber

## Answer

**Basics:** Subscribers disconnect for many reasons: deployments, network issues, outages. ReplayId is the bookmark. When subscribing you choose a replay option: `-1` means only new events from now (the tip), `-2` means all events still stored, and a specific number means events after that ID.

**How it works:**
1. The event bus assigns each event a ReplayId (ascending, not necessarily contiguous).
2. The client processes the event, then durably stores its ReplayId (database, not memory).
3. After a disconnect, the client resubscribes with the stored ReplayId.
4. Salesforce replays retained events with a higher ReplayId, then continues live.
In Pub/Sub API the equivalents are replay presets LATEST, EARLIEST and CUSTOM (with a replay_id).

**Advanced / deep dive:** Retention: high-volume platform events and Change Data Capture events are retained for 72 hours; PushTopic and Generic Streaming events for 24 hours; legacy standard-volume platform events also had 24 hours (retiring; verify against your release). Anything older is gone, so a subscriber down longer than the window must reconcile by querying the source data. Semantics are at-least-once: a crash between processing and saving the ReplayId produces duplicates on resume, so processing must be idempotent. Store the ReplayId after successful processing, not before. ReplayIds are specific to a channel and org (sandbox refresh or a different org yields different values). Apex triggers on events manage position automatically; use `EventBus.TriggerContext.currentContext().setResumeCheckpoint(replayId)` to control retries and checkpoints. LWC `lightning/empApi` subscribe accepts a replayId. Use a stale-checkpoint alert: if the stored ReplayId is older than the retention window, trigger a full resync.

**Code / config example:**
```javascript
// LWC using lightning/empApi: -2 = replay everything retained, -1 = new only
import { subscribe } from 'lightning/empApi';
const sub = await subscribe('/event/Order_Shipped__e', lastReplayId ?? -1, msg => {
    saveReplayId(msg.data.event.replayId);    // persist AFTER handling
});
```
```json
{"channel":"/meta/subscribe","subscription":"/event/Order_Shipped__e",
 "ext":{"replay":{"/event/Order_Shipped__e": 1337}}}
```

**Hidden gotchas & must-remember points:**
- "Zero message loss" is only true inside the retention window.
- Replay delivers duplicates by design; dedupe on ReplayId or a business key.
- A very old ReplayId does not raise an obvious error: you may silently miss older events.
- `-2` can replay a large backlog; plan throughput.
- Retention differs by event type and may change by release; verify the current documented value.

**Best practice:** Persist the ReplayId after successful processing, make handlers idempotent and add a periodic reconciliation for outages longer than the window.

---
Source: ForceChallenges · last updated 2026-11-02
