The Essence of Reactive Programming in Java
May 04, 2020
Reactive programming (RP) is not something new and cutting-edge when it comes to application development. Chances are, you’ve already heard about it. The term was initially introduced to the IT world in the 1960-s and ever since much has been said and written in its regard. Unfortunately, as it often happens, the new concept quickly fueled a set of misinterpretations around itself, and still continues doing so today. The Reactive Manifesto of 2014 that introduced ‘reactive systems’ and their four ‘sacred principles’ messed everything up even more. So, let’s try to clarify what is what, and understand why and where we need reactive programming in Java application development (if we really do).
Reactive programming is a programming paradigm that deals with asynchronous data streams (sequences of events) and the specific propagation of change, which means it implements modifications to the execution environment (context) in a certain order.
Here’s a real-life example. Say, it’s Friday and John wants to spend this evening with his friend Bob, scarfing pizza and watching one of the Star Wars episodes. Let’s outline the options he has.
- John finishes his work. Then goes and orders the pizza, waits till it’s done. Then picks up his friend. And finally (with Bob and pizza) makes it home and gets down to the movie. It will be the sync approach and it will be way too long, so that probably John will have wanted to call the thing off by that time.
- John orders his pizza online, phones Bob, invites him to come. He heads home, has his pizza delivered and starts watching the movie (and eating the pizza) without waiting for Bob to show up. That is what can happen with the async approach.
- John orders pizza, phones Bob, invites him to come, heads home, and gets his pizza delivered. But this time, he waits until Bob comes and only after that he turns the movie on. This is what the reactive approach is about. You wait till all async actions (changes) are completed and then proceed with further actions.
No, they are not. Though often used interchangeably, the terms are not exactly synonymous and reflect different things.
Reactive systems represent the next level of ‘reactivity’. This level implies specific design and architectural decisions that allow building resilient, flexible, and responsive applications.
You don’t have to use reactive programming in reactive systems, but it’s a good idea to do so, as the combination brings even more benefits to your application, as make them even more loosely coupled, allow more efficient use of resources, make them more responsive and ensure lower latency.