What does the term "backpressure" mean in Rxjava?

Viewed 8793

I am a beginner of RxJava and I am curious about the meaning of "backpressure".

Does it mean that the producer puts pressure behind the consumer's back?

Or does it mean that consumers are putting pressure on producers? (Pressure in opposite direction)

3 Answers

I have read a lot about back-pressure but nothing satisfied me until I came across a post by Jonas (co-author of the reactive manifesto). I hope this will clarify your doubts regarding back-pressure.

When one component is struggling to keep-up, the system as a whole needs to respond in a sensible way. It is unacceptable for the component under stress to fail catastrophically or to drop messages in an uncontrolled fashion. Since it can’t cope and it can’t fail it should communicate the fact that it is under stress to upstream components and so get them to reduce the load. This back-pressure is an important feedback mechanism that allows systems to gracefully respond to load rather than collapse under it. The back-pressure may cascade all the way up to the user, at which point responsiveness may degrade, but this mechanism will ensure that the system is resilient under load, and will provide information that may allow the system itself to apply other resources to help distribute the load, see Elasticity.

Consider a situation where you have a huge amount of data , for eg. a sensor emits huge amount of data which is emitted at a high rate.
In such a scenario we need backpressuring , which in simple words is just a way to handle the items that can’t be processed.
Now in order to relieve this pressure we explicitly specify different backpressuring strategies like

  • Dropping (BackpressureStrategy.DROP) : This strategy drops items ,
    when it can't handle more than its capacity
  • Latest (BackpressureStrategy.LATEST) : If the downstream can't cope up with the flow of items, it stops emitting and waits till it becomes available again. It keeps dropping items , except the last one that arrived and sends the last one when the downstream is available.
  • Buffer (BackpressureStrategy.BUFFER) : Save the items in a buffer till they can be processed.

Demystifying RxJava Backpressure on Android at Uber Engineering

Related