Why do you need reactive programming? What problems is it trying to solve? What are the benefits?
- [Instructor] Hi, welcome to the fourth video of this section, benefits of reactive programming. In the previous video, we learned what reactive programming is. Now it's time to know why we need it. What are the benefits, how we can get them? All those benefits will make more sense once we explain the push versus pull mechanism. That's going to allow us converting any imperative synchronously logic to our reactive asynchronous one. Let's just start with a why. Internet has changed a lot from what we had in the past.
10 years ago, we had self-sufficient apps that we didn't distribute. Even some of them caused some problems with concurrency. All of those problems could be solved using different threads and locks. But anyway, for example, the interaction with webpages was basically about submitting a long form to the back-end and then performing simple rendering in the front-end. Now it's different. The benefit is more evident in modern web apps and mobile apps. They're highly interactive with a multitude of UI events.
Nowadays, apps have an abundance of realtime events of every kind that enable a highly interactive experience to the user. We also want independent services in a Cloud environment. We have to take internet of things into account with realtime applications that would lead us to distributed apps. So we need tools to deal with that properly and reactive programming is an answer. Reactive programming is going to help us solve those problems and reach the current expectations. System built as reactive systems are more flexible, loosely coupled and scalable.
This makes them easier to develop and amenable to change. They are more tolerant of failure, and when failure does occur, they made it resilient (mumbles) disaster. They're also highly responsive, giving users effective interactive feedback and using resources in an efficient way. How does it happen? With imperative logic, latency is going to be there, we are forced to deal with asynchronicity with (mumbles) scalability problems. There is another way (mumbles) is reactive.
As we will see in the next video, reactive is fundamentally asynchronous and non-blocking. It's going to use very few threads in a low-level way which is going to bring safety to the table. The usage of concurrent data structures makes everything even more efficient. As we will be able to appreciate as we go through the course, it's going to help you thinking about asynchronous program intrinsically. So they will achieve all those expectations out of the box. It's going to make the flow of your application easier to understand.
And we can achieve that with lambdas. Lambdas are introduced in the second section of this course. It also improves the standard error handling with a better data structure, you will have less code which will translate to less bugs. With the imperative model, we pull and process things synchronously using iterators. At the core of reactive programming, you take that pull model and convert into a push one. How can we do that? Well, because we have a stream of events, a collection of events, we take the pull model and we push the same things that we use to have in the collection.
We push them asynchronously into the other end of the stream. That would do it. In this way, we can convert any imperatives synchronously logic to our reactive asynchronous one. That's it for this video. I hope you get the benefits of reactive programming. As you can see, we can convert our old application to a reactive one. That's great news. Okay, I more or less understand what reactive programming is and what are the benefits but can you talk more about it? Can you show me how it works and the principles behind it? Sure.
That's exactly what we are going to talk about in the next video. Reactive explained in a simple way. I'll see you there.
This course was created and produced by Packt Publishing. We are honored to host this training in our library.
- What is reactive programming?
- Java 9 reactive features
- Creating and observing sources with RxJava
- Unit testing
- Akka streams in a reactive environment
- Building a sample reactive application