# What are Platform Events and how do they enable event-driven architecture within Salesforce?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Platform Events, Event-Driven Architecture

Read it on the site: https://www.forcechallenges.com/interview-questions/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:**
1. 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.
2. Publish behavior (set on the definition): Publish After Commit (published only if the transaction succeeds) or Publish Immediately (published regardless of rollback).
3. Subscribe: Apex `after insert` trigger on the event, platform-event-triggered Flow, LWC/Aura via `lightning/empApi`, or external CometD/Pub/Sub API clients.
4. 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.

**Code / config example:**
```apex
// Publish
EventBus.publish(new Order_Shipped__e(Order_Id__c = o.Id, Carrier__c = 'DHL'));

// Subscribe
trigger OrderShippedTrigger on Order_Shipped__e (after insert) {
    List<Task> tasks = new List<Task>();
    for (Order_Shipped__e e : Trigger.new) {
        tasks.add(new Task(Subject = 'Shipped via ' + e.Carrier__c, WhatId = e.Order_Id__c));
    }
    insert tasks;   // bulk-safe
}
```

**Hidden gotchas & must-remember points:**
- Only `after insert` triggers are supported on events.
- Publishing returns success when queued; delivery is asynchronous.
- In tests, call `Test.getEventBus().deliver()` to run subscribers.
- Published events cannot be updated or deleted, and fields are limited in types.
- Replay window is 72 hours for high-volume events; do not rely on the bus as storage.

**Best practice:** Define small, versioned event schemas and make every subscriber idempotent and observable.

---
Source: ForceChallenges · last updated 2026-11-02
