Development

Queueable VS @future

The Winter ’15 platform release brought us the new Queueable Apex interface. It is a cool way to execute asynchronous process working much like what we used to use: @future. But it brings some new performances which are really cool. This article is going to compare the @future and Queueable Interface.

 

@future

A future method runs asynchronously. And its governor limits are higher:

 

 Description Synchronous Limit Asynchronous Limit
Total number of SOQL queries 100 200
Total heap size 6MB 12MB
Maximum CPU time 10,000 milliseconds 60,000 milliseconds

When to use it:

  1. Long-running operations (callouts to external web service)
  2. Separating mixed DML operations (i.e. inserting a user with a non-role must be done in a separate thread from DML operations on other sObjects)

 

How to define it:

1

  1. future anotation
  2. Must be static and return void
  3. Specify (callout=true) to allow callouts

 

Covid 19
COVID-19: Digital Insights For Enterprise Action

Access Perficient’s latest insights into how you can leverage digital technologies to not only respond to the pandemic, but drive your operations forward and deliver experiences your customers need.

Get Informed

Limits:

  1. Parameters passed in can be only of Primitive type
  2. Cannot chain @future method
  3. Cannot get the job ID

Queueable Interface

This interface enables you to add jobs to the queue and monitor them which is an enhanced way of running your asynchronous Apex code compared to using future methods.

Like @future, a benefit of using the Queueable interface is that some governor limits are higher than synchronous Apex (see the table above).

 

When to use it comparing with @future:

  1. Chaining jobs
  2. Asynchronous monitoring (it gets job ID)
  3. Using non-primitive types

 

How to define it:

2

Much like what we did on using batchable interface

  1. Implement the Queueable interface
  2. Define execute() method
  3. Chain this job to next job by submitting the next job
  4. Implement the Database.AllowsCallouts interface to allow callouts

 

Limits:

  1. When chaining jobs, you can add only one job from an executing job with System.enqueueJob.
  2. Cannot have more than 1 job in the chain that does callouts.

 

Reference:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_invoking_future_methods.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm

 

About the Author

More from this Author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up
Categories