Sub topic 12
Event-driven integration with Platform Events, CDC, and Pub/Sub API
This chapter assumes you can authenticate with OAuth 2.0 (covered earlier), read JSON, and know the difference between polling an API and receiving a push. It covers the event APIs that let Salesforce notify external systems instead of waiting for them to ask.
Why events instead of polling
With the REST API, a client that wants to know about changes must keep querying. Each poll costs an API call, and the client still sees changes late. An event-driven design reverses this. Salesforce publishes a message when something happens, and subscribers receive it within seconds.
Use events when latency matters, when many systems need the same change, or when polling would burn through your API limits.
The three building blocks
| Building block | What it is | Who defines the message |
|---|---|---|
| Platform Events | Custom messages you design and publish | You |
| Change Data Capture (CDC) | Automatic events for record creates, updates, deletes, and undeletes | Salesforce |
| Pub/Sub API | The gRPC interface used to publish and subscribe to either kind | Salesforce |
Platform Events
A platform event is a custom object whose API name ends in __e. You define its fields, then publish it from Apex, Flow, or an API call. Use it to announce a business fact that is not tied to one record change, such as Order_Shipped__e.
Change Data Capture
CDC publishes a change event whenever a record of a selected object changes. You enable it per object in Setup. Each event carries a header with the entity name, the change type, the record IDs, and the changed fields. For an Account the channel is /data/AccountChangeEvent. Use CDC to keep an external database in sync without writing publishing code.
Pub/Sub API
Pub/Sub API is the recommended way to subscribe from outside Salesforce. It is built on gRPC and HTTP/2, and event payloads use Apache Avro encoding. The client authenticates with the same OAuth access token, plus the instance URL and tenant ID in the request headers. It supports publishing, subscribing, and fetching a schema by ID so the client can decode Avro payloads. The older CometD-based Streaming API still works, but new integrations should prefer Pub/Sub API.
How a subscription works
Each event has a replayId, an increasing position on the channel. The subscriber stores the last replayId it processed. After a disconnect, it resumes from that position so it does not miss events.
Replay and retention
Salesforce keeps events on the bus for a limited window, 72 hours for standard-volume platform events and CDC. A subscriber that is offline longer than the window loses the events in between. Your design needs a fallback, usually a full or incremental sync through the REST API or Bulk API 2.0.
Common errors
- Assuming delivery is exactly once. Delivery is at least once in practice, so make handlers idempotent by keying on the record ID and
replayId. - Forgetting to store the replay position, so a restart replays from the tip and skips events.
- Expecting events to be ordered across different channels. Order holds only within one channel.
- Ignoring allocations. Event publishing and delivery have daily limits that depend on your edition.
- Enabling CDC on every object. Select only the objects you need.
Choosing between them
- Need to mirror record changes? Use CDC.
- Need to announce a custom business signal? Use a platform event.
- Need an external client to receive either? Subscribe through Pub/Sub API.
Exercise
Your warehouse system must learn within seconds whenever an Opportunity is updated. It was polling the REST API every minute and consuming a large share of your daily API limit. Which event feature do you enable, and how does the warehouse client resume safely after a 2-hour outage?
Show answer
Enable Change Data Capture for Opportunity and have the warehouse subscribe to /data/OpportunityChangeEvent through Pub/Sub API. The client saves the replayId of the last event it processed and passes it when it resubscribes. Because 2 hours is inside the 72-hour retention window, it receives the missed events in order.