What is Batching?
Batching allows you to process multiple task executions together in a single operation. Instead of executing tasks one at a time, Infinitic can group multiple pending tasks and execute them as a batch. This is invaluable for:- Database operations (bulk inserts, batch queries)
- External API calls (reducing API call count)
- Network operations (minimizing round trips)
- Resource-intensive operations (amortizing setup costs)
The @Batch Annotation
Location in source code:io.infinitic.annotations.Batch
@Batch annotation marks a method as a batch implementation. When applied, Infinitic will:
- Collect multiple pending task executions
- Group them according to batching configuration
- Execute the batch method once with all tasks
- Distribute results back to individual tasks
Basic Usage
To use batching, define both a regular method and a batch implementation:Batch Method Signature
Batch methods have specific signature requirements:Input: Map of Task IDs to Parameters
The batch method receives aMap<String, T> where:
- Key: Task ID (String)
- Value: Input parameters for that task
Output: Map of Task IDs to Results
The batch method must return aMap<String, R> where:
- Key: Task ID (String) - must match input keys
- Value: Result for that task
Void Return Type
For methods that returnvoid/Unit, the batch method returns Map<String, Unit>:
Complete Example
Here’s a complete example from the Infinitic test suite: Location in source code:/home/daytona/workspace/source/infinitic-tests/src/test/kotlin/io/infinitic/tests/batches/BatchService.kt:42-95
Batch Configuration
Configure batching behavior in your worker configuration:Configuration Parameters
- maxMessages: Maximum number of tasks to include in a batch (default: unlimited)
- maxSeconds: Maximum time to wait before processing a batch (default: no limit)
- The batch reaches
maxMessagestasks, OR maxSecondshave elapsed since the first task was received
Accessing Task Context in Batches
Within batch methods, access individual task contexts usingTask.getContext(taskId):
Batch Keys
Batch keys allow you to group tasks with related data together:Use Cases
Database Bulk Operations
External API Optimization
Data Enrichment
ML Model Inference
Error Handling
Handle errors for individual tasks within a batch:Best Practices
Choose Appropriate Batch Sizes
Balance between latency and throughput:Maintain Task Independence
Each task in a batch should be independent:Handle Partial Failures Gracefully
Don’t let one task failure affect the entire batch:Monitor Batch Performance
Performance Considerations
Batching can provide significant performance improvements:- Database operations: 10-100x faster for bulk inserts
- API calls: Reduce from N calls to 1 call
- Network overhead: Minimize connection setup costs
- Resource utilization: Better CPU and memory usage
Next Steps
Service Context
Learn more about accessing task information in services
Defining Services
Go back to service basics