4

What's the difference between @CircuitBreaker from spring-retry and @HystrixCommand from spring-cloud-starter-netflix-hystrix?

They both seem to implement the circuit breaker pattern.

And the offical Spring guide is using @HystrixCommand: https://spring.io/guides/gs/circuit-breaker/

In their example:

@HystrixCommand(fallbackMethod = "reliable")
public String readingList() {
URI uri = URI.create("http://localhost:8090/recommended");

  return this.restTemplate.getForObject(uri, String.class);
}

public String reliable() {
  return "Cloud Native Java (O'Reilly)";
}

Is equivalent (as far as I can tell) to:

@CircuitBreaker
public String readingList() {
URI uri = URI.create("http://localhost:8090/recommended");

  return this.restTemplate.getForObject(uri, String.class);
}

@Recover
public String reliable() {
  return "Cloud Native Java (O'Reilly)";
}
George
  • 2,820
  • 4
  • 29
  • 56

1 Answers1

2
  1. @CircuitBreaker- Not sure if it an actual annotation. AFAIK, it is not.
  2. spring-retry- @Retryable is the annotation used to achieve this. What this basically does is retry the current method being executed a specified number of times based upon certain criteria.
  3. @HystrixCommand- This annotation actually triggers the Circuit Breaker functionality when a method is executed.

Effective differences-

  1. @Retryable has nothing to do with Circuit Breaking. It blindly retries the operation even if the result is a failure everytime. It operates on a local context of the current request. What that means it is not aware about the global state of retries on your system. For all it cares is that every request must be getting retried a bunch of times.
  2. @HystrixCommand is a smart annotation. It is aware of the global state of the system and actually stops executing the method if the number of failures breach the Circuit Breaking threshold specified.
Mukul Bansal
  • 878
  • 8
  • 10