Backpressure explained

Backpressure is something every software engineer will have to deal with at some point. You might read it and have no clue what it really means. For those people, continue reading 😉

In software you generally speak of having input and turn it into desired output. Backpressure is when the progress of turning that input to output is resisted in some way. Most common resistance is computational speed but there are other examples too like UI rendering and writing files.

Example of backpressure

Drake & Josh

In an episode of Drake & Josh they work together at a sushi factory. Their job is to take sushi from a conveyor belt and place each of them in a plastic tray. Easy enough, except they quickly find out the conveyor’s speed is faster than they can handle.

This is a perfect example of backpressure. They tried different ways of dealing with the issue. #1 Setting some aside to get to it later, this is called buffering. #2 Eating and hiding it away, this is called dropping. However, in the case of a sushi factory, neither of these backpressure strategies are viable.

Twitter API

Consider the example of the Twitter API, which is constantly emitting tweets as they are added to the twitter platform. The Twitter API is probably sending the requests faster then your service can process them. Therefor it will buffer the requests and eventually run out of memory.

Keyboard

A prime example is the users input via the keyboard. If your code listens to the keyboard input and fire network requests for example then I you might end up with the same problem.

Typing GIFs - Get the best GIF on GIPHY

Strategies

Aside from scaling up your available compute resources, how you handle backpressure is dependent on the platform. Since I’m an Android Developer I prefer to use RxKotlin. Rx has a couple of predefined ways of handeling backpressure and you can find them here.

  • BUFFER – Buffers all onNext values until the downstream consumes it.
  • DROP – Drops the most recent onNext value if the downstream can’t keep up.
  • ERROR – Signals a MissingBackpressureException in case the downstream can’t keep up.
  • LATEST – Keeps only the latest onNext value, overwriting any previous value if the downstream can’t keep up.
  • MISSING – OnNext events are written without any buffering or dropping.

Let me show you how easy it is to add back pressure to your current code.

val observable = Observable.create<String> { 
emitter -> ... 
}.toFlowable(BackpressureStrategy.DROP)

and that’s it. Convert your observable to a Flowable and you are done. Hope this was helpful and otherwise leave a comment below 🙂

Add a Comment

Your email address will not be published.