Scrivio Interview library
ScenarioIntermediate7 min read · updated 12 November 2026

A request handler sorts and filters large lists with parallelStream(). At low traffic it is fast, but at high traffic latency is worse than with sequential streams. Why, and what would you recommend?

All concurrent requests run their parallel streams on the same small ForkJoinPool.commonPool(), so under high traffic the parallel work of many requests competes for a few threads. The server already has request-level parallelism that keeps the cores busy, so per-request parallelism adds splitting, merging and queueing overhead with nothing to gain. In request paths, use sequential streams; keep parallel streams for batch jobs or a dedicated ForkJoinPool, and benchmark at realistic concurrency rather than with a single request.

Why this question is asked

A parallel stream is one of the easiest optimisations in Java: change stream() to parallelStream() and a benchmark on a laptop shows the operation getting several times faster. The surprise comes in production, where the same change makes the service slower under load. Interviewers like this scenario because the answer requires us to think about the whole server, not one request. In this answer, we will recap how parallel streams run, compare one request with many, look at the costs that parallel streams add, and decide where they belong and how to test them honestly.

1A Quick Recap of How Parallel Streams Run

A parallel stream splits its source, for example an ArrayList, into chunks using a Spliterator, processes the chunks as fork/join tasks, and then merges the partial results. The tasks run in ForkJoinPool.commonPool(), a single pool shared by the whole JVM, plus the calling thread, which also works on chunks. The common pool has by default one worker fewer than the number of available processors, so on an 8-core machine, 7 workers plus the caller can work on one stream.

The important word is shared. Every parallel stream in the JVM, from every request and every library, uses the same 7 workers.

2One Request Versus Two Hundred

With one request, the parallel stream has the machine to itself. Sorting and filtering a list of 200,000 items, which takes 40 ms sequentially, can finish in roughly 8 to 10 ms on 8 cores. That is the measurement that convinces people.

Under high traffic, the picture changes completely. Suppose the web server is handling 200 requests at once on its 200 request threads. Those 200 threads are already able to keep all 8 cores busy, each running its own request. This is request-level parallelism, and it is the main way a server uses its cores. Now each of those requests also tries to split its work into many tasks for the common pool. All of them land in the same 7 workers' queues. A request's chunks wait behind other requests' chunks, so its latency now includes queueing in the common pool, and the cores still only have 8 cores' worth of work to do.

Java
// Same handler, same data: 200,000 items to filter and sort
List<Item> result = items.parallelStream()
.filter(i -> i.matches(query))
.sorted(BY_SCORE)
.toList();
// 1 concurrent request: parallel 9 ms, sequential 40 ms
// 200 concurrent requests: parallel p99 900 ms, sequential p99 600 ms
// (CPU at 100% in both; parallel adds split/merge work and queueing)
Why the same code behaves differently
ConditionIdle coresParallel stream effect
1 request at a time7 of 8uses idle cores: about 4x faster
20 concurrent requestsfewsmall gain, more variance
200 concurrent requestsnoneslower: overhead plus queueing in the common pool

Notice that the CPU is fully used in both cases at high traffic. Parallel streams cannot create more CPU, they can only divide it differently, and under load there is no idle CPU to divide. What they add is extra work and extra waiting.

3The Overhead That Only Pays Off for Big CPU Work

A parallel stream does more total work than a sequential one. It splits the source into chunks, creates and schedules a task for each chunk, steals tasks between threads, and merges the results. A sorted() step must sort the chunks and then merge them, and a toList() must combine partial lists. When cores are idle, that extra work is cheap compared with the speed-up. When cores are busy, it is pure cost.

So parallel streams only pay off when the work is large and CPU-bound, roughly when the per-element work times the number of elements reaches at least tens of thousands of simple operations, and when there are idle cores to use. Small lists, cheap operations, sources that split badly such as a LinkedList or an I/O stream, and anything that blocks are poor candidates even with idle cores.

4What We Recommend

In request paths, use sequential streams. Each request then uses one core efficiently, and the server's request threads spread the requests across the cores, which is exactly what we want under load. If one request type really does need heavy computation, the better questions are whether the result can be cached, precomputed or done with a better algorithm, such as filtering before sorting or using a partial sort for the top 20 results.

