Software Architecture
Reactive Systems: Backpressure, Reactive Streams and Project Reactor
Advanced8 min readUpdated
Reactive programming solves a specific problem: serving many concurrent connections without a thread per connection, while letting a slow consumer tell a fast producer to slow down. This guide separates the architectural traits from the programming model, explains backpressure and the Reactive Streams contract precisely, works through Reactor code, and is direct about when the model is not worth its cost.
What you will learn
- Explain the four traits of a reactive system and how they differ from reactive programming
- Describe what blocks a thread and why non-blocking I/O changes the scaling curve
- Apply the Reactive Streams contract and reason about backpressure strategies
- Compose asynchronous pipelines with Reactor's Flux and Mono, including error handling
- Decide between a reactive stack and virtual threads for a given workload
"Reactive" names two different things, and conflating them causes most of the confusion around it. A reactive system is an architectural property: responsive under load and failure, achieved by isolating components and connecting them with asynchronous messages. Reactive programming is a coding style for expressing asynchronous data flows as pipelines of operators. You can build a thoroughly reactive system with plain queues and blocking consumers, and you can write reactive code that gives you none of the architectural properties. It is worth being clear which one a discussion is about.
The engineering problem underneath is narrow and real. A server that dedicates one operating-system thread to each in-flight request can hold a few thousand of them before memory and scheduling costs dominate, and most of those threads spend their time blocked waiting for a database or a downstream API rather than computing anything. Reactive libraries let a small number of threads carry many in-flight requests, and add a mechanism for a slow consumer to limit a fast producer. Everything else is consequence.
The four traits
The reactive manifesto describes a system as reactive when it exhibits four related properties.
Responsive — it answers in a predictable time, and when it cannot, it says so quickly rather than leaving callers waiting. This is the goal; the other three are means.
Resilient — failure in one component is contained and does not become failure of the whole. Achieved by isolation, replication and delegating recovery to a supervisor rather than expecting the failing component to fix itself.
Elastic — throughput scales with resources in both directions, which requires no central bottleneck and no shared mutable state that must be coordinated.
Message-driven — components communicate through asynchronous message passing, which is what creates the boundaries that make isolation and elasticity possible in the first place, and what allows load management and backpressure to be expressed explicitly.
Read that way, "reactive system" describes goals that a well-built event-driven microservice platform already pursues. The manifesto's contribution is naming the connection between asynchronous boundaries and resilience, not inventing a new architecture.
What blocking costs
To see why non-blocking matters, follow a thread through a typical request. The handler receives the request, issues a SQL query, and calls ResultSet.next(). That call ultimately performs a socket read, and the thread cannot proceed until bytes arrive. The operating system parks it: its 512 KB to 1 MB of stack stays allocated, its registers are saved, and the scheduler moves on. For the 20 ms the query takes, the thread does nothing and occupies memory.
With a pool of 200 threads and a 20 ms average dependency latency, the arithmetic is unforgiving. Little's law says concurrency equals throughput multiplied by latency, so 200 concurrent threads at 20 ms yields roughly 10 000 requests per second at best — and every request arriving beyond that queues, adding latency, which increases concurrency, which lengthens the queue. This is the mechanism behind the sharp cliff blocking servers fall off, explored further in application performance.
Non-blocking I/O breaks the coupling between a request and a thread. The socket is registered with a selector; when data is ready, an event loop thread picks up the continuation and runs it. A handful of event-loop threads — conventionally one per core — carry thousands of in-flight requests, because a request in flight costs a small object rather than a thread stack.
The price is that the code is no longer a sequence of statements. The continuation must be expressed as a callback, and callbacks that call callbacks become unreadable quickly. Reactive libraries exist to make that composition tolerable.
Reactive Streams and backpressure
Asynchronous push has a failure mode of its own: a producer faster than its consumer fills memory until the process dies. Backpressure is the consumer's ability to signal how much it can take.
The Reactive Streams specification defines the minimal contract for that, with four interfaces — mirrored in the JDK as java.util.concurrent.Flow.
public interface Publisher<T> { void subscribe(Subscriber<? super T> s); }
public interface Subscriber<T> {
void onSubscribe(Subscription s);
void onNext(T item); // never called more times than requested
void onError(Throwable t); // terminal
void onComplete(); // terminal
}
public interface Subscription {
void request(long n); // demand: "I can handle n more"
void cancel();
}
public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {}
The essential rule is that nothing flows until the subscriber calls request(n). The publisher may emit at most n elements before the subscriber asks for more, so demand is pulled while data is pushed. Signals are serialised — no concurrent onNext for one subscriber — and onError and onComplete are terminal, after which nothing further arrives. Because the contract is an interface set rather than an implementation, libraries interoperate: a Reactor Flux can subscribe to a publisher produced by a different implementation.
When a source genuinely cannot be slowed — sensor readings, a market data feed, an upstream that does not honour demand — you must choose what to sacrifice.
| Strategy | Behaviour | Appropriate when |
|---|---|---|
| Buffer | Queue overflow in memory | Bursts are short and bounded; always set a limit |
| Drop | Discard new items when the consumer is behind | Latest-value feeds where staleness is worse than loss |
| Latest | Keep only the most recent item | Dashboards, price tickers, position updates |
| Error | Fail the stream with an overflow signal | Losing data is unacceptable and failing loudly is correct |
| Block the source | Slow the producer at its origin | The producer is yours and can be throttled |
Choosing "buffer" without a bound is the same as choosing "run out of memory later".
Reactor in practice
Project Reactor implements Reactive Streams and provides two publisher types: Mono<T> for zero or one element, and Flux<T> for zero to many. Both are lazy — declaring a pipeline does nothing until something subscribes, which is a frequent source of "my code never ran" confusion.
Mono<OrderView> orderView(String orderId) {
Mono<Order> order = orders.findById(orderId) // Mono<Order>
.switchIfEmpty(Mono.error(new UnknownOrder(orderId)));
return order.flatMap(o ->
Mono.zip( // run both calls concurrently
customers.findById(o.customerId())
.timeout(Duration.ofMillis(300))
.onErrorResume(e -> Mono.just(Customer.unknown())),
pricing.quote(o)
.retryWhen(Retry.backoff(3, Duration.ofMillis(50)).jitter(0.5))
)
.map(t -> new OrderView(o, t.getT1(), t.getT2())));
}
Three habits distinguish workable Reactor code from the kind people abandon. Use flatMap when the transformation itself returns a publisher and map when it does not — mixing them up produces Mono<Mono<T>>. Use zip to run independent calls concurrently rather than chaining them, since sequential flatMap calls wait for each other. And attach timeout, retryWhen and onErrorResume where the failure is meaningful, not once at the end of the pipeline.
Streaming a large result set is where the model genuinely shines, because nothing is ever fully materialised:
Flux<Trade> stream = trades.findByBook(bookId) // reactive driver, backpressure-aware
.limitRate(256) // request in batches of 256
.filter(t -> t.notional().compareTo(THRESHOLD) > 0)
.buffer(Duration.ofSeconds(1)) // group by time window
.flatMap(batch -> enrichment.enrich(batch), 4) // at most 4 concurrent enrichments
.onBackpressureBuffer(10_000,
dropped -> log.warn("dropping {}", dropped.id()),
BufferOverflowStrategy.DROP_OLDEST);
Scheduling is explicit and worth understanding. Reactor does not choose threads for you: by default work runs on whichever thread emitted the signal. subscribeOn decides where the subscription — and therefore the source — runs, and affects the whole chain upstream of it. publishOn switches the thread for everything downstream of that point. When blocking code is unavoidable, isolate it:
Mono.fromCallable(() -> legacyJdbcLookup(id)) // blocking, third-party, no reactive driver
.subscribeOn(Schedulers.boundedElastic()) // dedicated pool, never the event loop
.timeout(Duration.ofSeconds(2));
Debugging deserves a warning. A stack trace from a reactive pipeline shows the operator machinery, not the line of business code that assembled it. Enable Hooks.onOperatorDebug() in development or add the reactor-tools agent, and propagate a correlation identifier through the Reactor context rather than a ThreadLocal, because there is no thread affinity to rely on. MDC-based logging needs explicit help for the same reason.
When reactive pays, and when it does not
The model earns its complexity in a narrow band of workloads.
It pays when a service is an I/O-bound aggregator holding many connections that are mostly idle, when you need streaming semantics — server-sent events, WebSockets, a Kafka consumer with flow control, as in Kafka producers and consumers — when memory per connection is the binding constraint, or when the workload genuinely has stream shape and the operator vocabulary for merging, windowing and rate limiting saves substantial code.
It does not pay when the work is CPU-bound, since non-blocking I/O does nothing for computation. It does not pay when the stack blocks anywhere that matters — a reactive web layer in front of a blocking JDBC driver gives you the debugging difficulty with none of the scaling. It does not pay for services handling a few hundred concurrent requests, which is most internal enterprise services. And it does not pay when the team will not maintain it: reactive code is harder to read, harder to debug and unforgiving of a single blocking call, and a codebase half-converted is worse than either extreme.
Virtual threads as the alternative
Virtual threads, standardised in JDK 21, change this calculus for most applications. A virtual thread is scheduled by the JVM onto a small pool of carrier threads; when it performs a blocking operation on a channel or socket, the JVM unmounts it from its carrier and the carrier runs something else. The blocking cost disappears while the code remains ordinary sequential Java.
| Aspect | Reactive (Reactor) | Virtual threads |
|---|---|---|
| Code style | Operator pipelines, callbacks | Ordinary sequential code |
| Blocking calls | Must be isolated on a separate scheduler | Fine — that is the point |
| Stack traces | Operator frames, needs debug hooks | Complete and familiar |
| Backpressure | Built into the contract | Not provided; use bounded queues and semaphores |
| Streaming operators | Rich vocabulary | Manual, or use a stream library on top |
| Profiling and tooling | Specialised | Standard JFR, thread dumps, debuggers |
| Learning curve | Steep | Negligible |
The practical guidance for new JVM services in 2026: start with virtual threads and a thread-per-request model, which is simpler and gives most of the concurrency benefit. Reach for a reactive library when you need backpressure across component boundaries or the streaming operator vocabulary — not merely because you need to handle many connections. Java's own concurrency and executors guide covers the threading model in detail. Note the remaining caveats: a virtual thread pinned by a synchronized block holding a lock across a blocking call still occupies its carrier in older releases, so prefer ReentrantLock in hot paths, and pooling virtual threads defeats their purpose entirely.
Key takeaways
- A reactive system is an architectural property — responsive, resilient, elastic, message-driven; reactive programming is one way to implement parts of it, not the same thing.
- The concrete benefit of non-blocking I/O is decoupling in-flight requests from threads, which changes how a server degrades under high concurrency, not how fast one request is.
- Backpressure is the defining feature of Reactive Streams: nothing is emitted beyond what the subscriber has requested, and unbounded buffering is a deferred failure.
- Reactor's
FluxandMonoare lazy, compose declaratively, and require deliberate scheduling — any blocking call must be isolated onboundedElastic. - Reactive pays for I/O-bound, streaming, high-connection workloads and costs you dearly everywhere else, especially where any layer still blocks.
- For most new JVM services, virtual threads deliver the scaling with ordinary code and ordinary tooling; choose reactive when you specifically need backpressure or stream operators.
Sources and further reading
Frequently asked questions
- Is reactive code faster than blocking code?
- Not per request — an individual call usually takes slightly longer because of scheduling and operator overhead. What changes is the shape of the curve under concurrency: a blocking server degrades sharply once its thread pool is exhausted, while a non-blocking server keeps accepting connections with modest memory growth. If your service handles a few hundred concurrent requests, you will see no benefit at all.
- What actually happens if I call a blocking API inside a reactive pipeline?
- You occupy one of a very small number of event-loop threads — typically one per CPU core — for the whole duration of that call, and every other request assigned to that thread waits. A handful of such calls can stall the entire server. If you must call blocking code, schedule it on a bounded elastic scheduler with subscribeOn or publishOn so it runs off the event loop.
- Do virtual threads make reactive programming obsolete?
- They remove its main justification for most applications. Virtual threads give you the scaling behaviour of non-blocking I/O while keeping ordinary sequential code, debuggable stack traces and try/catch. What they do not give you is backpressure or a declarative operator vocabulary for merging, buffering and windowing streams, so genuinely stream-shaped workloads still benefit from a reactive library.
- How is a Flux different from a Java Stream?
- A Stream is pulled synchronously by the consumer and completes when the terminal operation finishes, so it cannot represent something that produces values over time. A Flux is pushed asynchronously, may be infinite, carries completion and error as first-class signals, and lets the subscriber request a bounded number of elements. Streams model collections; Flux models events arriving.