1. Spring WebFlux: Reactive micro service Introduction

George Gatheca
4 min readMay 8, 2021

--

So what is reactive micro service? Well this is a style of programming that is non blocking. Why non blocking? Well traditional approach to spring micro service has always been spring boot MVC. Well for many use cases this works great, but in some cases it has its short comings. eg.

  1. It uses thread pool approach; This is where when application starts, a preset number of threads lets say 200 are started that handle incoming requests. One thread picks up a request and will service it to the end. The issue is the request involves a blocking IO call eg reading data from a database; Then the thread cannot be used to handle other requests and will be in waiting state until data from database is returned and response sent to client. This is wasteful of resources like memory and CPU cycles.
  2. Dropping of requests as load increases; When lets say there are 200 concurrent requests to a micro service, then all the threads are utilized and other incoming requests are dropped or timeouts. This is especially true if the currently been handled requests are blocking i.e. waiting on a database response. We can resolve this by increasing thread pool size but there are limitations. Each new thread consumes memory and especially running the service in a container or kubernetes world, memory is scarce.

“Spring 5.0 has introduced WebFlux to support the reactive web in a non-blocking manner. WebFlux is based on the reactor API, just another awesome implementation of the reactive stream.” https://www.baeldung.com/spring-mvc-async-vs-webflux

So why use reactive approach to micro services? Reactive micro service offer higher performance with lower memory requirements. A few number of threads are started as compared to MVC and are shared by all requests. This means a single thread can handle a number of incoming requests. This is achieved by Event Queue. Every request is considered as an event and will come to event queue. Then any thread can pick up and event and service it with requirement that it should return immediately.

Illustration of event loop and Events coming in(Queue)

So lets say a request wants all users in a database, then when picked up by a thread, the thread will pass the request to the database layer and return a placeholder of the data immediately to the calling client. The client will use the placeholder to check when data is ready. When the data is ready, an event is raised and placed on the Event Loop. Any thread can now pickup the data and return to the calling client.

Advantages of Reactive Micro Service

  1. Lightweight request processing thread.
  2. Optimum utilization of hardware resources.
  3. Single Event Loop can be shared between an HTTP client and request processing.
  4. A single thread can handle requests over many sockets (i.e. from different clients).
  5. This model provides support for backpressure handling in case of infinite stream response.

Building blocks of Reactive Programming

  1. Mono; This is for returning a single item. e.g. A method that will get the name of user from database might return “String”. To allow for reactive, you have to return “Mono<String>” instead.
  2. Flux; This is for returning many items. e.g. A method that wants to get a list of usernames from a database might return “List<String>”. To allow for reactive, once can choose to return “Mono<List<String>>” which means it will return all the names at once when ready. This is not so good especially when the data been fetched is so large. The other option would be return “Flux<String>” which means each username is pushed to client when ready. This will tell compatible clients to keep listening for incoming data. Something like keeping connection open to receive more data as it comes. This allows supporting backpressure, where a service/database is returning more data than a client can handle. Client can request to get data at a slower rate.

WHATS NEXT

Well enough of theory, now its time for a simple hands on to understand better. So what’s next after this? Well checkout for the next articles on the series.

  1. Spring WebFlux: Reactive micro service Introduction (You are here)
  2. Spring WebFlux: Reactive micro service Practical guide Intro
  3. Spring WebFlux: Using Relational Database (MySQL & PostgreSQL)
  4. Spring WebFlux: Unit testing your REST API (Continuation)
  5. Spring WebFlux: Tips & Tricks, Common mistakes
  6. Spring WebFlux: Load testing using JMeter

Want to learn more, have a topic in JAVA you might need articles on or have questions? Reach out to me on my profile or via LinkedIn here.

Hello👋, Did you find this tutorial helpful, please click the clap 👏 button below to show your support for the author 👇. Also this will help the algorithm for others who are learning to see this story.

--

--

George Gatheca
George Gatheca

Written by George Gatheca

Senior Software Engineer; Spring Java enthusiast; Building scalable solutions; Reducing compute waste.

No responses yet