Reactive Programming (Part 1)

Yiğitcan Nalcı
Javarevisited
Published in
3 min readDec 22, 2021

--

Hi everyone, there was a big change in my life when I couldn’t write. I have a daughter 👨‍👩‍👧, I mean, I am a father now 😎. It took some time to adapt new life as a family, but we are all happy right now 👨‍👩‍👧 and I have a time to write something good 😁.

So getting back to my story, Reactive Programming is one of the most popular paradigm recently.

Its popularity comes from non-blocking I/O concept. This concept describes that while a code block is running, it doesn’t block any other threads. In short, two processes can work without blocking each other.

Let’s dig in! 🦽🦼

Photo by Abbas Tehrani on Unsplash

What is Reactive Programming?

An API for asynchronous programming with observable streams.

Our programs developed according to Object Oriented Programming(OOP) follow the flow of lines of code. Therefore, they are insufficient for working with side effects.

At this point, we need something else that capable of working with these side effects. Here, we call applications or code snippets that are designed to respond to these side effects as Reactive.

Photo by Chris Ried on Unsplash

Reactive programs follow events instead of the flow of lines of code while the application is running.

Reactive systems must have the following features:

  • Responsive: A reactive system gives a response for all requests in a fair time.
  • Resilient: A reactive system still handles requests even if it crashes.
  • Elastic: A reactive system must be responsive under heavy loads.
  • Message Driven: A reactive system uses asynchronous message passing to interact.
Figure from https://www.reactivemanifesto.org/

As you see, two code samples are given below about blocking and non-blocking I/O concept.

// Blocking
alert(1);
var value = localStorage.getItem('foo');
alert(2);

The blocking sample’s output is 1,... 2 . Because localStorage blocks I/O while it is working.

// Non-blocking
alert(1);
fetch('example.com').then(() => alert(2));
alert(3);

On the other hand, the non-blocking I/O sample’s output is 1, 3,... 2 . It’s because fetch(...) does not block I/O while thread is running.

Why Reactive Programming?

As mentioned before, Reactive programs have an ability to answer the side effects. Also, you can handle more requests with a low number of threads.

If your application needs this ability, you should learn reactive programming.

Note: This tutorial gives you a brief introduction to reactive programming. We will develop a reactive mail service using Spring WebFlux in (Part 2). So, keep on reading 😄

--

--

Yiğitcan Nalcı
Javarevisited

Dad. Software Engineer. Sports lover, doer. Curious about science and technology. Novice writer for now. https://www.linkedin.com/in/yigitcannalci