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:
- CDC: Setup > Change Data Capture, select objects; events appear on /data/<Object>ChangeEvent channels (for example AccountChangeEvent).
- Each event has a ChangeEventHeader (entityName, recordIds, changeType, changedFields, nulledFields, diffFields, commitUser, commitTimestamp, transactionKey, sequenceNumber, changeOrigin).
- Platform Event: define Order_Shipped__e with fields, publish with EventBus.publish, a flow, or the API.
- 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:
trigger ContactChangeTrigger on {
Set<Id> ids = <Id>();
(ContactChangeEvent e : Trigger.new) {
EventBus. e.ChangeEventHeader;
(h.getChangeType() == && h.getChangedFields().contains()) {
ids.addAll(h.getRecordIds());
}
}
(!ids.isEmpty()) System.enqueueJob( (ids));
}