Sub topic 3
Replay IDs, Retention, and Reliable Change Data Capture
This chapter assumes you understand Salesforce objects, Apex triggers, JSON, and basic publish/subscribe messaging.
Replay IDs and the retention window
A replay ID identifies an event position on the Salesforce event bus. It is opaque: do not calculate the next ID or assume IDs are consecutive. Store the latest successfully processed ID with your integration checkpoint.
When a subscriber reconnects, it requests events after its stored replay ID. The event bus can replay only events still inside its retention window, which is commonly 72 hours. A subscriber that is offline longer may no longer be able to recover by replaying events.
Common replay choices include:
| Replay choice | Meaning |
|---|---|
| `-1` | Receive only new events |
| Stored replay ID | Resume after the last processed event |
| Earliest available position | Start from the oldest event still retained |
The exact API syntax differs between empApi, CometD, and Pub/Sub API.
Gap and overflow events
A gap means the subscriber cannot receive every event in the expected sequence. An overflow event indicates that volume exceeded what the channel or subscriber could deliver. Neither condition should be treated as a normal record update.
Use the event as a signal to reconcile. Query Salesforce for records changed since a saved timestamp, compare them with the external system, and then resume event processing. Make reconciliation idempotent so running it twice has the same result.
Custom channels and selecting entities
Standard channels use names such as /data/AccountChangeEvent. A custom channel can group selected change-event entities and apply filters. Use one when subscribers need a focused stream instead of every event from several objects. Keep filters simple and document which entities and fields the channel includes.
Security and field-level access
Treat change events as sensitive data. Event delivery is not a replacement for authorization at the destination. Use a dedicated integration user with only the required object, field, and API permissions. Confirm how the chosen subscriber API applies sharing and field-level security, especially for Apex, because Apex execution context can differ from a user's interactive access. Do not place confidential fields on a broad channel without verifying the actual payload.
Allocations and limits
CDC has event-volume allocations, retention limits, subscriber limits, and platform limits. A high-volume object can exhaust an allocation or create a backlog. Monitor event usage, process events in batches where possible, and design a replay-plus-reconciliation path before production.
Testing change event triggers
Apex tests must enable CDC and explicitly deliver queued events:
@isTest
private class AccountChangeTriggerTest {
@isTest
static void createsTaskWhenPhoneChanges() {
Test.enableChangeDataCapture();
Account accountRecord = new Account(Name = 'Acme');
insert accountRecord;
Test.getEventBus().deliver();
accountRecord.Phone = '555-0100';
update accountRecord;
Test.getEventBus().deliver();
System.assertEquals(
1,
[SELECT COUNT() FROM Task WHERE WhatId = :accountRecord.Id]
);
}
}Integration patterns and common pitfalls
Use CDC for asynchronous synchronization, not immediate transaction responses. Make consumers idempotent, checkpoint after success, monitor lag, and reconcile after gaps. Avoid assuming event order across objects, assuming every update contains every field, or using a replay ID as a business identifier.
Exercise
A consumer was offline for four days and its stored replay ID is no longer retained. What should it do?
Show answer
Run a reconciliation query or bulk comparison using a saved timestamp or other watermark, repair the destination, then resume with the earliest available event or a new subscription.