Video in progress
Should you ban parallel streams across the codebase? How do you justify the decision?
In web-service code I ban them by default: requests are already parallel, the common pool is a single one for the whole JVM, and if I/O gets in there everything stops. In batch or computation modules, exceptions backed by measurements are allowed. This is written as an ADR and checked with ArchUnit.
📖 Answer
A decision like this must come from context, not from the idea that "parallel streams are bad".
Arguments for a ban in a web service (Spring MVC, Tomcat):
- Tomcat already runs requests in parallel with 200 threads. Parallelizing a single request further doesn't increase overall throughput; it only reduces the latency of one request while raising others'.
ForkJoinPool.commonPool()is one per JVM:CompletableFuture's default executor is there too. One module's mistake slows another module.- If the container's CPU limit is 1–2, the common pool's parallelism drops to 1: the parallel stream is effectively sequential, but with extra overhead.
- Determinism: floating point in
reduce,forEachorder, lambdas with side effects.
Where it's allowed: offline computation, report generation, ML features, CLI tools. There the JVM is dedicated to one big task and using the CPU fully is the goal.
How I formalize it:
- ADR: context, options (ban, free use, ban with exceptions), the decision and the exception process.
- ArchUnit: calls to
parallelStream()andBaseStream.parallel()only in allowed packages. - Evidence for an exception: a JMH result and a measurement on a core count similar to prod, in the PR description.
- Alternative: virtual threads or a bounded executor for I/O fan-out;
Gatherers.mapConcurrenton Java 24+.
One thing not to forget: libraries can also use parallel streams, and ArchUnit doesn't see them. To watch the common pool, it helps to export commonPool-worker threads as a metric from JFR or thread dumps.
The decision doesn't have to be absolute: a mechanism of "banned, but justified here" pushes people to bring evidence rather than work around it.
Sign up to mark studied questions and save your progress.