Introduction to reactive programming

If we want a view of reactive programming from 50,000 above ground level, it can briefly be termed as:

When input x in any function changes, output y automatically updates in the corresponding response without the need to manually invoke it. In short, the sole purpose is to continuously respond to external inputs whenever prompted by output worlds.

Reactive programming is achieved through utilities such as map, filter, reduce, subscribe, unsubscribe, streams. Reactive programming focuses more on events and message-driven patterns rather than manually fiddling with huge implementation details.

Let's take a practical day-to-day example to understand reactive programming. We all have used Excel since the beginning of our IT lives. Now, let's say you write one formula based on a cell value. Now, whenever the cell value is changed, all corresponding results based on that value will reflect the change automatically. That's called being reactive.

Briefly understanding reactive programming when combined to deal with various data flows, the reactive programming can be advanced data flows with the ability to handle the following things:

  • Event streams, streams we can tap into and subscribe, and then use subscription output as a data source.
  • Having streams gives us the ability to manipulate streams, create new streams from original ones, and apply transformations as and when needed.
  • Transformations should work independently in a distributed system. A specific transformation can be the merge of several streams received from various places.

Functional reactive programming is the variant of reactive programming that we are going to use. Briefly stated, our functional reactive microservice should have the following two fundamental properties:

  • Signifying or denotative: Each function, service, or type is precise, simple, single, responsible, and implementation-independent.
  • Continuous time: Programming should keep in mind time-varying values. Variables in functional reactive programming have a value for a very short time. It should provide us transformation flexibility, efficiency, modularity, single responsibility.

The following are the characteristics of functional reactive programming:

  • Dynamic: Knows how to react to time or to handle various input changes
  • Handle time variations: When reacting values change continuously, handle appropriate changes
  • Efficient: When there is a change in input value, have a minimum amount of processing as and when required
  • Aware of historic transitions: Maintain state changes locally and not globally

Now that we briefly know about reactive programming, let's look into what advantages we get while adopting reactive programming. The next section talks about and gives us very strong reasons for why you should drop everything and start reactive programming.