Asynchronous Processing in Apex On Salesforce: Part 2 – Queueable Apex

asynchronous processing

In the second part of this series, we will look at Queueable Apex. It is a more feature rich approach for asynchronous processing compared to the Future Method. The Queueable interface enables you to add jobs to the Salesforce processing queue and monitor them using an id.

Let’s review some important information about Queueable Apex and the scenarios where using it is most appropriate.

Key Considerations For Using Queueable Apex For Asynchronous Processing On Salesforce

Higher Governor Limits: Queueable Apex methods have higher asynchronous governor limits. e.g. a future method transaction can execute 200 SOQL to queries, double that of a synchronous transaction. The maximum allowed CPU time and total heap size are also significantly higher, allowing you to perform more complex processing.

Use of Non-Primitive Data Types: Unlike Future method, queueable class can contain member variables of non-primitive data types, such as sObjects or custom Apex types. Those objects can be accessed when the job executes. This allows more complex data processing in a queueable apex.

Job monitoring: When you submit a queueable apex job by invoking the System.enqueueJob method, the method returns the ID of the new job. This ID corresponds to the ID of the AsyncApexJob record. You can use this ID to identify and monitor your job, either through the Salesforce UI (Apex Jobs page), or programmatically by querying AsyncApexJob records.

Job Chaining: You can chain one job to another job by starting a second job from a running job. To chain a job to another job, submit the second job from the execute() method of your queueable class. You can add only one job from an executing job, which means that only one child job can exist for each parent job. Chaining jobs is useful if your process depends on another process to have run first. Also, if there are multiple discrete and independent steps that are part of the processing then you can chain those together.

Queued Job Limit: You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction. Whereas from an asynchronous transaction such as a batch apex, you can add only 1 job to the queue

Transaction Finalizers: The Transaction Finalizers feature enables you to attach actions, using the System.Finalizer interface, to asynchronous Apex jobs that use the Queueable framework. With this, you can perform an action based on Queueable job execution results. This is useful in performing any recovery action or creating summary metrics. You can even re-enqueue a failed job up to 5 times in a transaction finalizer.

When to Use/Not Use Queueable Apex

API Callouts: Similar to Future Methods, you can use queueable apex to make API callouts that take a long time to complete. With queueable apex, you have the added benefit of getting a job id that can be used for monitoring status and transaction finalizers that can be used to retry the callouts in case of failures. You can’t perform a DML operation before an API callout.

Extensive Database Operations: Complex Apex operations with extensive database operations can be run asynchronously using queueable apex. Using chaining, such discrete complex operations can be carved out in asynchronous operations. Since queueable apex also provides job id for monitoring, such operations can also be monitored and retried through transaction finalizers. 

Cannot Execute at Specific Time: Queueable apex jobs run when system resources become available. You can delay execution of the job by a few minutes if you need to wait but you cannot provide a specific execution time. For that you need Apex Scheduler.

Sequential Processing: Queueable apex job chaining can be used to perform operations in a specific desired sequence. Transaction Finalizers in such chained jobs can perform any recovery or retries and proceed with the next sequential operation.

Wrapping It Up 

In summary, use queueable apex when you want to execute an independent transaction asynchronously. It provides more control compared to a future method through job id and transaction finalizers. The most common use cases are a) making an API call, b) performing complex multi step sequential operations. Stay tuned for part 3 where we will review other ways of executing operations asynchronously in Salesforce.

Shailesh Bhide
Shailesh Bhide
Shailesh is a Principal at Varasi. He shares thoughts and insights on technical know-how and patterns that helps us build elegant Salesforce solutions for our customers. He is passionate about leveraging the Salesforce platform to its full potential.