Get started with Circuit breaker pattern using Resilience4j. Circuit breaker is a design pattern used for making fault tolerant software applications. It is used to detect and prevents failures from taking down other services in a micro-service architecture & helps prevent whole system from coming down.
5. Buy Game Service Payment Service
Request HTTP
IMMEDIATE FAILURE
Service is down
6. HOW TO HANDLE IMMEDIATE FAILURE
Return from the method as
soon as an error / exception is
encountered
7. BUT WHAT’S THE DISADVANTAGE?
Even after repetitive
connection errors, we are still
making request to that service
which is down
8. Although immediate failures are good, but if
we know request to a service has failed 10
times, why should be make request 11th time?
9. Buy Game Service Payment Service
Request HTTP
REMOTE SERVICE GOES UNRESPONSIVE
No response from service is
sent back to consumer
service
Waiting for response …..
10. HOW TO HANDLE UNRESPONSIVENESS
We set the timeouts
(connection timeout,
socket timeout)
11. Buy Game Service Payment Service
Request HTTP
DOWNSIDES OF TIMEOUT FAILURES
No response from service is
sent back to consumer
service
Waiting for response …..
Thread pool / resources
Service goes down
Resources exhausted
12. CASCADE FAILURES
Buy Game Service Payment Service
Request HTTP
No response from service
Game Checker
Service
Request
13. CASCADE FAILURES
Cascading failure is a failure where the failure of one part of an
interconnected system results in the failure of more parts, and eventually the
whole system
14. FAULT TOLERANT SYSTEMS
Fault tolerance refers to the ability of a system (a microservice application in our case) to continue
operating without interruption when one or more of its components fail.
Objective of fault tolerant systems is to remove single point of failure from a system
Fault tolerant systems ensures high availability
15. HOW TO MAKE FAULT TOLERANT SYSTEMS
Avoiding Cascade failures
Timeouts
Retry
Circuit Breaker
Bulkhead
Cache Optimizations
16. CIRCUIT BREAKER PATTERN
Circuit breaker acts as a GATE KEEPER for all requests
It monitors the response from supplier service for consecutive failures
When consecutive failure crosses a threshold Circuit breaker trips
As intermittent failures are common, we do not open circuit breaker on first request failure
17. DESIGNING CIRCUIT BREAKER
Buy Game Service Payment Service
Request
Response
Interceptor
Interceptor continuously
monitors if service is up or
not. If Payment Service goes
down or becomes
unresponsive, interceptor
blocks the requests &
notifies consumer there’s an
issue immediately.
18. STATES OF CIRCUIT BREAKER
Buy Game Service Payment Service
Request
Response
Circuit
Breaker
1. CLOSED STATE
CLOSED
When everything is normal, the circuit breaker remains in the CLOSED STATE and all calls pass through to
the supplier (Payment Service)
19. STATES OF CIRCUIT BREAKER
Buy Game Service Payment Service
Request
Response
Circuit
Breaker
2. OPEN STATE
OPEN
When the number of failures exceeds a threshold the breaker trips, and it goes into the Open state.The
circuit breaker returns an error for calls without executing the function.
20. STATES OF CIRCUIT BREAKER
Buy Game Service Payment Service
Request
Response
Circuit
Breaker
3. HALF-OPEN STATE
HALF-
OPEN
Periodically make calls to supplier (Payment Service) to see if it is successfully returning the result. This
state is HALF-OPEN.
21. STATES OF CIRCUIT BREAKER
CLOSED OPEN
HALF-OPEN
allow deny
partial allow
successful
Failure rate > threshold
Request
failed again
After wait
duration
22. WHY DO WE NEED CIRCUIT BREAKER?
FAIL FASTER : Circuit breaker helps services to FAIL FASTER, which helps conserve
resources & keep the system alive
PREVENTS SERVICE FAILURES : Prevents consumer services from failing
AUTOMATICE RECOVERY : As circuit breaker periodically checks if supplier is working
again, & recovers the whole system
23. WHAT IS RESILIENCE4J?
Resilience4j is a library for making Fault Tolerant Applications
Resilience4j provides modules for implementing:
Circuit Breaker
BulkHead
RateLimiter
Resilience4j is inspired by a framework called Hystrix which was designed by Netflix
24. CIRCUIT BREAKER USING RESILIENCE4J
Resilience4j’s uses a sliding window to store and aggregate the outcome of calls for circuit breaker
pattern
Count-based sliding window
It checks out of Last N calls how many were successful & how many failed
And based on that information, circuit breaker state is maintained
Time-based sliding window
It checks out of all the calls made in last N seconds; how many were successful & how many
failed
And based on that information, circuit breaker state is maintained