Learn how to use the Split In Batches node in n8n to break large datasets into manageable batches for efficient processing, API rate limit handling, and optimized workflows.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
The Split In Batches node in n8n is a powerful utility that breaks down large datasets into smaller, manageable batches for processing. It's particularly useful when dealing with API rate limits or when you need to process data in chunks. This node takes your input data and divides it into specified batch sizes, allowing subsequent nodes to process each batch separately.
Step 1: Understanding the Split In Batches Node
The Split In Batches node is a core utility node in n8n that allows you to split an array of items into multiple batches of a specified size. This is particularly useful when:
The node takes an array of items as input and outputs multiple batches, each containing a subset of the original items. Each batch is processed separately by subsequent nodes in your workflow.
Step 2: Adding the Split In Batches Node to Your Workflow
To add the Split In Batches node to your workflow:
The node will be added to your workflow. Connect it to a node that outputs an array of items that you want to split into batches.
Step 3: Configuring the Split In Batches Node
Once added to your workflow, you need to configure the Split In Batches node:
The main parameter to configure is the "Batch Size" which determines how many items will be in each output batch. The default is 10 items per batch.
Step 4: Creating a Sample Workflow with Split In Batches
Let's create a simple workflow to demonstrate how the Split In Batches node works:
For the Function node, use the following code to generate sample data:
// Generate an array of 25 sample items
const items = [];
for (let i = 1; i <= 25; i++) {
items.push({
id: i,
name: `Item ${i}`,
value: Math.floor(Math.random() \* 100)
});
}
// Return the array as output
return [{ json: { items } }];
Step 5: Configuring the Split In Batches Node Parameters
After adding the Split In Batches node to your workflow, you need to configure its parameters:
In our example, we'll set the "Batch Size" to 5, which means our 25 items will be split into 5 batches of 5 items each.
If your data is nested within the items, you can specify the "Source Data" parameter to point to the specific property. For our example, since our Function node outputs data in the format { items: [...] }
, we would set the "Source Data" to items
.
Step 6: Understanding Batch Processing Flow
When you execute a workflow with the Split In Batches node, the flow works as follows:
This sequential processing helps manage resource usage and respects API rate limits when working with external services.
Step 7: Working with the Batch Output
Each batch output from the Split In Batches node is an array of items. The items retain their original structure but are grouped into smaller arrays based on the batch size.
To process each batch, you'll typically:
Remember that each batch is processed entirely before the next batch begins, so any node connected after the Split In Batches node will be executed multiple times (once per batch).
Step 8: Using Split In Batches with API Requests
One common use case for the Split In Batches node is when working with APIs that have rate limits. Here's how to set this up:
For example, if you're updating user records via an API that allows only 10 updates per minute, you would set the batch size to 10 and add appropriate delays between batches.
Step 9: Advanced Configuration - Adding Delays Between Batches
To add delays between batch processing (useful for API rate limiting), you can use a Function node after the Split In Batches node:
// Add a delay of 2 seconds
await new Promise(resolve => setTimeout(resolve, 2000));
// Pass through the data unchanged
return items;
This adds a 2-second delay between processing each batch, which can help prevent hitting rate limits with external APIs.
Step 10: Example Workflow: Processing CSV Data in Batches
Let's create a practical example of using the Split In Batches node to process a large CSV file:
For the Function node, you could use code like this to process each batch:
// Process each item in the batch
const processedItems = items.map(item => {
// Example transformation: calculate a new field
item.json.total = (item.json.price \* item.json.quantity);
// Add a timestamp for tracking
item.json.processedAt = new Date().toISOString();
return item;
});
// Return the processed batch
return processedItems;
Step 11: Handling Errors in Batch Processing
When processing data in batches, it's important to handle errors properly so that one failing batch doesn't stop the entire workflow. Here's how to implement error handling:
You can also use try/catch blocks in Function nodes to handle errors within each batch:
// Process items with error handling
const processedItems = [];
for (const item of items) {
try {
// Process the item
const processedItem = {
json: {
...item.json,
processed: true,
processedAt: new Date().toISOString()
}
};
processedItems.push(processedItem);
} catch (error) {
// Handle the error for this item
console.error(`Error processing item ${item.json.id}: ${error.message}`);
// Add the item with error information
processedItems.push({
json: {
...item.json,
processed: false,
error: error.message
}
});
}
}
return processedItems;
Step 12: Monitoring Batch Progress
To monitor the progress of batch processing, you can add a Function node that logs information about each batch:
// Get the batch number from workflow data if available
const workflowData = $getWorkflowStaticData("global");
if (!workflowData.batchCount) {
workflowData.batchCount = 0;
workflowData.totalProcessed = 0;
}
// Increment the batch counter
workflowData.batchCount += 1;
workflowData.totalProcessed += items.length;
// Log batch information
console.log(`Processing batch #${workflowData.batchCount} with ${items.length} items`);
console.log(`Total items processed so far: ${workflowData.totalProcessed}`);
// Pass through the items unchanged
return items;
This allows you to track how many batches have been processed and how many items have been handled in total.
Step 13: Using Split In Batches with Webhooks
If you're receiving large amounts of data via webhooks, you can use the Split In Batches node to process this data in manageable chunks:
This approach helps prevent timeout issues when processing large webhook payloads.
Step 14: Combining Split In Batches with Merge Node
Sometimes you'll want to recombine the processed batches after they've been handled. For this, you can use the "Merge" node:
The key is to set the Merge node's mode to "Append" to combine all processed batches into a single output.
Step 15: Real-world Example: Batch Processing Database Records
Let's create a comprehensive example of using the Split In Batches node to update records in a database:
For the Function node that transforms data:
// Process customer data to prepare for update
return items.map(item => {
// Calculate loyalty tier based on purchase history
let loyaltyTier = 'Bronze';
if (item.json.totalPurchases > 50) {
loyaltyTier = 'Platinum';
} else if (item.json.totalPurchases > 25) {
loyaltyTier = 'Gold';
} else if (item.json.totalPurchases > 10) {
loyaltyTier = 'Silver';
}
// Add calculated fields
item.json.loyaltyTier = loyaltyTier;
item.json.lastUpdated = new Date().toISOString();
return item;
});
For the Database update node, you would configure an SQL query like:
UPDATE customers
SET loyalty\_tier = :loyaltyTier,
last\_updated = :lastUpdated
WHERE customer\_id = :id
Step 16: Performance Optimization with Split In Batches
To optimize performance when using the Split In Batches node:
Finding the optimal batch size often requires experimentation based on your specific use case.
Step 17: Using Environment Variables with Split In Batches
You can make your batch processing more flexible by using environment variables for batch size configuration:
{{$env.BATCH\_SIZE}}
This allows you to adjust batch sizes without modifying your workflow, which is particularly useful when moving between development and production environments.
Step 18: Troubleshooting Common Issues
Here are solutions to common issues when working with the Split In Batches node:
Step 19: Best Practices for Using Split In Batches
Follow these best practices when working with the Split In Batches node:
Step 20: Conclusion and Next Steps
The Split In Batches node is a powerful tool for managing large datasets in n8n workflows. By breaking data into manageable chunks, you can process information more efficiently, respect API rate limits, and avoid system resource constraints.
As you become more comfortable with batch processing in n8n, consider exploring these advanced topics:
With these techniques, you can handle virtually any volume of data efficiently within your n8n workflows.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.