Video in progress
For data processing, how do you choose among Java Stream, Reactor (Flux) and virtual threads?
Stream is for processing data that is in memory or read synchronously, with one thread (or CPU-bound parallel). Reactor is for backpressure, asynchronous sources and long-lived streams. Virtual threads are for running lots of I/O in parallel with plain blocking code. The choice between the last two often depends on the team's experience.
📖 Answer
All three are confused because their names resemble "stream", but they solve different problems.
| Java Stream | Reactor Flux | Virtual threads | |
|---|---|---|---|
| Model | pull, synchronous | push, asynchronous | blocking, plain code |
| Backpressure | none (not needed) | yes | through a semaphore/queue |
| Flow over time | no | yes (interval, window) |
you write it yourself |
| Errors and retry | try/catch | onErrorResume, retryWhen |
try/catch, a library |
| Debugging | easy | hard (stack traces) | easy |
When to use what:
- Stream: filtering a list, grouping, converting to DTOs: 90% of service code.
- Reactor: if WebFlux has already been chosen; a continuous flow from Kafka/RabbitMQ with backpressure; push over SSE/WebSocket; aggregation with time windows.
- Virtual threads (Java 21+): "call 200 services and gather the result", with imperative, readable code and without thinking about thread pool size.
In my experience many teams chose Reactor only for high concurrency. After Java 21 that reason got weaker: with virtual threads, even blocking JDBC and RestClient handle thousands of concurrent requests, without the complexity of reactive code (debugging, context propagation, block() mistakes). But Reactor keeps its place where you need backpressure and streaming semantics.
Be careful when mixing them: .toStream() or block() inside a Flux blocks a reactive thread; and long I/O inside synchronized in a virtual thread pins the carrier thread (up to Java 24).
For the decision I ask the team three questions: where the data comes from (memory, a request, a continuous flow), how much concurrent I/O there is, and how many people on the team can debug reactive code in prod.
Sign up to mark studied questions and save your progress.