Find a working solution at GitHub. It is based on Spring 5 milestone 1 and the current state of the experimental spring-boot-*-web-reactive libraries.

It is much like a normal Spring MVC controller. It is only the return type (Mono) that is different and the fact that the code is non-blocking itself: using the reactive HTTP WebClient.

Clearly, Pivotal believe in this model. And so do Oracle: who will include reactive types into Java 9 [2] that adheres to the Reactive Streams Specification [3]. Vert.x is another framework that is fully based on reactive programming. And there are many other languages and frameworks that supports it. So it seems like this model is here to stay.

Reasons for adapting this model

A rather pervasive promise that I have encountered is that reactive applications can exhibit superior performance. They can scale extremely well – apparently only to be limited by the networking capabilities of the underlying platform.

Thats all fine. But we’re not all Google, Facebook or Twitter. So to be honest I’m pretty satisfied with the juice that I can get out of the typical non-reactive Web application. But surely, there must be other wins too…

So what is it that makes the reactive model compelling ?

I am not going to pretend to be the expert here. But I’ve found these interesting remarks about it [4]:

The scalability of the application is not limited by choices in the application code – nor is it limited by any configuration of the Web container thread pools etc.

Inversion of control: The responsibility of handling the concurrency has been inverted: it is no longer the developers responsibility to tune it and get it right – it is entirely handled by the underlying platforms networking/buffering capabilities.

In my opinion that sounds really interesting. Imagine to be able to not care about tuning thread pools in the Web container – or to not care about ExecutorService threadpools tuning in the source code! Just leave it to the machinery below.

Potential issues

There’s a flipside to this as well [4]: a reactive application is likely more difficult to troubleshoot. And we can shoot ourselves in the foot if we by accident introduce blocking code (fx blocking RDBMS I/O drivers).

Add to that the fact that currently most traditional Java based integration libraries are blocking: so we will have to be creative to make it tick correctly (fx using the blocking-to-reactive pattern).

What do you think?

Are there any compelling reasons, other than those mentioned in this post, for us to switch from the imperative programming model into the reactive programming model?

More from Spring

If you are using the core Spring Framework (that is, not Spring Boot), then you may have encountered a problem where Spring doesn’t seem to completely ignore mocked beans in your tests: Perhaps Spring attempts to inject beans into them or run your @PostConstruct lifecycle methods. In this post I present that problem together with… Continue Reading

In this post I am going to show how to copy MDC data from Web threads to @Async threads using a brand new Spring Framework 4.3 feature: ThreadPoolTaskExecutor#setTaskDecorator() [set-task-decorator]. This is the end result: Notice the third and second last log lines: they have [userId:Duke] just left of the log level. The first line is… Continue Reading

In the context of my favorite framework, Spring Boot, I have recently started to explore the effect of using the Kotlin 1.1 programming language (as an alternative to Java 8). This post describes a few language features that may be interesting to a typical Java 8 developer. Also, I hope you will see that: Spring… Continue Reading

In this post I show how you can create a Spring Boot 1.5 application using Kotlin 1.1 (as opposed to typically Java 8 in these times). The example I’ve created is a typical “Hello World” example. I have chosen to implement a Spring MVC controller – and an awesome Spring Boot integration test. The Gradle… Continue Reading

In this post I show how you can add support for Prometheus in Spring Boot applications. Step 1 of 2: Add the Prometheus Spring Boot Starter You can get Prometheus support in Spring Boot applications via a so-called “starter”. Here is how to get that… It’s a one liner in Gradle: And a five liner… Continue Reading

In this post I show how you, from a Spring Boot application, can control the timezone used by Hibernate when it communicates timestamps with the database. This post is inspired by a very similar recent post by Vlad Mihalcea [1], Hibernate Developer Advocate at Redhat. It’s basically the same subject – but with a Spring Boot… Continue Reading