0

Given that Platform Events have multiple options in terms of "subscriber": Apex Trigger, Comet-D Streaming API, and Pub/Sub API, the different approaches would seemingly complicate any guaranteed delivery approach for a given subscriber.

How does Salesforce ensure a successful delivery to each subscriber? If an Apex Trigger is asynchronous and fails during processing, is there anything in the architecture to support replay without having to build out some custom object to track received/success vs received/failed with a separate monitor worker to retry received/failed messages? Does Salesforce remove Platform Messages that it believes have been delivered to subscribers, meaning if I have a received/failed scenario in my subscriber do I need to clone the event and try to store it separately?

1
  • 1
    If you read the docs you will discover that delivery is not guaranteed, full stop. Specifically note "In rare cases, the event message might not be persisted in the distributed system during the initial or subsequent attempts. This means that the events aren’t delivered to subscribers, and they aren’t recoverable."
    – Phil W
    Commented May 1, 2023 at 7:30

1 Answer 1

2

I think you have a fundamental misunderstanding of how Platform Events operate. The main topic you'd want to read is Bayeux Protocol, CometD, and Long Polling. The documentation explains this decently enough:

Long polling, also called Comet programming, allows emulation of an information push from a server to a client. Similar to a normal poll, the client connects and requests information from the server. However, instead of sending an empty response if information isn't available, the server holds the request and waits until information is available (an event occurs). The server then sends a complete response to the client. The client then immediately re-requests information. The client continually maintains a connection to the server, so it’s always waiting to receive a response. In the case of server timeouts, the client connects again and starts over.

In other words, the subscriber/client remains continually connected in a waiting state for new events to arrive, as well as being able to catch up on events it may have missed, even events that may have occurred before they subscribed initially.


How does Salesforce ensure a successful delivery to each subscriber?

There's no such delivery guarantee, as PhilW says. During high traffic, events may be lost. This failure is exceedingly rare, but is documented as a possibility. Platform Events, however, are incredibly reliable, and this failure is unlikely to happen most of the time. Platform Events are reliable enough that if you need to use them, they will likely work for your intended use case.

If an Apex Trigger is asynchronous and fails during processing, is there anything in the architecture to support replay without having to build out some custom object to track received/success vs received/failed with a separate monitor worker to retry received/failed messages?

The triggers are always asynchronous. Each trigger may be replayed up to nine times (the initial attempt plus nine retries, for a total of 10 tries) before they enter a final error state. This part happens automatically without developer intervention. However, a developer can be more intentional about processing, as they can Resume a Platform Event Trigger After an Uncaught Exception and Retry Event Triggers with EventBus.RetryableException. If the trigger reaches a total of 10 retries without success, it enters a final error state, an email is sent to a developer or admin, and it will no longer process events until it is redeployed.

To answer the question more directly, a developer does not need to create custom objects and write a bunch of custom code in order to facilitate the retry mechanism. They have two mechanisms, described above, to control the retry mechanism more precisely, as well as being able to Configure the User and Batch Size for Your Platform Event Trigger with PlatformEventSubscriberConfig. A developer can set how many events process at once by adjusting the size, and work with security problems by setting an appropriately configured user.

Does Salesforce remove Platform Messages that it believes have been delivered to subscribers, meaning if I have a received/failed scenario in my subscriber do I need to clone the event and try to store it separately?

The platform does not assume a message has been delivered successfully to any subscriber, other than Apex, which it can inherently tell where the state of the platform event is. Subscribers can even retrieve events that are up to 72 hours old by using the replay ID of -2. This would not be possible if events were immediately discarded upon successful delivery.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .