You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/utilities/batch.md
+81-1Lines changed: 81 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -484,7 +484,9 @@ used with SQS FIFO. In that case, an `UnsupportedOperationException` is thrown.
484
484
in most cases the defaults work well, and changing them is more likely to decrease performance
485
485
(see [here](https://www.baeldung.com/java-when-to-use-parallel-stream#fork-join-framework)
486
486
and [here](https://dzone.com/articles/be-aware-of-forkjoinpoolcommonpool)).
487
-
In situations where this may be useful - such as performing IO-bound work in parallel - make sure to measure before and after!
487
+
In situations where this may be useful, such as performing IO-bound work in parallel, make sure to measure before and after!
488
+
489
+
When using parallel processing with X-Ray tracing enabled, the Tracing utility automatically handles trace context propagation to worker threads. This ensures that subsegments created during parallel message processing appear under the correct parent segment in your X-Ray trace, maintaining proper trace hierarchy and visibility into your batch processing performance.
488
490
489
491
490
492
=== "Example with SQS"
@@ -536,6 +538,84 @@ used with SQS FIFO. In that case, an `UnsupportedOperationException` is thrown.
536
538
}
537
539
```
538
540
541
+
=== "Example with X-Ray Tracing"
542
+
543
+
```java hl_lines="12 17"
544
+
public class SqsBatchHandler implements RequestHandler<SQSEvent, SQSBatchResponse> {
545
+
546
+
private final BatchMessageHandler<SQSEvent, SQSBatchResponse> handler;
@Tracing // This will appear correctly under the handleRequest subsegment
561
+
private void processMessage(Product p, Context c) {
562
+
// Process the product - subsegments will appear under handleRequest
563
+
}
564
+
}
565
+
```
566
+
567
+
### Choosing the right concurrency model
568
+
569
+
The `processBatchInParallel` method has two overloads with different concurrency characteristics:
570
+
571
+
#### Without custom executor (parallelStream)
572
+
573
+
When you call `processBatchInParallel(event, context)` without providing an executor, the implementation uses Java's `parallelStream()` which leverages the common `ForkJoinPool`.
574
+
575
+
**Best for: CPU-bound workloads**
576
+
577
+
- Thread pool size matches available CPU cores
578
+
- Optimized for computational tasks (data transformation, calculations, parsing)
When you call `processBatchInParallel(event, context, executor)` with a custom executor, the implementation uses `CompletableFuture` which gives you full control over the thread pool.
590
+
591
+
**Best for: I/O-bound workloads**
592
+
593
+
- You control thread pool size and characteristics
Virtual threads are lightweight and can handle thousands of concurrent I/O operations efficiently without the overhead of platform threads.
614
+
615
+
**Recommendation for typical Lambda SQS processing:**
616
+
617
+
Most Lambda functions processing SQS messages perform I/O operations (calling APIs, querying databases, writing to S3). For these workloads, use the custom executor approach with a thread pool sized appropriately for your I/O operations or virtual threads for Java 21+.
Copy file name to clipboardExpand all lines: powertools-batch/src/main/java/software/amazon/lambda/powertools/batch/handler/DynamoDbBatchMessageHandler.java
Copy file name to clipboardExpand all lines: powertools-batch/src/main/java/software/amazon/lambda/powertools/batch/handler/KinesisStreamsBatchMessageHandler.java
0 commit comments