Reactive Programming with Spring

In this post I will try to cover most of the key terminologies in reactive world, then I will move to how it works, foundational principles of reactive systems, how we can use reactive paradigm to build robust and responsive systems and much more. So, lets start..

Reactive Programming

Its a programming paradigm where the code you write is asynchronous and non-blocking and data flows as an event-driven stream(message-driven communication). It follows functional style coding very much like Java Streams API with the support of Backpressure.
Lets say your application needs some data from the database, it makes a request to the DB and DB returns immediately being non-blocking and asynchronous (DB is also reactive) and once the query results are ready DB sends the data or precisely streams the data as onNext(explained further) event to the application and after sending all the data DB says “hey that’s all I got” and sends onComplete event. During this flow your application is not blocked on IO operations and the application can perform any other task.
Just remember the phrase “React to the changes”

Got confused! what is onNext, onComplete, reactive stream, blah blah.. dont worry, this post is just to get you familiar with high level terminologies in reactive world, for now you can assume them as internal level details of reactive programming which I will cover later.

Reactive Systems

Its a set of design principles or architectural style for building modern systems allowing them to react to changes by scaling up or down, recover from failures making it resilient and hence building a highly responsive system. Its possible to build a part of the application using reactive approach but that doesn’t make the whole system reactive. What makes the system reactive are the traits defined under reactive manifesto. Reactive systems must be responsive under all conditions.

Event-driven vs. message-driven

I have mentioned above that reactive programming is event-driven but communication between components is message driven. Now what does that mean? To put things simply messages have a fixed destination while events are the signals for someone listening to consume. Reactive programming follows event-driven approach while reactive systems focuses on message-driven approach to communicate b/w components across network.

Reactive Manifesto

For the programmers who started learning reactive programming, Reactive manifesto is like a bible to them that underpins the principles of reactive programming. This document was released back in 2013 and was updated later, so far signed by 30743 people.

Principles of reactive manifesto:
1) Responsive
(System responds in a timely manner)
2) Resilient (Stay responsive under failure as well)
3) Elastic (Staying responsive under varying workload)
4) Message Driven (Relying on asynchronous message passing)

Understanding Reactive Manifesto

How the system will stay responsive under varying workload, or precisely how should it react to the changes in load, one way is through elasticity, that means throughput of the system should increase if we have more hit on system and it should decrease automatically when load reduces.
By adding more computational resources and instances, the system can stay responsive without affecting the average latency.

We can achieve elasticity by employing scalability, be it horizontal or vertical. Building a scalable distributed system is a challenge in itself, adding an extra layer of having responsiveness is another headache. So to make the system stay responsive under failures (or to be resilient), we can have isolation between functional components of the system i.e. failure in one component doesn’t effect the flow of other component thereby isolating all internal failures and enabling independence. For example, E-com sites have many functional components like list items, feedback service, order list, payment service, etc. Payment service outage should not let its customer order to reject, the application can accept the order and can schedule an auto retry payment, and having no effect on user of the failure.

Elasticity and resilience are tightly coupled and we achieve a truly responsive system only by enabling both. Scalability helps us to have multiple replicas of the component, so that if one fails, we can mitigate the failure by switching to other replicas, minimizing the impact on rest of the system.

Now to connect components in the distributed system and preserve decoupling, isolation and scalability at the same time, we can use message-driven communication principle, each element(component) awaits the arrival of messages and reacts to them, otherwise lying dormant, or that component itself can send the message in non-blocking way. One of the ways to achieve message-driven communication is by employing message broker, where by monitoring the message queue, system can do load management and be elastic.
Finally, one of the fundamental way to attain responsive, elastic and resilient system is by employing message-driven communication.

Reactive Streams

Its an specification created by people from multiple organizations like Netflix, Pivotal, Lightbend and many others. Basically it gives a set of APIs for reactive programming intended to be implemented by reactive libraries built for the JVM.

It has four components:
1) Publisher(Data source/Data producer)
2) Subscriber(Represents Consumer)
3) Subscription(control emission of items, backpressure support)
4) Processor(transform incoming items then pass to another subscriber)

I will cover the APIs mentioned in the specification in greater details in another post.

Reactive Streams Implementations for Java

Following are the implementation of reactive streams specification:

In this series, I will focus on project reactor, its availability in Spring Framework 5 which includes Spring Webflux that provides reactive programming support for web application.

Project reactor is one of the implementation of reactive streams API, and Spring Webflux internally uses project reactor for building web applications using reactive paradigm.