Sub topic 15
When to use platform events and when to use something else
This chapter assumes you can publish events from Apex, write an after insert event trigger, and read the limits and monitoring chapter. It gives you criteria for deciding whether a platform event is the right tool for a given requirement.
What platform events are good at
A platform event carries a custom payload that you define, and it is not tied to any record change. Publishers and subscribers do not know about each other. Subscribers can be Apex triggers, flows, Lightning Web Components, or external clients using Pub/Sub API. Subscribers can replay events from the retention window (72 hours for high-volume events) after an outage.
Choose platform events when:
- A business occurrence has no single record behind it, such as "shipment left the warehouse".
- Several independent consumers need the same message.
- You want the subscriber's work to run outside the publisher's transaction and limits.
- An external system needs to receive or send messages in near real time.
Alternatives and what they cover
| Option | Best fit | Main difference from platform events |
|---|---|---|
| Change Data Capture | Syncing record creates, updates, deletes, and undeletes to other systems | Salesforce generates the events and you cannot define the payload |
| Direct Apex or flow logic | Work that must succeed or fail together with the triggering transaction | Same transaction, so a failure rolls everything back, and there is no decoupling |
| Queueable Apex | Deferred work that one component owns | No fan-out, no external subscribers, no replay |
| Outbound messages | Simple push of record fields to one SOAP endpoint | Workflow-based and limited to that one endpoint |
| PushTopic and generic streaming | Existing legacy integrations | Older Streaming API features. Avoid them for new designs. |
| Custom notifications | Telling a user something happened | Delivers a UI or mobile notification, not a data message |
A decision path
Trade-offs to weigh
Delivery is asynchronous. A subscriber trigger runs later in its own transaction, so the publisher cannot read its result. If the publisher needs an answer, use a synchronous call.
Rollback behavior depends on the publish behavior. With publish after commit, a rolled-back transaction discards the event. With publish immediately, the event stays published even if the transaction rolls back. Any design that treats an event as proof that data was saved needs publish after commit.
Events are not a queue you can inspect or edit. You cannot update or delete a published event, and a failed subscriber does not block newer events beyond what you configure with retries and checkpoints.
Limits apply on both sides. Publishing counts against the hourly publishing allocation and the DML limits of the transaction. Delivery counts against a 24-hour allocation shared by non-Apex subscribers. Estimate peak event volume before you commit to the design.
Keep payloads small. Send identifiers and a few key fields, and let the subscriber query for the rest.
Exercise
Your team must send each Opportunity stage change to a data warehouse, and a billing service must also react when a contract is signed, which spans several records. Which option fits each need, and why?
Show answer
Use Change Data Capture for the stage changes. Salesforce generates the events from record changes, so no publishing code is needed. Use a custom platform event for contract signing, because it is a business occurrence with a custom payload that does not map to one record change, and several consumers can subscribe to it.