Governor Limits and Performance Optimization

Based on Real Implementation Experience: These patterns come from large orgs with complex automations, integrations, and millions of records where governor limits are hit daily if code and Flows are not designed correctly.

Overview

Governor limits are hard platform constraints that protect Salesforce multi-tenancy. Well-designed solutions treat limits as a design input, not an afterthought. This topic describes how to design Flows and Apex so they are bulk-safe, selective, and resilient under load.

This file focuses on:

Prerequisites

When to Use

Use These Patterns When

Avoid Over-Optimizing When

The goal is safe by default, then optimize where you have clear risk or evidence.

Core Concepts

Bulkification

Bulkification means designing logic to handle many records in one transaction:

Selective Queries

Selective queries:

Workload Shaping

Workload shaping patterns:

Governor Limit Visibility

Use Limits methods in Apex and debug logs in lower environments to:

Patterns and Examples

Pattern 1: Bulk-Safe Trigger or Record-Triggered Flow

Intent: Ensure that automation can safely process 200+ records without hitting limits.

Structure:

Example (Apex):

Pattern 2: Selector Layer with Caching

Intent: Centralize and optimize SOQL queries with optional in-transaction caching.

Structure:

Pattern 3: Async Offload for Heavy Work

Intent: Move expensive work (complex calculations, callouts, large DML) to async Apex.

Structure:

See:

Edge Cases and Limitations

Q&A

Q: How many records should I assume a trigger or Flow will process?

A: Always assume at least 200 records will be processed in a single transaction, because the platform batches DML operations. For integrations and data loads, assume thousands of records per batch and design patterns (batch Apex, Queueable) accordingly.

Q: How do I know if a query is selective enough?

A: Use the Query Plan tool in Developer Console. A query is typically considered selective if it returns less than 10% of rows and uses an indexed field in the filter. Queries with a cost of 1 or 2 in Query Plan are usually safe; higher costs indicate potential issues, especially on LDV objects.

Q: When should I move logic from Flow to Apex for performance?

A: Move logic to Apex when: (1) the Flow is approaching element or CPU limits, (2) you need fine-grained control over batching and retries, (3) you must handle complex loops or calculations, or (4) you need robust error handling and logging not easily modeled in Flow. Keep orchestration in Flow, but move heavy work to Apex services.

Q: What is the safest way to handle large data loads?

A: Use Bulk API or Batch Apex with: (1) idempotent logic (safe to retry), (2) external Ids for upserts, (3) chunked processing (smaller batches if you hit limits), and (4) staging tables or objects when you need to preprocess data. See large-data-loads.md and data-migration-patterns.md for detailed patterns.

Q: How can I prevent governor limit errors from breaking user-facing operations?

A: Design with graceful degradation: (1) perform critical checks first, (2) offload non-critical work to async jobs, (3) use try-catch with structured logging around DML and callouts, and (4) avoid chaining too many automations on a single transaction. Monitor logs and Platform Events to catch patterns early.