Backpressure is the mechanism that lets a slow subscriber tell a fast publisher how many items it can accept. In Reactive Streams the subscriber calls request(n), and the publisher may never emit more than the total requested. When a source cannot be slowed down, Project Reactor gives you operators such as onBackpressureBuffer(), onBackpressureDrop(), onBackpressureLatest() and onBackpressureError() to decide what happens to the surplus.
Reactor is the reactive library behind Spring WebFlux, and it implements the Reactive Streams specification (also mirrored in the JDK as java.util.concurrent.Flow). Most pipelines never need explicit backpressure code because demand propagates upstream automatically. You only step in when a source ignores demand, such as a timer, a message listener or a UI event stream.
A Subscriber receives a Subscription in onSubscribe. Items only flow after it calls request(n). A publisher that respects the contract stops emitting once the outstanding demand reaches zero and resumes when more is requested.
Plain subscribe(consumer) requests Long.MAX_VALUE, which means unbounded demand. That is why a simple Flux.range(1, 1000).subscribe(...) with a slow lambda never overflows: the producer simply runs as fast as the consumer thread lets it. Backpressure only becomes visible when you request a bounded amount, or when an asynchronous boundary such as publishOn sits between the two sides.
BaseSubscriber is the simplest way to request items yourself, one at a time or in small batches.
The subscriber never holds more than two unprocessed items. For most code, limitRate(n) gives the same effect without a custom subscriber: it requests in chunks of n (replenishing at 75 percent by default) instead of unbounded.
When the producer cannot slow down, choose one of these operators.
| Operator | Behaviour when the subscriber is slow | Typical use |
|---|---|---|
onBackpressureBuffer() | Queues items; unbounded by default, bounded with a size argument | Short bursts where no data may be lost |
onBackpressureDrop() | Discards items that arrive without demand | Metrics, sensor readings, UI events |
onBackpressureLatest() | Keeps only the most recent item | Latest price or state |
onBackpressureError() | Signals IllegalStateException (overflow) | Fail fast when overflow is a bug |
An unbounded buffer can exhaust the heap, so prefer a bounded one with an explicit overflow policy.
Flux.interval cannot be slowed down, and publishOn introduces the asynchronous boundary that makes demand bounded (prefetch of 16 here). The buffer holds up to 100 items. When it is full, DROP_OLDEST evicts the oldest entry and calls the consumer you supplied. The other strategies are DROP_LATEST and ERROR.
Use onBackpressureLatest() in place of onBackpressureDrop() when the newest value is the only one that matters.
If every item must be processed, slow the producer or batch the work rather than dropping.
buffer(100) or window(100) groups items so the subscriber handles batches.limitRate(n) shapes demand toward a source that does respect it, such as a database cursor.flatMap(fn, concurrency) caps how many inner operations run at once. A call to a remote service inside flatMap should always have a concurrency limit.sample(Duration) or delayElements(Duration) thin out or space the stream.Flux.create and Flux.push let you bridge callback APIs, and they take an overflow strategy.
The default strategy is BUFFER. Flux.generate is synchronous and pull based, so it respects demand with no extra work.
Partly. Demand propagates through Reactor operators and to the network layer, so a slow HTTP client slows reading from a well-behaved source. Sources that push on their own, such as message listeners or timers, still need an explicit strategy.
Usually because the subscriber requests unbounded demand and runs on the same thread, so there is no backlog. Overflow appears once a bounded request or an asynchronous boundary like publishOn is involved.
Use a bounded buffer for short bursts where data matters, drop or latest where freshness matters more than completeness, and error when overflow indicates a bug.
Backpressure is demand signalled by the consumer so it is not overwhelmed. Rate limiting caps throughput by policy regardless of consumer capacity.
Backpressure in Reactor is built on request(n). Control demand with BaseSubscriber or limitRate, and handle sources that ignore demand with a bounded onBackpressureBuffer, onBackpressureDrop, onBackpressureLatest or onBackpressureError. Always limit concurrency inside flatMap and prefer bounded buffers over unbounded ones.