# How do developers optimize high-throughput event publishing strategies to prevent governor limit exhaustion?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Batch Publishing, Throughput Optimization

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-developers-optimize-high-throughput-event-publishing-strategies-to-prevent-governor-limit-exhaustion

## Answer

**Basics:** Each `EventBus.publish` call counts as one DML statement (limit 150 per transaction), and each event counts toward an hourly publishing allocation for the org. Publishing one event per loop iteration can burn the DML limit quickly and also wastes allocation efficiency.

**How it works:**
1. Collect events in a `List<Event__e>` during the transaction.
2. Call `EventBus.publish(list)` once, after the loop.
3. Inspect each `SaveResult`.
4. For very large volumes, move the work to Batch or Queueable Apex, or publish from outside Salesforce through Pub/Sub API or REST in batches.
5. Monitor usage with `/limits` (hourly published events) and alert before you hit the cap.

**Advanced / deep dive:** Combine several small business changes into a single event with a list payload, if subscribers can handle it, to cut event counts. Remember that Change Data Capture publishes for you without Apex, so do not duplicate it with custom events. Use "Publish After Commit" so rolled-back transactions publish nothing. When failures happen, a stray retry loop can double-publish, so use an idempotency key. If your publisher is Flow, use a single Create Records element with a collection rather than one per record. For external publishers, the Pub/Sub API supports batch publish and is better than one REST call per event. If you exceed the hourly allocation, publishing returns errors, and you may need to queue events in a custom object and retry (verify current error codes).

**Code / config example:**
```apex
public static void publishAll(List<Order> orders) {
    List<Order_Event__e> batch = new List<Order_Event__e>();
    for (Order o : orders) {
        batch.add(new Order_Event__e(Order_Id__c = o.Id, Key__c = o.Id + ':' + o.LastModifiedDate.getTime()));
    }
    if (!batch.isEmpty()) {
        List<Database.SaveResult> res = EventBus.publish(batch);   // 1 DML statement
        System.debug('Remaining DML: ' + (Limits.getLimitDmlStatements() - Limits.getDmlStatements()));
    }
}
```

**Hidden gotchas & must-remember points:**
- `EventBus.publish` counts as a DML statement and the events count toward the DML rows limit as well (verify in the current limits table).
- Batch publishing does not remove the hourly allocation limit.
- A partially failed list returns per-event results, so check each.
- There is no "buffer" feature on the platform. Accumulation is something you build.
- Publishing from a trigger on a bulk data load can multiply events, so test with 200+ records.

**Best practice:** Publish once per transaction with a list, and add monitoring for allocation use.

---
Source: ForceChallenges · last updated 2026-11-02
