Why is publish-subscribe architecture superior to tight point-to-point coupling for complex enterprise integrations?
Answer
Basics: In point-to-point, system A calls B, C and D directly. A needs their endpoints, formats and availability, and adding a new consumer means changing A. With pub-sub, A publishes an event such as OrderPlaced to a bus, and any interested system subscribes. The bus stores the message, so subscribers that are offline can catch up later.
How it works: In Salesforce the bus is the event bus. Platform events, Change Data Capture and Pub/Sub API are the building blocks. Publishers raise events from Apex, Flow, API or a trigger. Subscribers can be Apex triggers, Flows, LWC (empApi), CometD or gRPC clients. A replay ID lets each subscriber track its own position. Retention is commonly documented as 72 hours (verify for your release).
Advanced / deep dive: Benefits are loose coupling, fan-out to many consumers, burst absorption, and resilience when a consumer is down. Costs are eventual consistency, no direct response to the publisher, harder debugging (you need correlation IDs), and at-least-once delivery, which requires idempotent consumers. Use request-reply (REST or Apex callouts) when the caller needs an immediate answer, and use events when many systems need to learn that something happened. Event contracts are public APIs, so version them carefully. Retention limits mean a long outage needs a resync path from the source of truth.
Code / config example:
Point-to-point: Salesforce -> ERP, Salesforce -> Billing, Salesforce -> Warehouse (3 connections, each custom)
Pub-sub: Salesforce -> [Order_Event__e] -> ERP / Billing / Warehouse (publish once, add subscribers freely)
Hidden gotchas & must-remember points:
- Pub-sub is not a transaction. There is no rollback across subscribers.