What are Platform Events and how do they enable event-driven architecture within Salesforce?
Answer
Basics: Instead of one component calling another directly, a publisher writes an event (for example Order_Shipped__e) and walks away. Subscribers independently react. You define the event under Setup, Platform Events, add custom fields, then publish and subscribe. API names end with __e.
How it works:
- Publish: Apex
EventBus.publish(event or list), Flow (Create Records on the event object), REST API insert, or an external client via Pub/Sub API. - Publish behavior (set on the definition): Publish After Commit (published only if the transaction succeeds) or Publish Immediately (published regardless of rollback).
- Subscribe: Apex
after inserttrigger on the event, platform-event-triggered Flow, LWC/Aura vialightning/empApi, or external CometD/Pub/Sub API clients. - Events are stored on the bus for a replay window (72 hours) and each carries a
ReplayId.
Advanced / deep dive: Triggers on events run in their own transaction as the Automated Process user unless you configure a different run-as user and batch size through PlatformEventSubscriberConfig; batches can contain up to 2,000 events by default. Throw EventBus.RetryableException to retry (up to 9 times). Failures in a subscriber never affect the publisher. Delivery is at-least-once and ordered by ReplayId, so subscribers should be idempotent. Allocations govern publish rates and delivery per 24 hours based on edition and add-ons (verify current numbers in the Platform Events limits docs). Platform events are high-volume by default for new definitions; legacy standard-volume events are being phased out (verify). Compared with Change Data Capture (automatic events for record changes) platform events are custom business messages you define. Versus Outbound Messages, they are not tied to Workflow and use REST/gRPC instead of SOAP.