CIRCUIT BREAKER PATTERN
BY ANKIT GUBRANI
.about-me{
name: Ankit Gubrani;
title: Sr. Software Engineer;
working at: Playstation;
email-id: Ankit.gubrani90@gmail.com;
linkedIn: linkedin.com/in/ankitgubrani;
twitter: @ankitgubrani90;
blog: codebrains.co.in;
}
AGENDA
 Let’s build a microservice application !!!
 Cascade Failures
 Fault Tolerant Systems
 Circuit Breaker
 Circuit breaker implementation using Resilience4j
 Demo
BUILDING A MICROSERVICE
Buy Game Service Payment Service
Request HTTP
Response Response
Buy Game Service Payment Service
Request HTTP
IMMEDIATE FAILURE
Service is down
HOW TO HANDLE IMMEDIATE FAILURE
Return from the method as
soon as an error / exception is
encountered
BUT WHAT’S THE DISADVANTAGE?
Even after repetitive
connection errors, we are still
making request to that service
which is down
Although immediate failures are good, but if
we know request to a service has failed 10
times, why should be make request 11th time?
Buy Game Service Payment Service
Request HTTP
REMOTE SERVICE GOES UNRESPONSIVE
No response from service is
sent back to consumer
service
Waiting for response …..
HOW TO HANDLE UNRESPONSIVENESS
We set the timeouts
(connection timeout,
socket timeout)
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
CASCADE FAILURES
Buy Game Service Payment Service
Request HTTP
No response from service
Game Checker
Service
Request
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
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
HOW TO MAKE FAULT TOLERANT SYSTEMS
Avoiding Cascade failures
 Timeouts
 Retry
 Circuit Breaker
 Bulkhead
 Cache Optimizations
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
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.
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)
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.
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.
STATES OF CIRCUIT BREAKER
CLOSED OPEN
HALF-OPEN
allow deny
partial allow
successful
Failure rate > threshold
Request
failed again
After wait
duration
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
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
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
DEMO
THANK YOU
Contact Us :
LinkedIn : Ankit Gubrani
Twitter : @ankitgubrani90
Email-ID : ankit.gubrani90@gmail.com
Blog : www.codebrains.co.in

Circuit breaker pattern

  • 1.
  • 2.
    .about-me{ name: Ankit Gubrani; title:Sr. Software Engineer; working at: Playstation; email-id: Ankit.gubrani90@gmail.com; linkedIn: linkedin.com/in/ankitgubrani; twitter: @ankitgubrani90; blog: codebrains.co.in; }
  • 3.
    AGENDA  Let’s builda microservice application !!!  Cascade Failures  Fault Tolerant Systems  Circuit Breaker  Circuit breaker implementation using Resilience4j  Demo
  • 4.
    BUILDING A MICROSERVICE BuyGame Service Payment Service Request HTTP Response Response
  • 5.
    Buy Game ServicePayment Service Request HTTP IMMEDIATE FAILURE Service is down
  • 6.
    HOW TO HANDLEIMMEDIATE FAILURE Return from the method as soon as an error / exception is encountered
  • 7.
    BUT WHAT’S THEDISADVANTAGE? Even after repetitive connection errors, we are still making request to that service which is down
  • 8.
    Although immediate failuresare good, but if we know request to a service has failed 10 times, why should be make request 11th time?
  • 9.
    Buy Game ServicePayment Service Request HTTP REMOTE SERVICE GOES UNRESPONSIVE No response from service is sent back to consumer service Waiting for response …..
  • 10.
    HOW TO HANDLEUNRESPONSIVENESS We set the timeouts (connection timeout, socket timeout)
  • 11.
    Buy Game ServicePayment 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 GameService Payment Service Request HTTP No response from service Game Checker Service Request
  • 13.
    CASCADE FAILURES Cascading failureis 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 MAKEFAULT 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 BuyGame 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 CIRCUITBREAKER 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 CIRCUITBREAKER 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 CIRCUITBREAKER 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 CIRCUITBREAKER CLOSED OPEN HALF-OPEN allow deny partial allow successful Failure rate > threshold Request failed again After wait duration
  • 22.
    WHY DO WENEED 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 USINGRESILIENCE4J  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
  • 25.
  • 26.
    THANK YOU Contact Us: LinkedIn : Ankit Gubrani Twitter : @ankitgubrani90 Email-ID : ankit.gubrani90@gmail.com Blog : www.codebrains.co.in