Event-driven integration: Platform Events, Change Data Capture and Pub/Sub API
Publishing and subscribing to business events and record changes, replay and retention, delivery guarantees, and when events beat callouts.
Simple explanation
Events decouple producers from consumers. The producer publishes once; any number of subscribers react, in their own transactions, at their own pace.
Event types
- Platform Events (
__e): custom business events you define (Order_Ready__e). Published from Apex (EventBus.publish), Flow, or the APIs. Choose publish after commit (only if the transaction succeeds) or publish immediately (for example logs that must survive a rollback). - Change Data Capture (CDC): Salesforce publishes record change events (create, update, delete, undelete) for selected objects, including which fields changed. Ideal for keeping external systems in sync without polling.
- Standard platform events published by Salesforce (for example
BatchApexErrorEvent, real-time event monitoring events).
Subscribers
- Inside Salesforce: Apex triggers on the event, platform-event-triggered flows, and LWC via
lightning/empApi. - Outside: the Pub/Sub API (gRPC) for publishing and subscribing, CometD-based Streaming API (older clients), and Event Relays that forward events to Amazon EventBridge.
Reliability: events are retained for a period (high-volume events are kept for 72 hours) and carry a replay ID, so a subscriber that was offline can resume from where it left off β verify retention per event type. Delivery is at least once, so consumers must be idempotent.
Real-world analogy
Platform events are a radio broadcast. The station (publisher) doesn't know who's listening. Anyone tuned in (subscribers) hears the announcement; if you missed it, you can rewind within the station's recording window (replay ID, retention). Change Data Capture is the station automatically announcing every change to the record books.
Worked example
Publish an event after commit from Apex, and subscribe with a trigger that hands off to a queueable:
// Publisher: one event per opportunity, published only if the transaction commits (event's publish behaviour)
List<Order_Ready__e> events = new List<Order_Ready__e>();
for (Opportunity opp : wonOpportunities) {
events.add(new Order_Ready__e(Opportunity_Id__c = opp.Id, Idempotency_Key__c = opp.Id + ':' + opp.LastModifiedDate.getTime()));
}
List<Database.SaveResult> results = EventBus.publish(events);
// Subscriber: runs in its own transaction, as the Automated Process user by default
trigger OrderReadySubscriber on Order_Ready__e (after insert) {
Set<Id> oppIds = new Set<Id>();
for (Order_Ready__e e : Trigger.new) {
oppIds.add(e.Opportunity_Id__c);
}
System.enqueueJob(new ErpOrderSubmissionJob(oppIds));
// Checkpoint so a failure later in this batch resumes after the last processed event
EventBus.TriggerContext.currentContext().setResumeCheckpoint(Trigger.new[Trigger.new.size() - 1].ReplayId);
}
Diagram
Key points
- Events decouple producers and consumers; each subscriber has its own transaction.
- Platform Events for business events; CDC for record changes without polling.
- Publish after commit vs publish immediately: choose per use case.
- External subscribers use the Pub/Sub API (gRPC) or Event Relays.
- Replay IDs and retention let subscribers catch up; delivery is at least once β idempotent consumers.
- Event allocations (publishing and delivery) are org limits: size them β verify for your edition.
Limits and gotchas
- Events are not a database: they expire after the retention window. Persist what you need.
- Ordering is guaranteed only within certain scopes; don't assume global ordering across event types.
- Event trigger subscribers run as the Automated Process user unless a running user is configured; sharing and record ownership behave differently.
- Daily delivery allocations (especially to external subscribers) can be exceeded by chatty CDC on busy objects; select fields and objects carefully.
- Testing needs
Test.getEventBus().deliver()to deliver events in Apex tests.
π€ How to say this in an interview
Use events whenever the interviewer's scenario has several consumers, unreliable consumers, or "notify when something changes". Name replay and idempotency without being asked, and contrast CDC (record changes) with custom platform events (business meaning).
Common follow-ups
CDC or a custom platform event to tell ERP that an order is ready?
A custom platform event: it carries business meaning ('order ready') at the right moment, with only the fields ERP needs. CDC is better for generic replication of record changes to a data platform.
How do you handle a subscriber that was down for a day?
On restart it resubscribes from its last stored replay ID and processes the backlog within the retention window. If it was down longer than retention, run a reconciliation (for example a Bulk API query of records changed since the last processed timestamp).