What is Reactive Programming?

Hello everyone. In this article, let us see about the Reactive Programming paradigm and understand all the Jargon surrounding Reactive Programming.

What is Reactive Programming?

Most of the real-world applications are blocking in nature i.e they use blocking or synchronous calls. In the above diagram under the Blocking Request model, you can see that the server (Servlet Container) creates a new thread for every incoming request and all of them are waiting for the response from the database and hence blocked. This will consume a lot of resources and the new requests will be stalled bringing the entire application to a standstill.

In use cases like processing of big data, this kind of architecture will not keep up with the speed and agility required and hence the need for an asynchronous and non-blocking request model emerged.

Reactive programming is a programming paradigm that makes use of non-blocking, asynchronous, and event-driven or message-driven data processing. The data and events in this architecture are modeled as streams. This model requires a small number of threads to scale vertically and this is achieved by the NIO EventLoop

As seen in the above diagram under Non-Blocking Request model, all the incoming requests are mapped to the event handler and call back. The incoming requests are delegated to the thread pool that manages a limited number of threads. Upon availability, one of the threads in the thread pool would process the request by delegating it to the handler function inside the thread pool.

Once the handler function completes the processing, any available thread from the thread pool retrieves the response and sends it to the callback function. Then the callback provides the response to the request. As you can see, none of the threads will go into waiting state in the non-blocking model. This boosts the performance of the application using the minimal resources

A single request-response could be processed by multiple threads and hence the requests are decoupled from the threads when compared to the coupled nature in the thread-per-request in the Blocking Request Model.

Components of Reactive Programming

In Reactive Programming, also called Rx programming, data is emitted by one component, and RX library/framework will send the changes to another component that is registered to receive those changes. There are three components in the Reactive Programming as follows

RX = OBSERVABLE + OBSERVER + SCHEDULERS

  • Observable: The Observable is nothing but a Supplier that supplies the data to other components
  • Observers: Observers consumes the data stream emitted by the observable. Observers subscribe to the observable to receive the data emitted by the observable.
  • Schedulers: As seen above, Rx is for asynchronous / non-blocking programming, and hence it needs a mechanism to manage threads and this is where the schedulers are needed. Schedulers tell the observable and observers on which thread they should run.

Reactive Manifesto

The Reactive Manifesto is a manifesto signed by the authors to come up with an approach to building reactive systems architecture. It states that the reactive system must be asynchronous software that should have the following characteristics.

  • Responsive: A Reactive system must be quick and respond in a timely manner to provide a consistent and high-quality service.
  • Resilient: A Reactive system should be responsive during failure by adapting replication and isolation principles.
  • Elastic: A Reactive system must be responsive even under high load which can be achieved using shards or by replicating components in its infrastructure.
  • Message-driven: A Reactive system must use asynchronous messaging across the components to be loosely-coupled

What is a Reactive Application / System ?

After going through what is Reactive Programming and Reactive Manifesto, the next question that comes to our mind would be What is a Reactive Application or a System? Yes, you will get your answers in this section

Reactive systems โ€” as defined by the Reactive Manifesto โ€” are a set of architectural design principles for building modern systems that are well prepared to meet the increasing demands that applications face today.

So a Reactive System / Application must have the following

  • All the components of the system are built with Reactive Components. I.e From the Web (Java Script) to Server to Database. Everything should be built using Reactive components
  • Should be developed using Reactive Programming (RX)
  • Should be organized within Reactive Architecture
  • Should follow the Reactive Manifesto
  • Follow the below principles

Reactive vs Functional Programming?

Most of us would be doing Functional Programming in different languages and now many organizations are moving towards Reactive Programming and it is a good time to understand the differences between the both.

Functional programming is the principle of building software using the following concepts

  • Pure Function
  • Side Effects
  • Shared State
  • Immutable Data
  • Function Composition

On the other side, reactive programming is an asynchronous programming paradigm concerned with data streams and the propagation of change.

Functional Reactive Programming is the combination of both Functional and Reactive programming to get the benefit of both worlds. It uses the asynchronous dataflow programming on top of the functional programming that uses the map, reduce, filter, etc.

Spring Web Flux with Spring Boot is one example of Functional Reactive Programming

What is ReactiveX?

ReactiveX is an API for Asynchronous Programming with observable streams.

ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming. It abstracts away concerns about things like low-level threading, synchronization, thread safety, concurrent data structures, and non-blocking I/O.

Reactive Revolution is a breakthrough in programming and it has inspired many other APIs, frameworks, and programming languages across many layers

Languages that adopted ReactiveX (RX)

  • Java: RxJava
  • JavaScript: RxJS
  • C#: Rx.NET
  • C#(Unity): UniRx
  • Scala: RxScala
  • Clojure: RxClojure
  • C++: RxCpp
  • Lua: RxLua
  • Ruby: Rx.rb
  • Python: RxPY
  • Go: RxGo
  • Groovy: RxGroovy
  • JRuby: RxJRuby
  • Kotlin: RxKotlin
  • Swift: RxSwift
  • PHP: RxPHP
  • Elixir: reaxive
  • Dart: RxDart

ReactiveX for platforms and frameworks

  • RxNetty
  • RxAndroid
  • RxCocoa

Benefits of Reactive Programming

  • It provides faster response time and low latency by making better of the system resources
  • It makes the system resilient to failures
  • It enables consistency in software design and enables faster development time
  • Low maintenance costs and effort

In this article, we saw what is Reactive Programming in-depth and went through all the Jargon used. We also saw that there are so many languages and frameworks that use RX programming, and hence it’s a good time to learn Reactive Programming as it is a highly sought-after skill.

Thanks for reading this and stay tuned!!!!.

I work as a freelance Architect at Ontoborn, who are experts in putting together a team needed for building your product. This article was originally published on my personal blog.