Parallel streams belong in batch jobs and offline processing, where one big computation has the machine, or a part of it, to itself. If parallel work must run inside a service, give it its own ForkJoinPool so it cannot slow down unrelated requests and so we control its size. A widely used way is to run the stream from inside a task submitted to that pool, because a parallel stream started on a ForkJoinPool worker runs its tasks in that pool. That is an implementation behaviour rather than a documented promise, so test it on your JDK version.

Java
// Request path: sequential
List<Item> result = items.stream().filter(i -> i.matches(query)).sorted(BY_SCORE).limit(20).toList();
// Batch job: a dedicated pool for heavy parallel work
ForkJoinPool batchPool = new ForkJoinPool(4); // half the cores, leaves room for the rest
List<Report> reports = batchPool.submit(() ->
accounts.parallelStream().map(this::buildReport).toList()
).get(); // stream tasks run in batchPool, not the common pool

5Benchmarking at Realistic Concurrency

The original mistake came from measuring one request on an idle machine. To decide honestly, we measure at the concurrency the service really sees. With JMH, the Java Microbenchmark Harness, the @Threads annotation runs the benchmark from many threads at once, for example 64, so the parallel and sequential versions compete for cores as they would in production. Better still is a load test of the real endpoint at peak traffic, comparing p50 and p99 latency and throughput for both versions.

Whatever the method, the rule is to compare under the load we care about. A change that halves latency for one user and doubles it for everyone at peak is not an optimisation.

Java
@Benchmark @Threads(64) // 64 concurrent callers, like a busy server
public List<Item> parallel() { return items.parallelStream().filter(f).sorted(BY_SCORE).toList(); }
@Benchmark @Threads(64)
public List<Item> sequential() { return items.stream().filter(f).sorted(BY_SCORE).toList(); }
// typical result at 64 threads: sequential has higher throughput and lower p99

6Follow-up questions

Would a larger common pool fix the problem?

No. More common pool threads than cores just means more threads competing for the same CPUs, with more context switching. The cores are already saturated by request threads, so there is no spare capacity for any size of pool to use.

Is it ever right to use a parallel stream inside a request?

Occasionally, in a service with low concurrency and a few heavy, CPU-bound requests, such as an internal analytics endpoint. Even then a dedicated, bounded ForkJoinPool is safer than the common pool, and the decision should come from a load test at its real concurrency.

Why does the calling thread also do work in a parallel stream?

The thread that starts the terminal operation runs part of the work itself, then helps with or waits for the rest. That is why the common pool has one worker fewer than the number of cores: together with the caller, a single stream uses every core.

7Conclusion

In this answer, we saw why a parallel stream that is fast for one request becomes slower at high traffic. All requests share the common pool's few workers, the server's request threads already keep the cores busy, and parallel streams add splitting, merging and queueing work that only pays off for large CPU-bound tasks on idle cores. In request paths we use sequential streams, keep parallel streams for batch work or a dedicated ForkJoinPool, and benchmark at the concurrency the service really sees.

What to say in the interviewAbout 90 seconds aloud

Open with

At low traffic the parallel stream has idle cores to use, so it is faster. At high traffic two things change. First, every parallel stream in the JVM uses the same ForkJoinPool.commonPool, which has one worker fewer than the number of cores, so the parallel work of all concurrent requests competes for maybe seven threads, and each request's chunks wait in queues behind other requests' chunks.

Then, if they want more

Second, the server already has parallelism at the request level: 200 request threads keep all the cores busy on their own. Parallel streams cannot create more CPU, they only split the work differently, and they do more total work: splitting the list, scheduling tasks, stealing, sorting chunks and merging results.

With no idle cores, that overhead and the queueing are pure cost, so latency gets worse than sequential. My recommendation is sequential streams in request paths, and if one request type is genuinely heavy, look at caching, precomputing or a better algorithm, for example filtering first and taking only the top results.

Parallel streams belong in batch jobs where one computation owns the machine, or in a dedicated ForkJoinPool with a chosen size so they cannot hurt other requests.

And I would benchmark at realistic concurrency, either JMH with many threads or a load test at peak, comparing p99 and throughput, because measuring one request on an idle machine is what led to the change in the first place.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A request handler sorts and filters large lists with parallelStream(). At low traffic it...": https://scrivio-interview-library.netlify.app/java/executors/9df5649213/