The release of Infinitic 0.16.0 introduces Batch Processing, a feature designed to optimize high-volume tasks by processing them in bulk.
Batch processing in Infinitic is ideal for scenarios such as:
When we set out to build batch processing, we encountered some engineering challenges. The requirements seemed straightforward: Combine messages into batches based on size and time limits.
But the devil was in the details! 🤔 The real challenge emerged when designing a system that could:
So, After doing a deep dive into Kotlin coroutines, the power of channels and structured concurrency became evident as we were able to accomplish these goals elegantly.
The new batch execution model lets developers define batch parameters using annotations like @Batch(maxMessages, maxSeconds)
, configuring tasks based on size and timing.
maxMessages = 50
, if your system receives 175 tasks:maxSeconds = 1.0
:Additionally, an optional batch key can be added to the task metadata. When present, the task will be added in a batch with other tasks having the same batch key.
Note: batches are counted as a single execution for the concurrency parameter, meaning up to concurrency
batches may be processed in parallel.
Consider a high-traffic service needing to process bulk email requests. With batch processing, you can set maxMessages = 50
and maxSeconds = 1.0
to efficiently send emails while adhering to API rate limits. Yet each email’s response remains distinct, allowing fine control over individual results in the workflow.
Let's examine a practical implementation:
@Batch(maxMessages = 50, maxSeconds = 1.0)
Map<String, EmailResult> sendEmail(Map<String, EmailRequest> requests) {
// Batch process up to 50 emails or after 1 second
return emailService.sendBatch(requests);
}
This simple configuration delivers impressive benefits:
To dive deeper into implementing batched tasks, explore our documentation. Try out this feature and share your experience with the community!