Сlassic programming models give way to reactive programming, especially in the sphere of developing highly scalable applications. Reactive programming provides an increase in performance, helps to prevent errors with multi-threading, and better distribute the load. Let’s answer the most popular questions regarding reactive programming in Java.
Q1. What is reactive programming?
Reactive programming is the coding style, which is aimed to work with asynchronous data streams and a certain propagation of changes (see below). Simply put, reactive programming is about making modifications to the execution environment in a specific order.
Reactive programming is focused on the work with data streams. Everything that happens or changes in an application (calls, notifications, users’ actions, system errors, etc.) can be turned into asynchronous data streams.
Q2. What is meant by this specific propagation of change?
Actually, the question is: What is the difference between the synchronous, asynchronous, and reactive approach? It is easier to understand this using an example. Let’s say that some Tom wants to go to the movies with his friends, Kate and Jim. What should he do?
- Tom goes to the ticket office and buys the tickets. After that, Tom goes to Kate’s home. Together with Kate, they go to Jim’s home, and finally, they all go to the cinema. This is a too long and not very productive process, which corresponds to the sync approach.
- Tom orders a ticket to the nearest cinema, phones to his friends and goes to the movies. The film is about to start, so he enters alone. Kate has no time to buy a ticket, while Jim comes a bit later and buys a ticket on a different row. This situation illustrates the async method.
- Tom orders all tickets online and phones to his friends. Though Tom, Kate, and Jim travel from different parts of the city, they meet and go to the movies together. We see that all asynchronous actions should be completed before the beginning of the next one, and this is an example of a reactive approach.
Q3. Reactive programming and reactive systems: what is the difference?
If you are using reactive programming in your work, it doesn’t mean that you are creating a reactive system. The reactive system is a responsive, distributed system that should demonstrate four crucial features:
- be responsive and handle requests in relevant periods;
- be sustainable when a system error happens (it can be a crash, timeout, etc.);
- be well-scalable to work with various quantity of loads with the necessary minimum of resources;
- use messages for async interactions between systems’ components.
It is a big deal to build a reactive system: every node should contain the concurrency model based on the tasks, the async non-blocking model of development, and use non-blocking input-output.
Q4. Why is reactive programming useful for Java?
Java, as an example of old object-oriented programming, requires using async processing when it works with a huge scope of data. Async processing, in turn, can make code more difficult to understand and harder to maintain. Reactive programming simplifies work with asynchronous processes, that’s why it is so needed if you want to build a fast and responsive system.
Q5. What frameworks can developers use if they need reactive programming in Java?
The last release of Java, Java 8, tried to extend the built-in functionality, but it didn’t become popular with software developers. Yet, there are third-party solutions that work with Java perfectly. Such frameworks are regularly updated and easy to use.
- RxJava, the first extension for reactive programming in Java, works with all versions of this programming language since Java 6. With the help of this framework, developers can build asynchronous software using Java and Android Java. RX offers developers to use a wide set of tools needed for any kind of work with data streams: creating, combining, filtering, or merging.
- Reactive Streams is a complex of several Java interfaces that also can be used with other languages. These interfaces ensure that the language for interactive libraries is common and provide main blocks of Publisher and Subscriber.
- Eclipse Vert.x is an event-driven and non-blocking toolkit for building reactive applications with minimal hardware. It demonstrates a completely configurable nature and uses the Netty library for networking.
- Atmosphere framework 2.0 differs with its cross-framework and cross-browser asynchronous character. It is used for building real-time applications. It provides a friendly and simple API.
- Project Reactor is an open-source project developed by the Pivotal team. This framework doesn’t require a bridge and is built explicitly on the Reactive Streams. Another feature of Reactor is that it allows developers to wrap low-level runtimes, non-blocking applications and is positioned as a library of “the fourth Generation”. With the help of Project Reactor, programmers can create efficient reactive systems with amazing message passing (about 10 million messages per second).
- Spring Framework 5.2.6 provides in-built reactive features for creating HTTP servers and clients. Also, it offers annotations in programming for the processing of HTTP requests. Developers can use Spring on Reactor or turn to its features with the help of different libraries.
- Ratpack (the latest release was on April 27, 2020) is the package of libraries that are used for building HTTP services. It is needed for the implementation of Reactive Streams for providing compatibility. Developers who work with Spring Boot can implement Ratpack into a Spring application with the help of special autoconfiguration. It simplifies their work and allows them to request and listen to an HTTP endpoint without using one of the in-build servers of Spring Boot.
- Akka is a “third Generation library” (the latest update was on May 11, 2020), a set of tools needed for building applications with the Actor pattern in Java or Scala. It allows using communication between processes with the help of Akka Streams, and Reactive Streams.
Q6. What are the business and tech benefits of reactive programming?
Reactive programming has several obvious advantages, such as:
- An application demonstrates a high efficiency: processes large amounts of data quickly and efficiently.
- A web application becomes more user-friendly and responsive.
- Readable and predictable code allows developers to make modifications as frequently as they need without complications.
- It simplifies the callback process, the work with async threads, and data streams.
- It provides a lot of operators for convenient work and implementation of back-pressure.
- As a result, the code is clear and understandable.
Q7. Should all projects use reactive programming?
If your project requires upgrades in such spheres as data dependencies, the safety of the tread, the connection between components, or managing async tasks, you should think about reactive programming. It can solve all these problems simultaneously, and also make your code more clear. In its turn, the clear code saves time for your developers: it is easy to write, read, and maintain.
Reactive programming provides a substantially different way of connection between parts of your application, storing, and processing data. It will be a good choice if you are going to update your site, for example, with the possibility to leave comments or change the rating. Reactive programming will help with processing a large amount of data. Reactive components can be entered step by step without changing the entire programming model.
It will be the right solution in case of high-load and multi-user applications, for example, social networks, games, audio, or video applications. It solves problems with:
- External service calls: it optimizes work with such external situations and dependencies between them, so their logical composition becomes more simple for developers and, as a result, faster for clients.
- If you require to process numerous messages simultaneously, reactive programming can help.
- Dependencies in complicated spreadsheets: with the help of a reactive framework, you can declare dependencies of any level of complexity needed for your goals.
Q8. Can developers use reactive programming in separate parts of applications?
Developers can use reactive programming in several parts of any applications if it is necessary. To give a few examples, reactive programming is applicable to:
- server code for serving highly interactive UI elements;
- proxy servers;
- artificial intelligence;
- data streaming in real-time.
Q9. Why do companies go for reactive programming in Java?
In some cases, a desire to try something new or improve existing projects is enough for companies when they choose reactive programming for their needs. Another reason is that such programming saves resources, allowing developers to do more (process more requests, create data spreads, faster solve clients’ problems, etc.); and it is the main argument for most enterprises.
But it is not so simple, and such a schema doesn’t work: today, the company decides to use reactive programming in their goals, and tomorrow all full-time developers use it in their code. A Java developer should “regroup” to take the next level of logic, as it requires a lot of time and experience to get used to reactive programming. It is not about frameworks uploading and reading manuals. For a company, it will be the right solution to seek a specialist who can analyze your system and define the way to introduce reactive programming to the product with the greatest benefit.