In any processing pipeline the stages run at different speeds: a source may emit tens of thousands of events per second while the receiver keeps up with only a few thousand. Without back-pressure the surplus accumulates in queues and memory until resources run out; with it, the slowest stage sets the pace of the whole pipeline.
How it works
The idea is simple: the demand signal travels against the flow of data. The consumer declares how much it is ready to accept right now, and the producer sends no more than that.
| Layer | Back-pressure mechanism |
|---|---|
| TCP | receiver window: the receiver advertises how many bytes it can accept |
| HTTP/2 | WINDOW_UPDATE frames: separate windows for a stream and for the connection as a whole |
| Reactive Streams | request(n): the subscriber requests elements itself, and the publisher sends no more |
| Node.js | write() returns false, and sending resumes on the drain event |
| Message brokers | acknowledgements and prefetch: the consumer pulls tasks itself, at its own pace |
The Reactive Streams specification made non-blocking back-pressure a mandatory rule of asynchronous streams: the subscriber declares demand through request(n), and the publisher may not send more than was requested. Since Java 9 these interfaces have been part of the standard library as java.util.concurrent.Flow, and RxJava and Project Reactor are built on them.
When slowing down is not an option
When the source is external or the protocol cannot carry demand back upstream, only the overload strategies remain — in Reactor and RxJava these are onBackpressureBuffer, onBackpressureDrop and onBackpressureLatest:
- bounded buffer — absorb the burst and, on overflow, drop items or signal an error;
- drop the excess — for metrics and logs, where surviving the burst matters more than completeness;
- keep only the latest value — for sensor readings and quotes;
- error and stop — when no data may be lost.
A worked example: the producer emits 10,000 events per second while the consumer processes 2,000. Without back-pressure the queue grows by 480,000 messages a minute, and sooner or later memory runs out. With back-pressure the producer slows down, and occasional bursts are absorbed by a bounded buffer.
Where the term comes from
The word comes from hydraulics, where back pressure is pressure that opposes the flow. The model of “the subscriber requests the data itself” made the term part of the Reactive Streams specification.
Sources
the term is written from these pages- Reactive Streams — JVM specificationgithub.com/reactive-streams/reactive-streams-jvm(opens in a new tab)
- RFC 9113 — HTTP/2 (управление потоком)rfc-editor.org/rfc/rfc9113.html(opens in a new tab)
- Java 9 — java.util.concurrent.Flowdocs.oracle.com/javase/9/docs/api/java/util/concurrent/Flow.html(opens in a new tab)
Verified 8 Nov