Chapter 6. Flow Control and Backpressure

So far, we’ve become very familiar with the push-based nature of RxJava. Events are produced somewhere up in the stream to be consumed later by all subscribers. We never really paid much attention to what happens if Observer is slow and cannot keep up with events emitted from within Observable.create(). This entire chapter is devoted to this problem.

RxJava has two ways of dealing with producers being more active than subscribers:

  • Various flow-control mechanisms such as sampling and batching are implemented via built-in operators

  • Subscribers can propagate their demand and request only as many items as they can process by using a feedback channel known as backpressure.

These two mechanisms are described in this chapter.

Flow Control

Before RxJava began implementing backpressure (see the section “Backpressure”), dealing with producers (Observables) outperforming consumers (Observers) was a difficult task. There are quite a few operators that were invented to deal with producers pushing too many events, and most of them are quite interesting on their own. Some are useful for batching events; others are dropping some events. This section walks you through these operators, including some examples.

Taking Periodic Samples and Throttling

There are cases for which you definitely want to receive and process every single event pushed from the upstream Observable. But, there are some scenarios for which periodic sampling ...

Get Reactive Programming with RxJava now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.