Introduction
This post is an extension of a Question on Salesforce Stack Exchange and the discussion around the same. With so much attention on this topic, I thought of putting a comparison of when to use Platform Events vs. APIs (REST/SOAP) when it comes to Integration. The post describes the basics of both features and how they compare with each other and scenario when those are used.
If you happen to come across this post, do leave your feedback and recommendations.
Let’s Get Started
What is Platform Event?
Simply speaking, it is Salesforce’s approach of implementing an Event Driven Architecture. While I am not going into details of the architecture itself, this approach leads towards a Pub/Sub (Publisher/Subscriber) model. The model establishes a “Publisher” which publishes any data change using events (or messages), which is subscribed by one or more “Subscribers” which listen on those events and accordingly take action.
So When Do I Use It?
Whenever you want to implement a Pub/Sub model. As an example, let’s say you want to integrate Salesforce with multiple External Systems, and that you only want the External Systems to receive data from Salesforce whenever say an Opportunity reaches to stage Qualified (while you can use callouts/outbound messaging but that will mean Salesforce takes care of every callout in that case), this is where you can use Platform Events.
You can also use Platform Events when you want an External System notify Salesforce to act upon something, where the External Event “Publishes” the change on the Salesforce Event Bus and then Salesforce listens to those Events and acts upon accordingly.
How does Platform Event compare to REST/SOAP APIs?
There is no direct relationship between Platform Event or REST/SOAP APIs. They co-exist serving different purposes.
REST/SOAP APIs are way to expose a “(Web) Service” over HTTP. That means when someone calls an API, the caller expects certain details to be transferred either to them or to Salesforce based on the request. As an example, you provide an Account Id to get details back from an Account or you write to Salesforce objects on certain conditions. A Web Service is targeted to provide information around the data as compared to what an Event message consists of. While you can always transfer data using Platform Events, but consider if you need to transfer a full Opportunity details – you will end up creating those many fields on a Platform Event object which already exists on an Opportunity, definitely not a good design!
And then and most importantly, Platform Events are “transient” in nature, i.e., they are retained only for 24 hours, whereas a Service is always available to an end system is available On-Demand.
A very basic image to highlight how both Platform Event and Services are patterned in Salesforce.
When should I use Platform Events vs. APIs?
Platform Events
- When you want to design a Pub/Sub integration model
- When you don’t want to transfer lot of data
- When you only want to “notify” a subscriber with an event or message with minimal information for them to act upon
- When you want your notification to be transient in nature
Remember, even in case of Platform Events, to transfer data on a larger scale, you still need APIs.
REST/SOAP APIs
- When you are building a true Web Service
- When you are dealing with data instead of a simple notifications
- When you want the service/data available across the lifecycle
Comments
Post a Comment