What is Reactive Functional Programming?

Puneet Sapra
The Mighty Programmer
4 min readDec 1, 2020

Reactive Functional Programming, sometimes known as Reactive Programming, is a popular paradigm¹. It is applying Functional Programming techniques to manipulate streams of data.

Writing code in “stateless functions” is what Functional Programming is all about. A stateless function takes in data and outputs it without changing the global state. A processing stage can be built by chaining together functions.

Thus, Reactive Programming is applying “stateless processing” on streams of elements.

Concepts

Publisher, Subscriber, Element and Operators are most common concepts of Reactive Functional Programming.

Element

An element is an “entity of interest” for processing. It might be as complex as a Business Object or as basic as an integer.

Consider a case modeling:
A company wants to know the most performant Employee.
— To respond to the inquiry, you must process each employee
→ You can consider building a stream of employees and processing each employee’s information. “Elements” are represented by the “Employee” entity.

Publisher

A Publisher is a source of elements. A publisher can source an unbounded or bounded (finite) stream of elements. A publisher can generate elements or can be plugged into a compatible data source like Web API, Messaging Queue, In-memory collection, or, Database.

Considering the previous discussed performant employee case:
Publisher can be connected to Employee data source to “stream employee data”.

Operators

An Operator is a Function that is applied to each passing element. An operator can compose/chain another operator; thus Multiples operators can be chained together to make a robust processing pipeline.

Operators are the “Functional Programming aspects” in the context of Reactive Programming.

Map, Limit, and Filter are commonly available operators in most Reactive libraries.

Considering the previous discussed performant employee case:
You can introduce an operator to calculate “the performance score” of each employee.

Subscriber

A subscriber consumes elements. It may do one of the following

  • Collects all incoming elements and turned them into a collection
  • Streams the elements to Messaging Queue or Socket stream.
  • Simply returns a result after processing all elements for a finite stream.

Sink, Reduce, and Collect are common types of subscribers in most Reactive libraries.

A subscriber can be considered as “the last operator” in the pipeline.

Considering the previous discussed performant employee case:
You can reduce (“aggregate operator”) employee stream to find the max of “performance” thus finding the performant employee.

Design Aspects

Asynchronous

Asynchronous means processing “later in time.” Reactive Programming facilitates “asynchronous processing” in many ways:

  • Streams: Streams are “asynchronous”. There is always a time gap between publisher and subscriber handling. Subscribers can be viewed as “callback” called at end of the processing pipeline.
  • Schedulers: Most Reactive frameworks allow publishing, operations, and subscribing on different threads or thread groups. Threads introduce “asynchronous behavior” when tasks are submitted to them since there is always a time gap between task submission and task completion.
  • Event Loop: Most Reactive frameworks utilize Event Loop which makes them efficient for IO oriented tasks like reading or writing to Web API or DB. Event Loop is “asynchronous” by design.

Observer Design Pattern

Reactive Programming is an extended variant of “Observer Design Pattern”. Subscribers are the “Observers” and Publisher are the “Subjects”. Each element that arrived in the stream is “notified” to the subscriber. In between publisher and subscriber, there are processing stages.

Chain of Responsibility Pattern

Reactive programming is an elegant Chain of Responsibility pattern. A passing element is a “request” and operators are the “handlers” acting on passing requests. Operators are convoluted to form a “chain of handlers”.

Back Pressure

Backpressure is a mechanism to gauge and control the speed of “flow of elements.”

Unlike the usual PubSub, in Reactive programming, Subscribers communicate with Publishers.

A Subscriber can tell Publisher to send elements according to its handling capacity. It can ask for speed up or turn down the traffic coming to it.

What’s next?

Explore the reactivity in your favorite language/s:

  • Java Flow, RxJava, Project Reactor for Java Ecosystem
  • Combine, RxSwift for Apple Ecosystem
  • RxJs for Javascript Ecosystem
  • Rx.Net for .Net Ecosystem
  • RxRuby for Ruby
  • RxPy for Python

Footnotes

  1. A programming paradigm is a “conceptual view” or “ideology” of the “way of programming.”

--

--