Implementing Reactive Circuit Breaker Using Resilience4j | by Wenqi Glantz | Feb, 2022

A step-by-step guide to implementing Reactive Resilience4j

Wenqi Glantz
Image background by Craig Glantz

This article will focus on implementing reactive Circuit Breaker using Resilience4j, a Spring Cloud Circuit Breaker library.

Let’s take a step back and take a closer look at some of the options available for REST API consumption.

The “blocking” side of the options in the diagram above are basically all based on the thread-per-request model. This means that the thread will block until the REST API client receives the response. If we have a lot of incoming requests, the application will create many threads, which will exhaust the thread pool or occupy all the available memory. Users may experience performance degradation.

Spring 5 Reactive framework introduced WebClient, an asynchronous, non-blocking solution. The Reactive framework uses an event-driven architecture. It provides means to compose asynchronous logic through the Reactive Streams API. Compared to the synchronous/blocking method, the reactive approach can process more logic while using fewer threads and system resources. Moreover, using WebClient We can make synchronous or asynchronous HTTP requests with a functional fluent API that can integrate directly into our existing Spring configuration and the WebFlux reactive framework.

For REST API consumption, the winner is clear! The non blocking reactive WebClient it is!

There are two main libraries that we can use to implement Circuit Breaker. Netflix Hystrix, which embraces an Object-Oriented design where calls to external systems have to be wrapped in a HystrixCommand offering multiple functionalities. However, In SpringOne 2019, Spring announced that Hystrix Dashboard will be removed from Spring Cloud 3.1 version which makes it officially deprecated. Not a good idea to use a deprecated library. So the choice is clear, Resilience4j it is!

Resilience4j is a standalone library inspired by Hystrix but build on the principles of Functional Programming. Resilience4J provides higher-order functions (decorators) to enhance any functional interface, lambda expression or method reference with a Circuit Breaker, Rate Limiter or Bulkhead.

Other advantages of Resilience4J include more fine tuned configuration options (eg the number successful executions needed to close the Circuit Breaker pattern) and a lighter dependencies footprint.

We are going to use two Spring Boot microservices to demonstrate how to implement reactive Circuit Breaker:

  • customer-service, which acts as the REST API provider, offering customer CRUD endpoints.
  • customer-service-client, which utilizes WebClient through Spring Boot Starter Webflux library to call the REST APIs.

Now let’s dive into the detailed steps to implement Resilience4j for reactive Circuit Breaker.

Since we have chosen WebClient to consume REST API, we need to add the Spring Cloud Circuit Breaker Reactor Resilience4J dependency to our REST client application.

Leave a Comment