[SalesForce] Do Platform Event Triggers process synchronously? Do they get a fresh set of limits

I'm intrigued by Platform Events, but I have a few questions about how they operate:

  1. Are the subscription triggers processed synchronously or asynchronously?
    • The developer guide makes it clear that the publish part of the pub/sub is synchronous. But I haven't yet ascertained if that also applies to the subscribe part.
  2. Do the subscription triggers get separate limits from the publishing context?
  3. If the limits are separate, does each subscription trigger get its own set?

Best Answer

Are the subscription triggers processed synchronously or asynchronously?

Aynchronously

Do the subscription triggers get separate limits from the publishing context?

Yes, separate limits from the publishing context

If the limits are separate, does each subscription trigger get its own set?

Each Apex subscription trigger executes in its own transaction with independent limits.


To back these claims up I create a test platform event with two Apex trigger subscriptions. I then executed a publish for 300 events via anonymous Apex.

There were 5 independent transaction logs generated. One for the Tooling API execute anonymous, followed by 4 "Platform Event Trigger" operations. Historically each log entry has always represented a separate transaction (with their own limit). I'm assuming the same applies here. I'll add some more limit logging to back that up.

Sequence of logs for my second test attempt:

  • Operation /services/data/v40.0/tooling/executeAnonymous/ to publish the 300 events. Timestamp: 7:57:09 PM
  • Operation Platform Event Trigger 2nd Apex trigger with CPU Limit at 36 with last DEBUG message. Trigger.new size of 300. Timestamp: 7:57:11 PM
  • Operation Platform Event Trigger 1st Apex trigger with CPU Limit at 23 with last DEBUG message. Trigger.new size of 200. Timestamp: 7:57:11 PM
  • Operation Platform Event Trigger 1st Apex trigger with CPU Limit at 12 with last DEBUG message. Trigger.new size of 100. Timestamp: 7:57:12 PM

    enter image description here

Note how my first test split 200 events to the first event trigger transaction and then 100 events to the latter transactions. Yet in my second test with the same triggers and same number of published events the second trigger only fired once for the full 300 records. Important note on potential Trigger.new size for platform events.

It can potentially go all the way up to 2000 events in one trigger transaction. Source