Saturday, April 8, 2017

Why Go Reactive?

Reactive Programming


Computer Programming is the process of creating instruction that can tell a computer to execute some tasks. Basically we can identify three types of computer programs in modern world. Namely Transformational Programs, Interactive Programs and Reactive Programs. Transformational Programs are pretty simple. They take set of inputs and produce an output. Interactive is bit different which interacts with users and other programs in its own speed and produce outputs. Reactive is also interacting with users and produces outputs, this time around not in its speed but in the speed of the user. 

Reactive Programming is where you implement computer programs to be reactive. It is an Asynchronous Programming Paradigm which is oriented around Data Streams. Data Stream is like a pipe where some service inputs the data from one end and another service receives data in the other end. And most importantly Reactive also means that it is event-driven. In Reactive Programming the program reacts to any occurred event and fetches the relevant data when data is ready in the data-stream. 

For an example think about the expression [ a = b + c]. In Imperative Programming to take the value of [ a ] we have to recalculate the mentioned expression again and again whenever the values of [ b ] and [ c ] are changed.  But in Reactive whenever the values of [ b ] and [ c ] changes the value of [ a ] will be automatically update without executing the expression. Whenever the change-event for [ b ] or [ c ] occurs the [ a ] which is listening to the data-stream then will automatically update it-self.

Reactive Principles


There are four major principles to be applied when programming re-actively. More than principles we can think of these are constraints on Reactive Realm.
  • Responsive
    • Reactive mean responding to interactions. The program has to respond to user interactions in the speed of user.
  • Resilient
    • Program should be able to maintain and run the service with the occurence of faults.
  • Scalable
    • The service should be able to scale-up and scale-out with maintainig the responsiveness.
  • Event-Driven
    • Reactive is always works whenever event occurs. Program should listens to interactions and works accordingly.

Push or Pull


In programming, there are three mechanisms to take inputs. Push, Pull or Pull-and-Push. Pulling is when the program needs the data it waits until data is ready. Pushing is program executes asynchronously and fetch the data whenever data is ready. Hence Reactive Programming uses data-streams it uses 'Pulling' to fetch data. 

More on Data Streams


Data-Streams are extremely powerful. They are asynchronous. You can execute expressions on data when they are in a data-stream. Executing expressions or functions on data-streams means you can shape the data into the shape which applications needs it to be. Linux pipe-lining is a good example for a data-stream where you inject output from one function into another. And Java-Streaming API is another good example. 

Should or Should Not ?   

There are some good frameworks for Reactive Programming. ReactiveX has produced Reactive Extensions for major programming languages. React is another good Reactive JavaScript Framework. 

Even-though lot of programming paradigms available we should use them very carefully only in accordance with the requirement. So, it is not good to go 'reactive' every where. But if the requirement is event-driven, responsive program then it is better to use 'reactive'.  Please refer to this blog post and ReactiveX home page for more details.


   

3 comments: