-> Circuit Breaker is a design pattern.
-> It is used to implement fault tolerence systems.
-> Fault Tolerence systems are also called as resillence systems.
What is Circuit Breaker?
Circuit breaker is one of the integration-tier design pattern, it is used for protecting the client applications against the slow/un-responsive remote services.
The Circuit breaker is not something that belongs to Spring Framework or something that is only applied in microservices technology. It is an independent design pattern of its own and can be applied anywhere when an client application is trying to access an remote service (the remote service can be rmi, ejb, soap, rest)
Whenever an client application is trying to access an remote service, due to various different reasons the remote service could be un-responsive or might be slow in serving the requests. The remote service could be
1. serving heavy requests due to high traffic
2. running into low system resources
3. it might be accessing another remote system which seems to be slow
Requirement:
-> When m1 ( ) method failed to give response to client then m2() method should provide response to client.
@RestController
public class DemoRestController {
@GetMapping("/")
public String m1() {
System.out.println("********m1() method executed.....");
String msg = "This is m1() method response";
try {
int i = 10 / 0;
} catch (Exception e) {
e.printStackTrace();
msg = m2();
}
return msg;
} public String m2() {
System.out.println("********m2() method executed.....");
String msg = "This is m2() method response";
return msg;
}
}
------------------------------------------------------------------------
How to solve this problem?
We need to implement Circuit breaker design pattern as below:
Whenever the client application has detected an remote service is un-responsive and taking more amount of time in returning the response than stipulated interval of time (response timeout), the client application immediately has to cut the chord of the current request and increment the failure count.
If the failureCount at the client application has reached to a Threshold, then all the subsequent requests from the client application to the remote service should be terminated locally without spawing a thread or connection to the remote service. After certain interval of time again the client application can allow the requests to the remote service conditionally. This whole process of monitoring and allowing the requests in controlled way to the remote application is called "Circuit Breaker".
-> As per above program when exeception occured in 'try' block then catch block will be executed and it is calling 'm2 ( )' method.
-> When m1( ) method is failing continuosly (ex : for 5 requests) then i want to execute only m2 ( ) method directley for next 30 minutes. We can achieve this requirement by using 'Circuit Breaker'.
-----------------------
@SpringBootApplication
@EnableHystrix
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
---------------------------------------
@RestController
public class DataRestController {
@GetMapping("/data")
@HystrixCommand(
fallbackMethod="getDataFromDB",
commandProperties= {
@HystrixProperty(name="circuitBreaker.requestVolumeThreshold", value="5"),
@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value="10000")
}
)
public String getDataFromRedis() {
System.out.println("**Redis() method called**");
if (new Random().nextInt(10) <= 10) {
//throw new RuntimeException("Redis Server Is Down");
}
// logic to access data from redis
return "data accessed from redis (main logic) ....";
}
public String getDataFromDB() {
System.out.println("**DB() method called**");
// logic to access data from db
return "data accessed from database (fall back logic) ....";
}
}
Comments
Post a Comment