Home SalesforceInterview Question Queueable Vs Batch Apex In Salesforce

Queueable Vs Batch Apex In Salesforce

by Dhanik Lal Sahni

Both Queueable and Batch Apex are Asynchronous Apex which is used when some long-running operation is required. Both asynchronous processes are used in different scenarios based on the business case or business requirement.

Batch Apex

Batch Apex is used for long-running jobs with large data volumes that need to be performed in batches, such as database maintenance jobs. Batch Apex run over small batches of records, covering our entire record set and breaking the processing down to manageable chunks (a batch). Each batch is considered a separate Apex transaction so that it will not exceed the government limits while doing long-running operations.

Limitations of Batch Apex

  • Can have only 5 batch jobs running at a time.
  • Execution may be delayed due to server/resource availability.
  • Future methods are not allowed.
  • Future methods cannot be called from Batch Apex.

Queueable Apex

Queueable Apex can be used to run processes that will take a long time, such as extensive database operations or external web service callouts. Queueable Apex is a more advanced and enhanced version of the future method with a few added features which are mentioned below

  1. Chaining jobs
  2. Getting an Id for a job
  3. Using non-primitive types

Limitations of Queueable Apex

  • Queueable can’t handle millions of records in one job.
  • Only one job can be scheduled at a time.
  • In the developer edition stack depth for chaining the jobs is limited to 5 including the parent job.

Reasons to use Queueable Apex over Batch Apex

  • It is useful in operations that involve both Batch and Future methods.
  • Queueable apex can be called from the Future and Batch class.
  • In Queueable Apex, we can chain up to 50 jobs and in the developer edition, we can chain up to 5 jobs only whereas in Batch Apex 5 concurrent jobs are allowed to run at a time.
  • We can call a future method for executing long-running operations, such as callouts to external web services or other operations, on its own time as we can’t call future methods from the batch class.
  • Check out the blog Avoid Batch Apex and Use Queueable Class to understand where we should use Queueable instead of Batch Apex.

Reasons to use Batch Apex over Queueable 

  • It allows the processing of 50 million records in the background
  • Best suitable for long-running processes as it will create chunks of records.

Related Blogs

Optimize SOQL Filter in Apex Code

Optimizing Loop in Apex Code

Optimizing Salesforce Apex Code

Summer’22 Release Enhancement

Summer 22 Enhancement for Lightning Web Component

Summer’22 Enhancement for Sales Cloud

You may also like

3 comments

What are Skinny Tables? - Salesforce Codex - Stories July 1, 2022 - 3:41 pm

[…] Queueable Vs Batch Apex In Salesforce […]

Reply
What is Data Skew? - Salesforce Codex July 5, 2022 - 4:32 am

[…] Queueable Vs Batch Apex In Salesforce […]

Reply
What is PK Chunking? - SalesforceCodex July 6, 2022 - 6:13 am

[…] Queueable Vs Batch Apex In Salesforce […]

Reply

Leave a Comment

Top 10 Salesforce Service Cloud Features Top 10 Best Practices for Lightning Flow Facts and Statistics for Salesforce’s Size and Market Share Top 5 Contract Management Salesforce Apps Top 10 Enterprise Integration Use Cases