Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you handle backpressure in reactive streams?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 13, 2024
UpdatedNov 16, 2026
6 min read

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.

Table of Contents

  • How demand flows from subscriber to publisher
  • Controlling demand with BaseSubscriber
  • Backpressure strategies in Reactor
    • Buffering with a bound
    • Dropping or keeping only the latest
  • Reducing load without losing data
  • Creating sources that respect backpressure
  • Frequently Asked Questions
    • Does Spring WebFlux handle backpressure for me?
    • Why does my onBackpressureBuffer never overflow?
    • Which strategy should I pick?
    • What is the difference between backpressure and rate limiting?
  • Summary

How demand flows from subscriber to publisher

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.

Controlling demand with BaseSubscriber

BaseSubscriber is the simplest way to request items yourself, one at a time or in small batches.

java
text

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.

Backpressure strategies in Reactor

When the producer cannot slow down, choose one of these operators.

OperatorBehaviour when the subscriber is slowTypical use
onBackpressureBuffer()Queues items; unbounded by default, bounded with a size argumentShort bursts where no data may be lost
onBackpressureDrop()Discards items that arrive without demandMetrics, sensor readings, UI events
onBackpressureLatest()Keeps only the most recent itemLatest price or state
onBackpressureError()Signals IllegalStateException (overflow)Fail fast when overflow is a bug

Buffering with a bound

An unbounded buffer can exhaust the heap, so prefer a bounded one with an explicit overflow policy.

java

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.

Dropping or keeping only the latest

java

Use onBackpressureLatest() in place of onBackpressureDrop() when the newest value is the only one that matters.

Reducing load without losing data

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.

Creating sources that respect backpressure

Flux.create and Flux.push let you bridge callback APIs, and they take an overflow strategy.

java

The default strategy is BUFFER. Flux.generate is synchronous and pull based, so it respects demand with no extra work.

Frequently Asked Questions

Does Spring WebFlux handle backpressure for me?

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.

Why does my onBackpressureBuffer never overflow?

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.

Which strategy should I pick?

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.

What is the difference between backpressure and rate limiting?

Backpressure is demand signalled by the consumer so it is not overwhelmed. Rate limiting caps throughput by policy regardless of consumer capacity.

Summary

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.

Share