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 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 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
- Chaining jobs
- Getting an Id for a job
- 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.