Introduction to Reactive Programming

Reactive Programming is a new declarative paradigm. It makes it possible to work with finite or infinite data flows asynchronously, allowing it to propagate generating changes in the application. As it is indicated by the title, it ‘reacts’ to the data by executing a series of events.

Reactive programming is related to the Observer design pattern: when there is a change of state in an object, the other objects are notified and updated accordingly. Therefore, instead of probing events for changes, events are performed asynchronously so that observers can process them.

The idea behind using asynchronous programming is simple: to reduce the inefficient use of resources. The way that is achieved is simple – using resources that would otherwise be inactive, for they would be waiting for the activity of a particular component. With reactive programming new data notifications are sent to customers without them having to make a request, as data entry and exit are asynchronous. Therefore, the normal design of the input and output processing is reversed. This approach frees the client to do other things while waiting for new notifications.

Its conception and evolution have been linked to the publication of the Reactive Manifesto, which established the basis of reactive systems, including four core principles:

  • Responsive: they ensure the quality of the service by complying with established response times.
  • Resilient: they remain responsive even when faced with error situations.
  • Elastic: they remain responsive even when the workload is increased.
  • Message-driven: they minimize the coupling between components by establishing interactions based on the exchange of messages asynchronously.

You may wonder about the key motive to develop this paradigm. It stems from the need to respond to scaling limitations that are present in prevalent development models. These scaling limitations cause wasted use of the CPU due to I/O, memory overuse (large thread pools) and inefficiency of blocking interactions. 

How to apply reactive programming?

Now comes the part that interests us most. How do we apply reactive programming, where do we incorporate it in our technologies?

For those who use Java, the two libraries recommended by the community are:

RxJava: this library and its version 1.x were the pioneers in Java reactive development, (currently they are going for version 3). They are fully integrated into frameworks such as Spring MVC, Spring Cloud, and Netflix OSS. 

Project Reactor: It was conceived with the involvement of the team responsible for RxJava 2, so they share much of the architectural base. Its main advantage is that being part of Pivotal, it has been chosen as the foundation of the future Spring 5 WebFlux Framework. This API introduces the Flux and Mono types as Publisher implementations, which generate a series of 0… N and 0… 1 element respectively.

For those who use Javascript frameworks, these are our recommendations:

React + redux-saga: it is a reactive framework in itself, and it is managed by integrating a global state to the application which is updated through side effects.

NgRx: is a framework for building reactive applications in Angular. It provides state management to create explicit maintainable applications, storing a single state and the use of actions to express state changes, in addition to side effects isolation, entity collection management, and router links. 

Benefits of reactive programming

These are some of the main benefits of applying this type of programming:

Scalability: By using reactive programming we obtain a weakly coupled implementation that is scalable and tends to isolate errors. Scalability refers to the ability to scale horizontally and quickly. For those who don’t know, a system scales horizontally when its performance improves with the addition of nodes. For example, adding a new computer to a system that balances the load between the old and the new can improve the performance of the entire system. This also applies to programming, either with services on the backend side or modules and/or components on the frontend side.

Savings: The efficient use of resources, allows us to spend less on servers and data centers. One of the main premises of reactive programming is that more can be done with less. Specifically, you can process higher workloads with fewer threads.

With the rise of web applications that deal with large amounts of data, these became increasingly complex. In many cases, they demand visualization of data from many different sources. The data has to be updated on a recurring basis or in real-time. This is why reactive programming has become increasingly popular in recent years. It facilitates and optimizes the management of these types of data flows.

If you have any questions about reactive programming and/or think you may find it useful in some of your projects, don’t hesitate to contact us.

on November 14, 2019