Good practices in asynchronous programming

The key to reactive programming is to react. You don’t say “do this now,” you say “do this when.” The “when” applies to when you have work to do. The work comes to you as events: a message on a message bus or an HTTP request.

First, I should explain the reason reactive programming is important. One of the benefits of Java is relatively easy threading. That has made threads the predominant model for handling events. When you get an event, you dispatch a thread to handle it. The problem is when you get a lot of events, you wind up creating a lot of threads. Threads can be expensive; each one has stack memory and switching threads requires a system call and context switch.

The Node.js system only had a single thread when it was created. (It introduced worker threads in v10.5.0). And yet it became a very popular system for building servers that could handle thousands of requests. It does this by using an event-driven idiom for handling requests. Because it only had a single thread, most libraries that implement things like HTTP servers or clients, database clients, or other I/O intensive libraries had to use the single event loop of the single thread.

But Java used the thread-per-request, which has become a bottleneck in scaling. Languages like Scala, so named because it could be more scalable, were created with extensive frameworks to enable event-driven or asynchronous I/O. Java 8 introduced CompletableFuture. Java 9 introduced the Flow class with its Publisher and Subscriber. These are used as the basis for the two fully reactive frameworks, RxJava and Project Reactor.

Because Java has used the thread-per-request model for so long, most libraries that deal with I/O will block. They can block because they expect to own the thread they’re running on and won’t block other requests. But now that we can use the asynchronous model, they become a problem. And because Java now has a hybrid model, it’s hard to tell when and how you should use threads when in a mostly asynchronous system.

#java #programming #software-engineering #reactive-programming #software-development #how to avoid blocking in reactive java

How To Avoid Blocking in Reactive Java
1.80 GEEK