Reactive Programming
- What Programming paradigm does this follow?
- What did people use before using reactive programming?
- Problem statement
- What is reactive programming?
- Benefits
- Similarities and Differences between java8 stream API and reactive programming
- Is this an alternative to virtual threads?
- Reading material
- Tags
- Reactive Programming - Hot and Cold publishers
What Programming paradigm does this follow?
This follows Declarative Programming
What did people use before using reactive programming?
- They used
Imperative Programming
- C - Prodedural Programming Language
- Unit of reusability is function
- Java - Object Oriented Programming Language
- Unit of reusability is Class
- Solves some of the short-comings of C
- Data and Methods are together
- List/Set/Map…
- But we do NOT have tools to simplify the non-blocking async IO calls.
- Solves some of the short-comings of traditional programming languages
How things used to work in traditional way?
- Usually, the backend application is hosted in a webserver.
- When a request comes in, it will be assigned to a particular thread by the webserver, and this thread will be occupied with that request until it finishes the process. This process may be calling a database or a 3rd party API which takes time to complete.
- If another request comes in, that request will be assigned to another available thread and that thread will also be busy until the process is completed.
- There is a maximum thread count for a particular web server and that count may be based on the web server.
- If we use spring-boot, the webserver container will be tomcat and that container will have 200 maximum threads.
Drawbacks in the traditional way
- Once a thread is assigned to a request that thread won’t be available until that request finishes its process.
- If all the threads are occupied, the next requests that come into the server will have to wait until at least one thread frees up.
- When all the threads are busy, performance will be degraded because memory is being used by all the threads.
Problem statement
Managing real-time updates to content.
- The client applications cannot keep asking the server for updates. Instead, the server needs to notify the clients when there is a change in state. This needs to be done using a stream of responses from the server to the client.
- The client applications cannot keep notifying the server about things like the user’s browser history, mobile devices, updates from wearable devices, etc. This needs to be done using a stream of requests from the client to the server.
What is reactive programming?
Definition:
Reactive programming, unlike the traditional programming paradigm, works with streams of data by reacting to events instead of waiting for them to happen. It is a type of programming that works with asynchronous data streams in a non-blocking way. In other words, reactive programming is the use of asynchronous data streams that send data to the consumer as it is available so that developers can design code that reacts in an asynchronous manner.
Java reactive programming is a programming approach that emphasizes the development of applications that are both responsive and scalable, capable of handling both concurrent and asynchronous operations effectively.
- A programming paradigm designed to process steams of messages in a non-blocking and asynchronous manner, while handling backpressure.
- It is based on Observer design pattern.
- It is for IO calls.
- Reactive programming complements Object-Oriented Programming by providing powerful tools and abstractions to handle asynchronous IO calls and manage complex data flows in modern applications.
Benefits
Why is it beneficial to understand such a complex concept?
The short answer to this question is that responsive applications have become a necessity. They are no longer a luxury.
The longer answer to this question involves the multiple benefits of reactive programming:
- Reactive programming in Java is a great way to create modern, responsive, and scalable applications.
- The reactive programming paradigm can handle both asynchronous and non-asynchronous code. Developers can write code that handles multiple tasks at once without having to stop the main thread from executing. This is especially useful considering how asynchronicity may have caused a lot of problems in Java. Reactive programming simplifies the administration of asynchronous processes and handles asynchronous data streams.
- Higher customer satisfaction: With faster and more responsive applications created, customers have a better overall experience. Most users nowadays have become used to the fluid response of websites and are quick to point out if a website is less responsive.
- Boosting performance: In order to maximize performance, reactive coding reduces the overhead of synchronization and context switching. Because it offers methods for effectively managing streams of data and events, reactive programming enables you to create applications that are performant.
Similarities and Differences between java8 stream API and reactive programming
- Laziness - Unless there is a terminal operator (
.subscribe() in reactive programming
), nothing is going to happen. - https://stackoverflow.com/questions/30216979/difference-between-java-8-streams-and-rxjava-observables
Is this an alternative to virtual threads?
No, Virtual Threads solve an entirely different problem.
We can use Virtual Threads for reactive programming.
Reactive programming is not an alternative to Virtual Threads.
Reading material
- https://github.com/ReactiveX/RxJava/wiki
- https://github.com/ReactiveX/RxJava?tab=readme-ov-file
- https://reactivemanifesto.org/
- https://reactivex.io/
- https://spring.io/blog/2016/06/07/notes-on-reactive-programming-part-i-the-reactive-landscape
- https://spring.io/blog/2016/06/13/notes-on-reactive-programming-part-ii-writing-some-code
- https://www.baeldung.com/cs/reactive-programming
Tags
- Observer pattern
- Inbound Outbound models in programming
- Reactive Programming - Implementations of the specification in Java
- Reactive Programming - example use cases
- Reactive Programming - Reactive Manifesto - Reactive programming specification
- Reactive Programming - Challenges
- Reactive Programming - Terminology
- Reactive Programming - Operators
- Reactive Programming - Schedulers
- Reactive Programming - Convert REST endpoints to be Reactive
Reactive Programming - Hot and Cold publishers
All the simple publishers we see in examples are Cold publishers.
https://projectreactor.io/docs/core/milestone/reference/advancedFeatures/reactor-hotCold.html