tutorial, no_image, rx,

Rx - no_image

Upendra Upendra Follow Jan 23, 2025 · 1 min read
Rx - no_image
Share this

Backpressure

Backpressure is the inability of a Subscriber to handle all incoming events in time. In other words Backpressure can occur when the Producer of events is faster than the Consumer. There are a variety of strategies with which you can exercise flow control and backpressure in RxJava in order to alleviate the problems caused when a quickly-producing Observable meets a slow-consuming observer.

For example, imagine using the zip operator to zip together two infinite Observables, one of which emits items twice as frequently as the other. A naive implementation of the zip operator would have to maintain an ever-expanding buffer of items emitted by the faster Observable to eventually combine with items emitted by the slower one.

This could cause RxJava to seize an unwieldy amount of system resources. Since it requires system resources to handle backpressure, you need to choose right backpressure strategy that suits your requirement. There are a few backpressure strategies:

  • Drop: Discards the unrequested items if it exceeds the buffer size
  • Buffer: Buffers all the items from the producer, watch for OOMs
  • Latest: Keeps only the most recent item
  • Error: throws a MissingBackpressureException in case of over emission
  • Missing: No strategy, it would throw a MissingBackpressureException sooner or later somewhere on the downstream

https://github.com/ReactiveX/RxJava/wiki/Backpressure
https://proandroiddev.com/rxjava-backpressure-and-why-you-should-care-369c5242c9e6
https://www.baeldung.com/rxjava-backpressure

credit goes to @swayangjit
Join Newsletter
Get the latest news right in your inbox. We never spam!
Upendra
Written by Upendra Follow
Hi, I am Upendra, the author in Human and machine languages,I don't know to how 3 liner bio works so just Connect with me on social sites you will get to know me better.