Sub topic 10
Gap and Overflow Events
This chapter assumes you know how change events are structured, what the change types are, and how replay IDs work. Here you will learn what gap and overflow events are and how to recover from them.
Why Gap and Overflow Events Exist
Change events normally arrive one per record change, each with a full ChangeEventHeader. Sometimes Salesforce cannot produce a normal event. In that case it sends a special event so your subscriber knows its view of the data may be incomplete. You can't ignore these events. If you do, your external copy of the data will silently drift from Salesforce.
There are two kinds, and both are identified by the changeType value in the header.
| Event | `changeType` | What it means |
|---|---|---|
| Gap event | `GAP_CREATE`, `GAP_UPDATE`, `GAP_DELETE`, `GAP_UNDELETE` | Salesforce hit an internal error or could not generate the change event. One or more changes were missed. |
| Overflow event | `GAP_OVERFLOW` | More than 100,000 changes happened in a single transaction. Salesforce sent a single overflow event in place of the individual events. |
What a Gap Event Contains
A gap event replaces a normal change event for one record. The header carries the record ID and the type of change that was missed. It does not carry the changed field values. Fields in the body are null, apart from a few header-level details.
{
"ChangeEventHeader": {
"entityName": "Account",
"recordIds": ["001XXXXXXXXXXXXXXX"],
"changeType": "GAP_UPDATE",
"changedFields": [],
"commitTimestamp": 1700000000000,
"commitNumber": 1700000000000000
}
}Because the field values are missing, you can't apply the change directly. You have to fetch the current state of the record.
What an Overflow Event Contains
An overflow event is sent when a transaction changes more than 100,000 records. Salesforce publishes one GAP_OVERFLOW event for the affected entity instead of individual events after the limit is reached. It does not list every record that changed. Your subscriber knows only that a large number of changes occurred for that object, so you must reconcile the whole object, or a bounded slice of it.
placeholderRecovering From Gaps and Overflows
Follow the same pattern each time. First detect the event, then re-read the affected data from Salesforce, then resume normal processing.
- Check
changeTypeon every event. If it starts withGAP_, route it to recovery logic instead of your normal handler. - For
GAP_CREATE,GAP_UPDATE, andGAP_UNDELETE, query each ID inrecordIdsand upsert the current values into your copy. - For
GAP_DELETE, check whether each record still exists. Remove it from your copy, or mark it deleted. - For
GAP_OVERFLOW, query the object for records whoseLastModifiedDateis later than your last successful sync, and upsert them. Also compare IDs to find deletions, since deleted records don't appear in a normal query.
The following Apex trigger shows the detection step. It sends gap events to a queueable job instead of handling them like normal changes.
trigger AccountChangeTrigger on AccountChangeEvent (after insert) {
Set<Id> idsToResync = new Set<Id>();
Boolean needsFullResync = false;
for (AccountChangeEvent event : Trigger.new) {
EventBus.ChangeEventHeader header = event.ChangeEventHeader;
if (header.changeType == 'GAP_OVERFLOW') {
needsFullResync = true;
} else if (header.changeType.startsWith('GAP_')) {
for (String recordId : header.recordIds) {
idsToResync.add(recordId);
}
}
}
if (needsFullResync) {
System.debug('Overflow received. Schedule a full reconciliation.');
} else if (!idsToResync.isEmpty()) {
System.debug('Gap received. Re-read records: ' + idsToResync);
}
}In a real integration, replace the System.debug calls with code that starts your reconciliation job.
Common Mistakes
- Handling only
CREATE,UPDATE,DELETE, andUNDELETE, and dropping everything else. - Replaying from an older replay ID to "fix" a gap. The missed changes were never published, so replay can't bring them back.
- Reconciling with
LastModifiedDatealone and missing deletions. - Running reconciliation in the same subscriber thread, which stalls new events.
Exercise
A subscriber receives an event with changeType GAP_UPDATE and one ID in recordIds. What should it do, and why can't it apply the event directly?
Show answer
It should query the current record from Salesforce by that ID and upsert the result into its copy. The gap event has no changed field values, because Salesforce could not generate the full change event, so applying it directly would lose or blank data. Replaying will not help since the missed change was never published.