# How does Change Data Capture (CDC) differ from custom Platform Events in tracking database modifications?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Change Data Capture, CDC Streaming

Read it on the site: https://www.forcechallenges.com/interview-questions/how-does-change-data-capture-cdc-differ-from-custom-platform-events-in-tracking-database-modifications

## Answer

**Basics:** Both are event streams delivered after commit. CDC answers "what changed in the database?" with no code to publish. Platform events answer "something business-relevant happened" and carry exactly the fields you define, so they can represent things that are not a single record change (for example OrderShipped or a message from an external system).

**How it works:**
1. CDC: Setup > Change Data Capture, select objects; events appear on /data/<Object>ChangeEvent channels (for example AccountChangeEvent).
2. Each event has a ChangeEventHeader (entityName, recordIds, changeType, changedFields, nulledFields, diffFields, commitUser, commitTimestamp, transactionKey, sequenceNumber, changeOrigin).
3. Platform Event: define Order_Shipped__e with fields, publish with EventBus.publish, a flow, or the API.
4. Subscribers: Apex triggers (run as Automated Process user), event-triggered flows, lightning/empApi, CometD, Pub/Sub API.

**Advanced / deep dive:** Both retain events for 72 hours for replay. CDC is a coarse, data-shaped feed; consumers must reconstruct the meaning (an UPDATE with certain changedFields). It also emits GAP and OVERFLOW events when it cannot send full detail, so consumers must handle them by re-querying. Selected-entity limits apply by edition (a small default number of objects, more with an add-on; verify). Platform events are decoupled and versionable contracts, publishable from external systems, suitable for orchestration, but you must publish them (and keep that in sync with transactions). Publish behavior: Publish After Commit (default) or Publish Immediately. Use CDC for replication, caching and data sync; use platform events for domain events and cross-system commands. Both count toward event delivery and publish allocations.

**Code / config example:**
```apex
trigger ContactChangeTrigger on ContactChangeEvent (after insert) {
    Set<Id> ids = new Set<Id>();
    for (ContactChangeEvent e : Trigger.new) {
        EventBus.ChangeEventHeader h = e.ChangeEventHeader;
        if (h.getChangeType() == 'UPDATE' && h.getChangedFields().contains('Email')) {
            ids.addAll(h.getRecordIds());   // collect, then do one bulk operation
        }
    }
    if (!ids.isEmpty()) System.enqueueJob(new EmailSyncJob(ids));
}
```

**Hidden gotchas & must-remember points:**
- CDC triggers run as Automated Process, so use commitUser in the header to identify the origin.
- Replay retention is 72 hours.
- Handle GAP and OVERFLOW events by re-querying.
- Custom platform events require you to publish them; CDC does not.
- CDC entity count is limited by license; verify.
- In tests use Test.enableChangeDataCapture() and Test.getEventBus().deliver().

**Best practice:** Use CDC for data replication and platform events for business events; do not use either as a guaranteed queue without handling replay and failures.

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