Handling Backpressure in Reactive Streams
Manage fast producers and slow WebSocket clients in WebFlux using Reactor backpressure operators so streams stay stable under load.
Handling Backpressure in Reactive Streams is a free WebSockets & Real-Time Systems with Spring lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the WebSockets & Real-Time Systems with Spring learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Fast Producer, Slow Consumer Problem
A reactive WebSocket may emit market ticks faster than a client can consume them. Without control, buffers grow until memory is exhausted. This mismatch is what backpressure solves.
What Backpressure Means
Backpressure is the consumer telling the producer how much it can handle. In Reactor, the subscriber requests n items; the publisher must not exceed that demand.
Reactor Is Demand-Driven
A reactive Flux built from a cold source naturally honors demand: nothing is produced until requested. The challenge appears with hot, time-driven sources like a price feed that emits regardless of demand.
WebSocketHandler Returns a Mono
In WebFlux a handler wires the outbound Flux into session.send. The framework subscribes and applies the transport's demand for you.
public Mono<Void> handle(WebSocketSession session) {
Flux<String> out = prices.map(p -> session.textMessage(p));
return session.send(out);
}onBackpressureBuffer
Buffer overflow items up to a limit, then take an action. Good when bursts are short.
flux.onBackpressureBuffer(1000,
dropped -> log.warn("dropped {}", dropped),
BufferOverflowStrategy.DROP_OLDEST);onBackpressureDrop
When the consumer is slow, simply drop new items. Ideal for telemetry where only the latest values matter.
flux.onBackpressureDrop(dropped -> metrics.increment("dropped"));onBackpressureLatest
Keep only the most recent item, discarding intermediate ones. Perfect for a live dashboard that shows the current value, not the history.
flux.onBackpressureLatest();Sampling and Throttling
Instead of dropping reactively, reduce the rate up front. sample emits the latest value at a fixed interval, smoothing a firehose into a manageable stream.
flux.sample(Duration.ofMillis(200));Bounding Buffers Everywhere
Unbounded buffers are the silent killer. Always cap buffers and choose a strategy (drop, error, latest) so a stalled client cannot consume the server's heap.
Detecting Overwhelmed Clients
If a client repeatedly triggers drops, it may be too slow for the feed. Consider lowering its update rate, sending deltas, or closing the session with a clear status.
session.close(CloseStatus.create(1011, "client too slow"));Choosing a Strategy
Match the operator to the data:
- Must-not-lose orders →
bufferwith a safe cap, or error - Live metrics/prices →
latestorsample - Best-effort telemetry →
drop
Quick Check
Test your backpressure understanding.
Recap
You tamed reactive streams:
- Backpressure aligns a fast producer with a slow consumer
- Reactor is demand-driven; hot sources need explicit handling
buffer,drop, andlateststrategies suit different datasamplethrottles a firehose at the source- Never leave buffers unbounded; close clients that cannot keep up
Frequently asked questions
Is the “Handling Backpressure in Reactive Streams” lesson free?
Yes — the full text of “Handling Backpressure in Reactive Streams” is free to read here on the web, and the WebSockets & Real-Time Systems with Spring course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the WebSockets & Real-Time Systems with Spring course, upgrade to CoddyKit PRO.
What will I learn in “Handling Backpressure in Reactive Streams”?
Manage fast producers and slow WebSocket clients in WebFlux using Reactor backpressure operators so streams stay stable under load. You practise WebSockets & Real-Time Systems with Spring with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start WebSockets & Real-Time Systems with Spring?
No prior experience is required. WebSockets & Real-Time Systems with Spring on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Handling Backpressure in Reactive Streams” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this WebSockets & Real-Time Systems with Spring lesson?
Yes. Every WebSockets & Real-Time Systems with Spring lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Reactive Programming
- WebFlux WebSocket Handlers
- Building Reactive Real-Time Services
- Handling Backpressure in Reactive Streams