2

I was implementing a solution using Platform Events, and was wondering can I use ReplayId as a unique field for the new record I am creating after subscribing to fired platform event.

All I can see for Platform Events' ReplayID Field is

Each event message is assigned an opaque ID contained in the ReplayId field. The ReplayId field value, which is populated by the system, refers to the position of the event in the event stream. Replay ID values are not guaranteed to be contiguous for consecutive events. For example, the event following the event with ID 999 can have an ID of 1,025. A subscriber can store a replay ID value and use it on resubscription to retrieve events that are within the retention window. For example, a subscriber can retrieve missed events after a connection failure. Subscribers must not compute new replay IDs based on a stored replay ID to refer to other events in the stream.

There is no mention that replayId is unique and autoincrementing even after retention window.

I don't want to corrupt data if the replayId repeats itself.

Source: https://developer.salesforce.com/docs/atlas.en-us.platform_events.meta/platform_events/platform_events_define_ui.htm?search_text=Each%20event%20message%20is%20a

  • 1
    In one of our recent implementations, I did find replayId to be incremental even after the retention period (of 24 hours). Consumer of the events failed to consume it for more than 24 hours, and when they started again, the replayId was next in the sequence. – Jayant Das Apr 19 '18 at 17:21
2

This may clarify to an extent. Both Platform Events and Streaming API are based on Bayeux protocol and CometD. This is something what the documentation link on Streaming API states on the replayId:

Each broadcasted event is assigned a numeric ID. IDs are incremented and not guaranteed to be contiguous for consecutive events. Each ID is guaranteed to be higher than the ID of the previous event. For example, the event following the event with ID 999 can have an ID of 1,025. The ID is unique for the org and the channel. The IDs of deleted events aren’t reused.

And this is what the documentation on Platform Event says:

The process of replaying platform events is the same as for other Streaming API events

However not really sure, what's the limit of the ids generated, as say it reaches 9999, does it reset to 1 or goes to 10000. But assuming it's a system field, it must have been accounted for a larger limit. But still, I would think these ids should not be used for any uniqueness outside of the event scope.

  • Well its for Streaming API, I would not rush into implement it . As suggested by sfdcfox i can use replayId and createdDate to form a composite primary key for my table. – Pranay Jaiswal Apr 19 '18 at 20:00
  • 1
    Both Streaming and PE follow same protocol (Bayeux), so the attributes in the event (including Ids) generated should follow the same. But anyways, I wouldn't recommend using replayId either for utilizing it out of event's scope for any purposes. But overall it does look that replayIds are unique and are not reused. However not really sure, what's the limit of the ids, as if it say reaches 9999, does it reset to 1 or goes to 10000. – Jayant Das Apr 19 '18 at 20:07
  • Happy to help. And actually got this to kind of confirm, that it's same for both Platform Event and Streaming API. Updated my answer (it was somewhere deep in the documentation for Platform Event :)). – Jayant Das Apr 19 '18 at 20:22
2

There is one special case wherein ReplayId may not be unique for an application - not necessarily the example in the OP:

  • Apex trigger T1 subscribes to Platform Event Foo__e
  • Above trigger republishes via a second Platform_Event Bar__e that includes as a custom field the value from Foo__e.ReplayId. Some other trigger T2 (or PB/Lightning Flow) consumes Bar__e.

If T1 throws EventBus.RetryableException, then T1 will get re-executed with the same ReplayId and republish Bar__e again with a duplicate Foo__e.ReplayId. This could happen up to 9 times.

Thus, T2 may end up inserting duplicates into some Sobject as it has no way of knowing that T1 was executed multiple times due to EventBus.RetryableException

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.