Back to Articles
Apex & Architecture
June 2, 20268 min read

Mastering Salesforce Governor Limits: The Architect's Guide

Learn advanced bulkification, CPU time optimization, dynamic Heap limit management, and how to design Salesforce architectures that never hit limits.

A
Alex RiveraPrincipal Salesforce Architect

Salesforce is a multi-tenant platform. This means your code runs in a shared environment where resources like database processing, memory, and CPU time are shared with thousands of other customers. To prevent poorly written code from monopolizing these resources, Salesforce enforces strict execution boundaries known as Governor Limits. Understanding how to work within these boundaries is the difference between a Junior developer and a Senior Architect.

Core Execution Limits You Must Master

Salesforce has two main categories of limits: synchronous and asynchronous. Synchronous limits are enforced per-transaction, while asynchronous jobs (Queueable, Batch, Scheduled) have higher allocations.

Limit Name

Total SOQL Queries

Synchronous Limit

100

Asynchronous Limit

200

Limit Name

Total DML Statements

Synchronous Limit

150

Asynchronous Limit

150

Limit Name

Maximum CPU Time

Synchronous Limit

10,000 ms

Asynchronous Limit

60,000 ms

Limit Name

Maximum Heap Size

Synchronous Limit

6 MB

Asynchronous Limit

12 MB

Bulkification Best Practices: Writing Scale-Safe Apex

Bulkification is the design pattern of ensuring your Apex code can handle multiple records efficiently. Never write SOQL or DML inside for loops. Doing so is a fast track to System.LimitException errors.

apex
// WEAK: Query inside loop (Hits 100 limit quickly) for (Account acc : Trigger.new) { List<Contact> contacts = [SELECT Id FROM Contact WHERE AccountId = :acc.Id]; } // STRONG: Bulkified using Map & Single Query Set<Id> accountIds = new Set<Id>(); for (Account acc : Trigger.new) { accountIds.add(acc.Id); } Map<Id, Account> accountsWithContacts = new Map<Id, Account>( [SELECT Id, (SELECT Id FROM Contacts) FROM Account WHERE Id IN :accountIds] );

Advanced CPU Time & Heap Optimization

In Large Data Volume (LDV) environments, even bulkified code can hit CPU and Heap limits. When querying massive sets of data, standard collections load all records into memory, which bloats the heap. Use SOQL For Loops instead, which process records in batches of 200:

apex
// Process 50,000 records in batches of 200 without overloading Heap memory for (List<Opportunity> oppBatch : [SELECT Id, StageName FROM Opportunity WHERE StageName = 'Prospecting']) { for (Opportunity opp : oppBatch) { opp.StageName = 'Qualification'; } update oppBatch; }

Architect Tip

When dealing with heavy payloads, always set unused variables to null before performing next-stage operations, helping the garbage collector free up heap memory.

Designing Programmatic Governor Limit Bypasses

In complex enterprise systems, recursive triggers or deep execution chains can consume excessive CPU time and query limits. Architects design bypass patterns that prevent execution loops:

apex
public class TriggerBypassController { private static Set<String> bypassedTriggers = new Set<String>(); public static void bypass(String triggerName) { bypassedTriggers.add(triggerName); } public static void clearBypass(String triggerName) { bypassedTriggers.remove(triggerName); } public static Boolean isBypassed(String triggerName) { return bypassedTriggers.contains(triggerName); } }

Technical Interview Hub

Prepare comprehensively for Salesforce hiring loops by exploring our specialized interactive study modules.

Evaluate Your Readiness

Done reading? Put your theory to the test. Launch our AI-powered voice recruiter simulation to benchmark your performance.

Launch Mock Interview