2

We have a huge concurrent influx of incoming custom api requests. (1000+ under 5 seconds approx) Because of this we were getting ConcurrentPerOrgLongTxn Limit exceeded.

We moved from Sync processing in Custom web service to Async processing. i.e we used System.enqueueJob to process the request asynchronously, assuming the processing would take max 1 second.

From past few days a huge influx of events started coming to our custom webservice, and our System.enqueueJob gets fired, but the requests goes in Queued status, We can see after running this SOQL

SELECT COUNT() FROM AsyncApexJob WHERE Status = 'Queued' Some 70K records are in Queued status and only 3-4 in Processing status.

Not sure of the reason though. But we are planning to move to Platform Events or CDC instead of System.enqueueJob, and then have an Async Trigger for CDC or Listen to Platform Events from trigger and then process the incoming requests.

My question is "Does CDC/Platform Event uses the same Async Queue which System.enqueueJob (Queueable) uses?"

2 Answers 2

4

You'll see that AsyncApexJob lists out what mechanisms use it

Represents an individual Apex sharing recalculation job, a batch Apex job, a method with the future annotation, or a job that implements Queueable

Platform events do not use the same "queue" as Phil answered and have their own, separate limits to consider and track with the services/data/v54.0/limits endpoint

  • HourlyPublishedPlatformEvents
    • High-volume platform event notifications published per hour
  • MonthlyPlatformEvents
    • If your org doesn’t have the high-volume platform event or Change Data Capture add-on. Includes usage for both high-volume platform events and Change Data Capture events.
  • MonthlyPlatformEventsUsageEntitlement
    • If your org has the high-volume platform event or Change Data Capture add-on. This value includes usage for both high-volume platform events and Change Data Capture events.

Important to note that Change Data Capture (CDC) "change events" are specialized platform events. As such, the allocations for CDC (and platform events above) are shared when it comes to event delivery and why some of the above contains delivery information for both combined.

Change events are based on platform events. The default allocations and usage-based entitlement of delivered events are shared between change events and high-volume platform events.

Monitor Platform Event Publishing and Delivery Usage goes over how the REST limits endpoint provides info on both CDC & Platform Events delivery - but, there's a PlatformEventusageMetric which allows getting usage with a distinction between the two.

The usage metrics stored in PlatformEventUsageMetric are separate from the REST API limits values. Use the REST API limits to track your monthly delivery and publishing usage against your allocations. The monthly CometD-client delivery usage that the limits API returns is common for platform events and change data capture events. PlatformEventUsageMetric breaks down usage of platform events and change data capture events so you can track their usage separately.


In terms of what does it use if it's not using AsyncApexJob - the Considerations for Publishing Platform Events goes into some details on what it's doing

Platform events are temporarily persisted to and served from an industry-standard distributed system during the retention period. A distributed system doesn’t have the same semantics or guarantees as a transactional database. As a result, we can’t provide a synchronous response for an event publish request. Events are queued and buffered, and Salesforce attempts to publish the events asynchronously. 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.

Since you seem most interested in subscribing through Apex Triggers - Subscribe to Platform Event Notifications with Apex Triggers covers that area

An Apex trigger processes platform event notifications sequentially in the order they’re received. The order of events is based on the event replay ID. An Apex trigger can receive a batch of events at once. The maximum batch size in a platform event trigger is 2,000 event messages. The order of events is preserved within each batch. The events in a batch can originate from one or more publishers.

Unlike triggers on standard or custom objects, triggers on platform events don’t execute in the same Apex transaction as the one that published the event. The trigger runs asynchronously in its own process. As a result, there can be a delay between when an event is published and when the trigger processes the event.

9
  • HourlyPublishedPlatformEvents and MonthlyPlatformEvents I believe only applies to Platform Events and not CDC. Right? Apr 5, 2022 at 19:11
  • The default allocations & usage-based entitlements of delivered events are shared between the two. I'll update the answer Apr 5, 2022 at 19:26
  • In documentation there is no limits mentioned for Publishing CDC, but publishing PE has a limit as per documentation. So there shouldn’t be any limits to CDC fired in 1 hour or montly. Apr 5, 2022 at 19:47
  • You're correct for HourlyPublishedPlatformEvents (just contains info for Platform Events) - but MonthlyPlatformEvents is different and is what I was commenting on - limits endpoint: "This value contains the default allocation and usage for event delivery to CometD clients and is incremented for each client. MonthlyPlatformEvents includes usage for both high-volume platform events and Change Data Capture events". Apr 5, 2022 at 20:12
  • One of the problems I have with the way the documentation frames the execution as "async" is that the platform considers then as synchronous, from the limits perspective. The documentation authors completely failed to consider this. That is why I frame it as "synchronous" in a decoupled transaction (or "process" as this documentation oddly calls it) where the (distributed, in-memory) queue provides that decoupling, and the "queue consumer" processes the invocations for trigger and CometD subscribers. I imagine the "queue" tech is something like IBM MQ, Oracle AQ or similar.
    – Phil W
    Apr 6, 2022 at 1:06
3

Does CDC/Platform Event uses the same Async Queue which System.enqueueJob (Queueable) uses?

No. Platform Events are not actually async executions; they are essentially synchronous executions in a separate transaction, where the decoupling is via use of an "event queue" of some kind.

One of the problems I have with the way the documentation frames the execution as "async" is that the platform considers then as synchronous, from the limits perspective. The documentation authors completely failed to consider this aspect.

That is why I frame it as "synchronous" in a decoupled transaction where the (distributed, in-memory) queue provides that decoupling, and the "queue consumer" processes the invocations for trigger and CometD subscribers.

My understanding is that this "event queue" does not persist in the database, which is how come delivery is not guaranteed - the queue is in-memory and thus could get lost if the server/cluster dies. (I imagine the "queue" tech is something like IBM MQ, Oracle AQ or similar but have no actual knowledge of this.)

Importantly, if a behaviour is not explicitly documented by Salesforce they can, in principle, choose to change it at any time. I would therefore say you probably don't want to rely on whether multiple trigger-based subscribers run sequentially or in parallel since as far as I know this is not documented. (Kris Goncalves's answer nicely links to the official documentation available.)

4
  • Ok understood, so even here it will enter an event queue if a huge influx is there and the processing might be delayed. As event queue should work in fifo manner. Just last follow up question. Can the platform event trigger listener process requests concurrently or one by one. Apr 5, 2022 at 15:56
  • Not sure I understand "trigger listener process requests"? Do you mean "are the events sent to multiple subscribers concurrently"? If so, I don't actually know; I've not tried to analyse that behaviour.
    – Phil W
    Apr 5, 2022 at 16:21
  • That means platform event are being listened via trigger. So can trigger process request in parallel . Or will they process it one by one. Apr 5, 2022 at 16:24
  • So, that's what I call an "trigger-based event subscriber"; you subscribe to platform events either using apex triggers or using cometD. As I mentioned, I didn't analyse the behaviour when there are multiple subscribers (trigger or cometD based). You would need an actual Salesforce R&D dev to confirm. Sorry.
    – Phil W
    Apr 5, 2022 at 16:32

You must log in to answer this question.

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