What is Reactive Programming ?

Pragmatic Code School
A free video tutorial from Pragmatic Code School
Technology Enthusiast, Online Instructor
4.5 instructor rating • 7 courses • 35,791 students

Lecture description

In this tutorial I will explain the concepts that form the foundation of reactive programming.

Learn more from the full course

Build Reactive RESTFUL APIs using Spring Boot/WebFlux

Learn to write reactive programming in Spring using WebFlux/Reactor and build Reactive RESTFUL APIs.

09:38:13 of on-demand video • Updated April 2021

  • What problems Reactive Programming is trying to solve ?
  • What is Reactive Programming?
  • Reactive Programming using Project Reactor
  • Learn to Write Reactive programming code with DB
  • Learn to Write Reactive Programming with Spring
  • Build a Reactive API from Scratch
  • Learn to build Non-Blocking clients using WebClient
  • Write end to end Automated test cases using JUNIT for the Reactive API
English [Auto] Hi everyone. Welcome back to this tutorial industrial. I'll give you all an introduction to reactive programming and the concepts that form the foundation of reactive programming. Quick note here this section is going to cover purely about waters reactive programming in general and how it works. I'm going to save the discussion a bold reactive API is for the future part of the course as you need the reactive Programming Fundamentals in order to build the reactive API. So what does reactive programming. This is a new programming paradigm and it is going to be a complete shift from the way we have been building things in Java. The definition as per the documentation is that this programming style is fundamentally asynchronous and non blocking and data is going to flow as an event driven stream. Don't worry if you're not able to wrap your head around what it means you just got to wait a little bit more. I'll be discussing about these concepts in detail and a reactive programming provides functional style API. Just like the stream API that got introduced as part of Java 8 and it also supports back pressure on the event driven data streams. We touched on this topic a bit on one of the previous video. We will discuss more in the future part of this course. In this light let's understand how the reactive programming works. Using a simple example here I have two friends texting each other friend 1 and Friend 2 for the sake of this example let's assume friend 1 is watching TV and Friend 2 is busy outside with his family for dinner. Friend one decides to text a friend too. In reality after the message is sent a friend one is not going to wait and block until the response is received from this friend. As per the example here after the Hey what's up messages sent from friend 1 basically friend one is going to continue watching TV. This kind of communication is called asynchronous and non blocking the ideas and not to wait and block on anything. No friend 2 came home from his dinner after sometime and gets a chance to respond to a friend one with a message just came back from dinner and they continue the conversation in a similar fashion which is asynchronous and non blocking. Once they are done with the conversation they're going to do what they were planning to do. In addition to the asynchronous and non blocking aspect this kind of communication is called a message are event driven communication. So in a nutshell this is how reactive programming works. Now let's get into the detail of how the data is going to flow as an event driven stream. In reality let's take a look at an example. Before we get into the reactive programming part let's quickly touch base on how does the data is going to flow in that imperative. Will the code here uses GOP makes a call to the database to retrieve the data. In reality the left side represents the app and the right side represents the database fast. We make the call to the database to get the data and then basically the app waits before it gets the data from the database. It goes into the blocked and waiting state and in this example we are retrieving the list of items from the database so we will be blocked until the complete list of items are retrieved. This is the way we have been calling for many many years in Java this programming model is also called a synchronous and blocking communication model. How does this work in the reactive programming world in reactive programming world. As I mentioned before the data is going to flow as an event are message stream. A quick note here of I say even our message board refers to the same thing. So basically the data is going to flow as an event stream. So we are going to receive one event for every resource item from their data source the data source could be a database or an external service or an external file or etc. Once all the data is vented out to the caller then we are going to receive a completion even if there is an error in retrieving the data from the actionable source. Then we are going to receive an error even I understand this concept is totally new and it is really hard to get your head around this concept. Now let's take a look at the same concept that I explained using a floor diagram that will give you a good understanding of what do we mean by event driven stream. The query here is going to retrieve a list of items from the database the real source that I have here in this example is a database so it could be anything. Basically it could be actionable servers or a file or anything like that. So the first step is we are making the call to retrieve the list of items as I mentioned in the example the call returns immediately. The reason being this is asynchronous and non blocking the interesting behavior is not the data will be pushed to the application as a stream and as it is available. One by one meaning one even for every single item and its own set. As you see here we receive on next call from the database layer to the application layer for every single item and this will be continued until all the items are pushed to the application and this data flow is called event driven stream. Basically or not being blocked and the data is pushed to the application. Now once all that it is pushed we receive an even card on complete that means hey I have pushed all the items it is in on completion even which tells you I don't have any more data. Don't think too much about the internal details of how it's going to work. I'll be covering all those concepts in the future tutorial. For now let's focus on how the flow works in general. What I showed you here is a happy path. Let's talk about the unhappy part where some kind of error after the retrieval we make the call to the database as like the previous example and the call returns immediately after the first result item after the first on next call which gave you the first item because of some reason we got an exception. In that case instead of abruptly exiting the execution floor which is how the imperative programming still works here it is going to send you an error event to let the application know something unexpected happened. So after this even was received we can take it off. Handling how you want to recover from that exception. Now let's talk about the noted of law. There for a given search Corey there is no result then in that case we just get the aren't complete even from the database layer. Now let's talk about the safe floor where the return type is wired. The example here makes a call to the database to see if the item in those kind of scenarios if the save is successful they get it on complete even are if the service unsuccessful for some reason. Then we get an on error. Even so this is how the data flows in the reactor programming world. So in a nutshell there are three things when it comes to data communication and reactive programming. No one is on next for a data stream evens Number 2 is on complete for data completion. Even number 3 is on error for error. Even so fundamentally this is what we need to know when it comes to react to programming. We will be exploring all these in code in the future part of the course. Now let's take a look at the next aspect of reactive programming which is the functional style called reactive programming embraces the concept of functional style code. If we have worked with Java 8 streams API then you might be able to relate what I'm talking about the code that we write and reactive programming makes it easy to work with lambdas. Here is an example of the code that we will be writing in the future part of the course. The example that we have here is a put end point that talks to the DP and perform some kind of transformation operations and returns a response to the client. Don't worry too much about the code at the moment. We will be building the reactor non blocking restful endpoints in the future part of the course. Let's talk about the next aspect of the reactive programming which is the back pressure reactive programming supports back pressure. If the data source is producing more data than we expected then there is a way for the application to provide a feedback to the data source stating you need to slow down and play catch up. This option is handy when it comes to build stable systems using reactive programming. So we talked about the different aspects that form the foundation of reactive programming. We will dig into the details of how all of these concepts can be implemented using code in the following tutorials but thus we came to the end of this material. Thank you for watching.