Synchronous Apex Occurring at the same time. Quick and Immediate actions. Transactions are immediate and serial. Normal Governor Limits Asynchronous Apex Not occurring at the same time. Executes when resources are available. Not immediate. Higher Governor Limits.
Governor Limits Description Synchronous Limit Asynchronous Limit Total number of SOQL queries issued 100 200 Total number of records retrieved by SOQL queries 50000 50000 Total number of records retrieved by Database.getQueryLocator 10000 10000 Total number of SOSL queries issued 20 20 Total number of records retrieved by a single SOSL query 2000 2000 Total number of DML statements issued 150 150 Total heap size 6 MB 12 MB Maximum CPU time on the Salesforce servers 10000 ms 60000 ms
Example scenario
Types
Future Apex @future annotation at the start of method. Always method should be public static and return type should be void . The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types; future methods can’t take objects as arguments. Future methods won’t necessarily execute in the same order they are called. In addition, it’s possible that two future methods could run concurrently, which could result in record locking if the two methods were updating the same record. You’re limited to 50 future calls per Apex invocation, and there’s an additional limit on the number of calls in a 24-hour period. Cannot call another future method from a future method.
Batch Apex Has a different syntax. Every transaction starts with a new set of governor limits. If one batch fails to process successfully, all other successful batch transactions aren’t rolled back. Start, Execute and Finish are the 3 methods. Can track the progress of batch in the apex job page in setup. 5 batch jobs at a given time You can call another batch from batch or queue but not future method. What happens when more than 5 batch jobs are invoked?
Queueable Apex Similar to future apex. Accepts non primitive datatypes too. Can call another queue. (Only one) Progress can be tracked in apex batch jobs page. You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction.
Schedulable Apex Runs apex class at a specified time and frequency. Can be scheduled in UI or using cron jobs. Scheduled jobs can be tracked from scheduled jobs page in setup. You can only have 100 scheduled Apex jobs at one time Note: Always asynchronous apex should be executed within startTest () and stopTest ().