Advanced 15 min read

Designing for Governor Limits at Scale

By the end of this lesson, you'll be able to:

  • Identify architectural patterns that keep a large org within governor limits
  • Explain the risk of recursive trigger chains in a complex org

Prerequisites: The Unit of Work Pattern

Recursive trigger chains

In a large org, a trigger on Account might update a Contact, which fires a trigger on Contact, which updates the Account again, re-firing the first trigger — an unintentional loop. A static recursion-guard variable (private static Boolean hasRun = false;), checked and set at the top of a handler, is the standard defense.

Selective queries and automation budgets

As more automation (triggers, Flows) touches the same object, each one's queries and DML add up within the same transaction's shared governor limits. Architecture at scale means being deliberate about what runs where, keeping queries selective (indexed, filtered fields), and periodically auditing what automation exists on high-traffic objects.

A recursion guard

public class AccountTriggerHandler {
    private static Boolean hasRun = false;

    public void run() {
        if (hasRun) {
            return;
        }
        hasRun = true;

        // ... trigger logic that might indirectly cause this trigger to re-fire ...
    }
}

The static hasRun flag persists for the lifetime of the transaction — once set, any re-entrant call into this handler within the same transaction short-circuits immediately, breaking the recursive loop.

Exercise

Add a recursion guard to this trigger handler so its logic only runs once per transaction.

Show hint

A private static Boolean, checked and set at the very top of run().

APEX

Designing for Governor Limits at Scale — Quick Check

1. What causes an unintentional recursive trigger chain?

2. A static Boolean recursion guard persists for the lifetime of a single transaction.

3. As an org grows, what becomes an architectural concern beyond bulkifying individual methods?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

At enterprise scale, staying within governor limits isn't just about bulkifying individual methods — it's an architectural concern: recursion guards, selective queries, and careful cross-object automation design all matter once dozens of triggers, flows, and integrations touch the same records.