Member-only story
WebFlux: Modern Way to Build APIs in Spring Boot
We as developers are often creatures of habit. We become familiar with a solution to a specific problem, become proficient in its use, and fall back to using it as our default. In addition, if you are like me, you get busy and may not always spend the needed time evaluating alternatives and revert to using only familiar method. This article will look at how an API can be built in an asynchronous manner instead of the standard blocking request implementation.
Recently I was working on a Spring Boot application and implementing the API part of the code. I reached into my default toolbox and pulled out a standard Spring Boot REST controller as the choice of implementation and began to code the needed CRUD operations as HTTP methods. This produced an API that I understood, worked as required and I knew from experience how to write and perform tests on.
But one issue with this approach is as I implemented these operations were synchronous. This means they follow the standard method where the request is assigned to a single thread that is blocked until it is complete. Therefore, this is often referred to as a blocking request. In this case, several of these API calls were for lengthy processes where a potentially large data object would need to be processed in full, with several dependent sub-operations performed dependent on the data. Even if these would be done as much as possible in parallel, the user would still be left wondering why it takes so long.