Occasionally, services work together to complete requests. There is always a chance that one service will synchronously invoke another that is either unavailable or has such a high delay that it is practically unusable. While waiting for the other service to react, the caller may use valuable resources like threads. The calling service can become resource-constrained as a result, rendering it unable to handle additional requests. Throughout the application, the failure of one service could potentially cascade to other services.
How to prevent a network or service failure from cascading to other services?
*Implement Circuit Breaker Pattern.
*
A proxy that works like an electrical circuit breaker should be used by a service client to call a remote service. All attempts to launch the remote service will immediately fail for a timeout period after the circuit breaker trips when the number of consecutive failures exceeds a threshold. A certain number of test requests are permitted to pass through the circuit breaker after the timeout expires. The circuit breaker restores regular operation if those requests are granted. Otherwise, the timeout period starts over if there is a failure.
Following is Circuit Breaker example using Java Spring Boot and Hystrix lib.
package com.test.service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Service
public class TestService {
@HystrixCommand(commandProperties = {@HystrixProperty( name ="execution.isolation.thread.timeoutInMilliseconds",value="2000")},fallbackMethod="getDataFallBack")
public List<String> getLatestBookNames()throws Exception {
ArrayList<String> listOfBooks = new ArrayList();
//some service to get list of Books
//To Simulate service is taking time to reply
Thread.sleep(5000);
return listOfBooks;
}
public List<String> getDataFallBack() {
System.out.println("Inside fallback");
ArrayList<String> listOfBooks = new ArrayList();
listOfBooks.add("Clean Code: A Handbook of Agile Software Craftsmanship");
listOfBooks.add("Introduction to Algorithms");
return listOfBooks;
}
}
Here We are simulating delay from other services using Thread.sleep.
If We call "getLatestBookNames" method, it will wait for 5 Seconds. We have timeout of 2 seconds and after 2 seconds, Circuit will break, and Circuit breaker (CB) will call "getDataFallBack" method.
This shows that CB will help caller services to handle the failure of other services that they invoke.
For more information visit: https://spring.io/projects/spring-cloud-circuitbreaker
Top comments (0)