Reactive Programming is a programming model which dealt with data streams and event propagation to build responsive and non-blocking system. A responsive system quickly react to all users , in order to ensure a consistently positive user experience. RP is precisely the Observer Design Pattern/Push Down Model where publisher emits the stream of data and subscriber consumes them with managing the backpressure.
Why Reactive Programming?
As you know now a days we are living in the era where there lots of competition in each sector. So if you’re not quick enough to respond to your users they might find some other options. Before this programming model applications were not made responsive means the main thread was blocked until response got computed due to which user experience was not so good. This kind of request is synchronous in nature. Reactive Programming is all about asynchronous processing.
- The benefit is more evident in modern webapps and mobile apps that are highly interactive with a multitude of UI events related to data events. Earlier, interaction with web pages was basically about submitting a long form to the backend and performing simple rendering to the frontend. Now with the advent of Reactive Programming we have asynchronous processing.
- Simpler Code.
- Functional Code than imperative.
By Reactive Programming model we can have a application with following features according to Reactive Manifesto
- Responsive : The system responds in a timely manner if at all possible. Responsiveness is the cornerstone of usability and utility, but more than that, responsiveness means that problems may be detected quickly and dealt with effectively. Responsive systems focus on providing rapid and consistent response times, establishing reliable upper bounds so they deliver a consistent quality of service. This consistent behaviour in turn simplifies error handling, builds end user confidence, and encourages further interaction.
- Resilient: The system stays responsive in the face of failure. This applies not only to highly-available, mission-critical systems — any system that is not resilient will be unresponsive after a failure
- Elastic: The system stays responsive under varying workload
- Message Driven: Reactive Systems rely on asynchronous message-passing to establish a boundary between components that ensures loose coupling, isolation and location transparency
Use Cases for RP Model?
- Spreadsheet cell formulas are the best example of reactive programming. Cells react to changes in the data they reference automatically. For example, A=B+C means that no matter what happens to B and C, A will change accordingly
- Apart of all no blocking features, another great feature to use Reactive programming, is the important use of backpressure. Normally is used in situations where your publisher emit more information than your consumer can process.
- In microservice architecture to enable faster end-to-end processing as services can be executed in parallel/asynchronously. Lets assume if there three microservices A,B and C. A internally consumes B and then C, if we go for synchronous call then what happens. If Service B is down then Service C never be called.