Chapter 5. Reactive Programming: Taming the Asynchronicity

In the previous chapter, we introduced reactive systems and how they elegantly handle the challenges of distributed systems. Although never forget that nothing comes for free in the IT world. One of the characteristics of reactive systems is the use of nonblocking I/O. Nonblocking I/O improves the concurrency, responsiveness, and resource utilization of reactive applications. To fully benefit from nonblocking I/O, you must design and develop the code in a nonblocking manner, and that is a not-so-easy challenge.

This chapter explores approaches to writing nonblocking and asynchronous Java code such as callbacks and reactive programming. We also cover flow control and Reactive Streams, which is an essential part of modern reactive applications.

Asynchronous Code and Patterns

How does nonblocking lead to asynchronous code? Remember the design of nonblocking I/O from the preceding chapter. It allows using a few threads to handle concurrent network interactions. That particular architecture reduces memory consumption but also CPU usage. As a consequence, the application code gets executed by one of these I/O threads, and there are scarce resources. If your code unconsciously blocks one of these threads, it would reduce your application’s concurrency and increase the response time, as fewer threads are available to handle the requests. In the worst-case scenario, all the I/O threads get blocked, and the application cannot handle ...

Get Reactive Systems in Java now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.