Apex Rest Vs Platform Events for Inbound SF integration which one to choose?

Am in a design dilemma in choosing which would be the best approach in the Integration which we would be doing shortly.

To give a brief idea, the external system wants to send JSON data to SF. We would validate the JSON data and create a few sets of records.

So we have 2 options:

  1. Using Apex Rest : The age-old world accepted Rest based Apex web services. It can do exactly what the API wants.

  2. Using Platform Events : Event-Driven Decoupled Architecture having Messaging bus. This will involve sending JSON in Event’s Custom Text area field and then parsing it in Event’s trigger.

What should be the deciding factor, in what should be used for this Integration?

I have researched and came with few pointers.

  1. Error/Exception Feedback to external System : Rest Api being Synchronous, can convey this back in the same transaction (realtime), Platform events being Async not so much(Might have to rely on emails/or create new platform error events that external systems will subscribe.

  2. Retry : Yes we can have retry in platform events, but do we really need it here in this Integration? If its a Validation Exception or Nullpointer exception it will still be there no matter how many times you retry. The only advantage I see is If the exception is something like “UNABLE_TO_LOCK_ROW”, in that case, yes retry functionality of platform events works, but here we are just inserting few records, so am pretty much sure that record locking wont be happening.

  3. Replay Past Events : SF can’t replay past events via trigger or flow/process.

  4. Request JSON Size : Rest API can accept JSON till 6MB, whereas for Platform events its just 1MB,so Rest API can be used for more bulkified JSON records.

  5. API calls and Event Limits : When I publish a platform event I am consuming 2 limits, 1 API calls to Publish events and Platform Events Hourly limit. Whereas in Rest API call its just API calls.

Can anyone point out the design consideration I am missing, plus what points I should consider in selecting the best approach.

Answer

UPDATE

I have also discussed this topic with some more detail on on my blog here.


Based on your requirements where:

External System will push data in Salesforce.

My recommendation is to construct REST APIs for this purpose which the External Systems can utilize to send data to Salesforce. Platform Events are more targeted towards a Pub/Sub model, where you may want to “trigger” a notification from/to Salesforce to all subscribers and based on that notification the subscriber then takes necessary action. Excerpt from the documentation:

Platform events simplify the process of communicating changes and responding to them without writing complex logic. Publishers and subscribers communicate with each other through events. One or more subscribers can listen to the same event and carry out actions.

If at all you want the External System to “act upon” only when an event has occurred in Salesforce, you can then have Platform Events in this case, where the External System (the subscriber) is subscribed to the events in Salesforce and then sends data to Salesforce. Even in this case, you still need an API exposed for the External System to send data in Salesforce.

One of the most important factors to consider here is what is your integration approach. If you want a more “traditional integration”, say point to point (not going into M/W details here), and that the External System always knows at what time they need to send data to Salesforce, go for REST API.

If you want your External System to notify you or if you want the External System to be notified for it to initiate the operation, that is when you should consider the event route (pub/sub).

Hope this helps.

Attribution
Source : Link , Question Author : Pranay Jaiswal , Answer Author : Jayant Das

Leave a Comment