Batch state allows you to execute a Function, Webhook, or Circuit in batches as multiple jobs in an iterative manner.
While the Parallel state executes multiple states simultaneously using the same input, the Batch state executes the (same set of) State(s) for multiple entries of an array in the state input. This helps to handle a large volume of input or tasks in sets rather than overloading the involved resource. Collection Path
The Collection Path field in the Batch state allows you to specify an array within an input JSON for the state to iterate over. This is particularly useful when you have a JSON input that contains an array of items, and you want to process each item individually within the Batch state. Use JSONPath syntax to specify the location in the input of the JSON array used for iteration.
Let us consider the following JSON input:
{
"employee_list": [
{
"name": "John",
"email": "john@zylker.com",
"role": "Business Analyst"
},
{
"name": "Helena",
"email": "helena@zylker.com",
"role": "Programmer"
},
{
"name": "Mark",
"email": "mark@zylker.com",
"role": "QA Analyst"
}
]
}
Every index in the above input can be iterated, set to a variable, and sent for processing using the collection path $.employee_list.
Collection Variable
The Collection Variable is used to store the JSON values that need to be processed in the associated resource (Circuit, Webhook, or Function states) of the Batch state.
For example, if we want to process the employee details of each employee from the 'employee_list' array in the above JSON input, in the Batch state configuration, set the Collection Path as $.employee_list and the Collection Variable as an employee. This will extract each employee object within the 'employee_list' array and assign it to the 'employee' variable.
If the associated resource is a Circuit, the values from 'employee_list' will be processed using the collection variable. The values in the Collection Variable can be used as $.employee.name, $.employee.email, $.employee.role in the Circuit.
Example Use Case: In the employee onboarding scenario, a circuit named 'New Employee Onboarding' is already designed for the onboarding of a single employee, which has a predefined sequence of steps or actions to be followed during the onboarding process. However, to onboard multiple employees efficiently, we can execute the circuit in batch mode for multiple entries of employee details using the batch state. Each entry represents an individual employee going through the onboarding process, ensuring that each step of the process is followed consistently for all employees.
This enables us to onboard multiple employees simultaneously. By setting the number of jobs in the Batch state, let's say 10, the circuit can be executed concurrently for 10 employees, processing their respective inputs. Once the initial 10 jobs are completed and the first set of employees is processed, the subsequent iteration will initiate the next batch of 10 jobs to onboard the next set of employees. This iterative process continues until all employees are successfully onboarded.
Now, let's configure a Batch state in Builder View and Code View for executing multiple employee onboarding circuits as a batch.
Builder View
To configure a Batch state to your circuit:
In Builder View, drag and drop the Batch state from the left pane into your circuit or click the required Batch state in your circuit.
Under Configuration, update the common state field attributes.
In the Collection Path, enter the key that contains the JSON array with the values to be processed by the Batch state in the input JSON.
In the Collection Variable, provide a variable name to hold the JSON value or object extracted from the list specified in the Collection Path. The iterated JSON value or object can be accessed using this variable.
Using Bind Type, you can bind the Batch state to a Function, Webhook, or Circuit.
In this process, we are binding circuit developed for single employees onboarding and executing as a batch for multiple employees during the employee onboarding process.
In the No. of Jobs field, define the number of threads for each batch.
Since multiple instances of a function or a circuit are executed, this ensures that they do not face a timeout error.
If you define the Retry option for the Batch state, the retry policy applies only if all iterations of the Batch state fail. For example, if you configure the Retry field in a Batch state with three iterations, the retry policy applies only if all three iterations fail. Code View
In Code View, JSON details can be provided in fields collectionPath and collectionVariable. You can link a Function, Webhook, or Circuit using bind and define the number of threads for batch using degree.
Batch state example:
"Employee Onboarding Tasks": {
"type": "batch",
"next": "End",
"start": true,
"collection_path": "$.employee_list",
"collection_variable": "employee",
"degree": 10,
"bind": {
"type": "circuit",
"circuit_id": "new_employee_onboarding"
}
}