Skip to main content

Reactive Streams

Reactive Streams is a specification for asynchronous stream processing.

reactive-streams-01.png

Reactive Streams API

The purpose of Reactive Streams is to provide a standard for asynchronous stream processing with non-blocking backpressure.

Handling streams of data—especially “live” data whose volume is not predetermined—requires special care in an asynchronous system. The most prominent issue is that resource consumption needs to be carefully controlled such that a fast data source does not overwhelm the stream destination. Asynchrony is needed in order to enable the parallel use of computing resources, on collaborating network hosts or multiple CPU cores within a single machine.

The main goal of Reactive Streams is to govern the exchange of stream data across an asynchronous boundary – think passing elements on to another thread or thread-pool — while ensuring that the receiving side is not forced to buffer arbitrary amounts of data. In other words, backpressure is an integral part of this model in order to allow the queues which mediate between threads to be bounded. The benefits of asynchronous processing would be negated if the backpressure signals were synchronous (see also the Reactive Manifesto), therefore care has been taken to mandate fully non-blocking and asynchronous behavior of all aspects of a Reactive Streams implementation.

It is the intention of this specification to allow the creation of many conforming implementations, which by virtue of abiding by the rules will be able to interoperate smoothly, preserving the aforementioned benefits and characteristics across the whole processing graph of a stream application.

It should be noted that the precise nature of stream manipulations (transformation, splitting, merging, etc.) is not covered by this specification. Reactive Streams are only concerned with mediating the stream of data between different API Components. In their development care has been taken to ensure that all basic ways of combining streams can be expressed.

In summary, Reactive Streams is a standard and specification for Stream-oriented libraries for the JVM that

  • process a potentially unbounded number of elements
  • in sequence,
  • asynchronously passing elements between components,
  • with mandatory non-blocking backpressure.

API Components

Package org.reactivestreams:

  • Publisher: is a provider of a potentially unbounded number of sequenced elements, publishing them according to the demand received from its Subscriber(s).
  • Subscriber: can serve multiple Subscribers subscribed subscribe(Subscriber) dynamically at various points in time.
  • Subscription: represents a one-to-one lifecycle of a Subscriber subscribing to a Publisher. It can only be used once by a single Subscriber. It is used to both signal desire for data and cancel demand (and allow resource cleanup).
  • Processor: represents a processing stage—which is both a Subscriber and a Publisher and obeys the contracts of both.

JDK9 java.util.concurrent.Flow

The interfaces available in JDK 9 >= java.util.concurrent.Flow, are 1:1 semantically equivalent to their respective Reactive Streams counterparts.

Read this if you are interested in learning more about Reactive Streams for the JVM.

Implementations

reactive-streams-jvm

Project Reactor

RxJava

Akka-Streams