When to use Batch Apex
Batch Apex has several key use cases, all of which we will discuss here. But before we discuss them, it is worth spending some time understanding how Batch Apex processes data.
As shown in the following diagram, Batch Apex is great for working with large volumes of data. When utilizing Batch Apex, we define a set of data to be iterated over, either through a query or a custom iterable. Salesforce then chunks this data into batches and processes the batches one by one. So, if we had 200,000 records to process and wanted to process them in batches of 1,000, Salesforce would chunk the job into batches of 1,000 and process them one by one.
Refer to the following diagram:
Figure 8.1 – Batch processing
This makes Batch Apex fantastic at some tasks and less useful at others. Now, let’s look through some use cases where having these processes executed in a sequential series of batches makes the most sense.