How to retry with hystrix

evgeniy44 picture evgeniy44 · Oct 20, 2017 · Viewed 14.2k times · Source

I have a hystrix command that encapsulates a REST call. In case of failure(e.g. timeout) I want to make a single retry and in case of failure return an appropriate error.

As I can see Hystrix doesn't support retries. The only way to do it with Hystrix is to put the main logic into getFallback() method. But it doesn't look to be correct.

So, what is a proper way to implement timeout with hystrix?

Answer

meistermeier picture meistermeier · Oct 20, 2017

Hystrix itself does not care what kind of command gets wrapped by it and it does not support the idea of retries. Example behind the idea: If your command (that wraps a REST request) is parametrised it could be that some resource endpoints should be retried while others not. It won't be nice to have either two commands that do more or less the same nor a technical parameter to activate a retry. Additionally this will add some extra complexity to the project.

To get around this problem and stick with Hystrix you might want to take a look into SpringRetry if you are working on a Spring application.

Another possible solution is resilience4j which could be seen as a combination of Hystrix and SpringRetry in this context.