This repository contains a test around implementation of resilience and (for the moment) around Circuit breaker.
I use 3 libraries, and I want to see implementation, configuration, complexity and result.
To help me (and for my curiosity of course ^_^), I use microprofile starter to initialise all this project.
- Context
- Microprofile Starter
- Projects
- Circuit Breaker configuration
- Circuit breaker Results
- Unit Tests
This is a test around this 3 libraries
- Hystrix (Deprecated : use Resilience4j Instead...)
- Resilience4j
- Microprofile Fault Tolerance
For the moment, I only use the "Circuit Breaker".
To initialise the project, I use Microprofile starter with this configuration
- MP 3.3
- JDK 11
- Wildfly
Version 1.0.0.Alpha4 (include in MP Starter)
more information about this maven plugin
Each folder contains circuit breaker library :
- JDK 11
- Maven 3.6.X
Start Maven command
mvn clean package wildfly-jar:run
Every project run on http://localhost:8080
- hystrix : http://localhost:8080/hystrix
- resilience4j : http://localhost:8080/r4j
- Microprofile Fault Tolerance : http://localhost:8080/mp-ft
Every project expose 2 endpoints :
- /github : this endpoint access to github (https://api.github.com/repos/jufab/opentelemetry-angular-interceptor/releases) and have Circuit Breaker configuration but depend on github availability's
- /github/exception : this endpoint generates a BadRequestException to force circuit breaker reaction
- (Only on Resilience4j, see Bonus) /github/slow : this endpoint reduce respone time (with a Thread.sleep() )
Every project have the same configuration :
- Request Volume Threshold (or Minimum Number Of Calls) : 4
- Error Threshold Percentage (or failure Rate Threshold) : 50%
- Sleep Window (or Wait Duration In Open State or Delay) (Duration) : 5 seconds
So, If there are 2 errors on 4 requests (50%), circuit breaker is open during 5 seconds.
At first try, there are 4 requests before trigger the circuit breaker...
You have circuit breaker between your api exposed and an external API (or an other service/database/etc...)
Suppose that Request Volume Threshold have a value of 2, the sequence is :
This a sequence diagram to help to understand the Request Volume Threshold (or minimum number of calls for Resilience4j)
Circuit breaker open after 2 errors : we suppose Error Threshold Percentage have a value of 100%
This configuration is the percentage of error before circuit breaker open.
So, 50% of 4 Request Volume Threshold, the circuit breaker open after 2 errors.
It's time during circuit breaker is Open and wait to Close (or Half Open).
This schema from Resilience4j resume this state :
If you make call during this time, you have an exception by the circuit breaker.
After this time, circuit breaker closed
With previous sequence and a value of 5 seconds :
After advice of Robert Winkler (@rbrtwnklr) on Twitter, I try this two configurations.
- Slow Call Duration Threshold define time of a slow call (default 60000 ms)
- Slow Call Rate Threshold is a percent value (value between 0 to 100 default 100). it's calculate with the minimumNumberOfCalls value and the number of slow call
For example, we have this configuration :
- minimumNumberOfCalls = 4
- slowCallRateThreshold = 25%
- slowCallDurationThreshold = 3 secondes
And there is no error during call (we have a response) but just a slow call...
Circuit Breaker is now open because we have 25% (slowCallRateThreshold) of 4 calls (minimumNumberOfCalls) greater than 3 secondes (slowCallDurationThreshold)
Sleep Window reset circuit breaker state (open to close)...
In exception endpoint
- Circuit breaker close : expose an HystrixException with a BadRequestException cause
- Circuit breaker open : expose an HystrixException with a RuntimeException cause
In exception endpoint
- Circuit breaker close : expose a BadRequestException
- Circuit breaker open : expose a CallNotPermittedException
In exception endpoint
- Circuit breaker close : expose a BadRequestException
- Circuit breaker open : expose a CircuitBreakerOpenException
Try to test with a unit test circuit breaker (for example in case of fallback?)
Can forceOpen programmatically
see here
Can forceOpen programmatically
see here
No unit test for the circuit breaker : only on integration test (Not Implemented)
to be continued...