Sub topic 2
The event bus and publish/subscribe
This chapter assumes you know the basic Salesforce data model, can read Apex triggers, and have a general idea of messaging. It explains the part of Change Data Capture (CDC) that sits between a record change and the code that reacts to it: the event bus.
Why an event bus
Without CDC, a system that needs to know about Salesforce changes has to poll. It runs a query on a schedule, such as "give me accounts modified since 9:00." Polling wastes API calls when nothing changed. It also delays updates until the next run.
CDC uses publish/subscribe instead. Salesforce publishes a message when a record changes. Any number of consumers receive that message. The publisher does not know who is listening, and the listeners do not query the publisher.
The pieces of the model
- Event: A message that describes something that happened. In CDC, it is a change event that describes a record change.
- Event bus: The Salesforce service that stores events and delivers them to subscribers. It is a separate store from your database tables.
- Channel: A named stream on the bus. Publishers write to it and subscribers read from it. The next chapters cover how CDC channels are named.
- Publisher: The source of events. For CDC, Salesforce is the publisher. You do not write publishing code.
- Subscriber: Any consumer of a channel. Examples are an Apex trigger, a Lightning web component, or an external client.
What the bus guarantees
The bus has properties that shape every design in this tutorial.
- Asynchronous delivery. The event is published after the transaction commits. The code that changed the record has already finished when subscribers run. A subscriber cannot roll back or block the original save.
- Independent subscribers. Each subscriber reads at its own pace. A slow external client does not slow an Apex trigger on the same channel.
- Ordered events. Events from a channel carry increasing replay IDs. Subscribers see them in that order.
- Temporary retention. The bus keeps events for a limited time, so a subscriber that was offline can catch up. Replay IDs and the retention window get their own chapter.
The first property matters most when you compare CDC with a trigger on the object itself. An Apex trigger on Account runs inside the save and can reject it. A trigger on a change event runs afterward and cannot.
Common mistakes
- Expecting a response. Publish/subscribe is one-way. A subscriber cannot return a value to the code that made the change.
- Treating the bus as a database. Events expire. If you need a permanent history, the subscriber must store what it receives.
- Assuming one consumer. Adding a second subscriber does not change the first. Plan for several consumers reading the same events.
Exercise
A team's nightly job queries Salesforce for accounts changed since the last run and copies them to a warehouse. They want updates to arrive within seconds. Name two ways the publish/subscribe model differs from this polling approach, and say what a subscriber cannot do that a before trigger on Account can.
Show answer
Publishers push events when a change commits, so the warehouse does not have to run queries on a schedule and does not wait for the next run. Several subscribers can also read the same channel independently. Because delivery is asynchronous and happens after commit, a subscriber cannot block or roll back the original save, which a before trigger on Account can do.