Bookmarks

Back-pressure

A mechanism in which a slow consumer signals the producer to slow down, and the producer then sends data no faster than it can be processed.

Also: backpressure, back pressure, counterpressure

checked 8 Nov

All terms

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.

LayerBack-pressure mechanism
TCPreceiver window: the receiver advertises how many bytes it can accept
HTTP/2WINDOW_UPDATE frames: separate windows for a stream and for the connection as a whole
Reactive Streamsrequest(n): the subscriber requests elements itself, and the publisher sends no more
Node.jswrite() returns false, and sending resumes on the drain event
Message brokersacknowledgements 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

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

  • rate limiting

    Rate limiting cuts excess traffic off at the entrance, on the client side; back-pressure passes the overload signal back along the chain to the source itself.

  • circuit breaker

    A circuit breaker interrupts calls to a failing service to give it time to recover; back-pressure regulates the pace while the connection still works.

In depth in the references

where the topic is covered in detail