Dariusz Mydlarz

Dariusz Mydlarz

Software Craftsmanship, Project Management & Producitivity

QR code

Hystrix Circuit Breaker - 101

  • Hystrix is a neat Circuit Breaker provided by Netflix.
  • In order to protect external calls use the below code at simplest cases.
public class ForecastCommand extends HystrixCommand<Forecast> {
    private final String city;

    public CommandHelloWorld(String city) {
        this.city = city;

    protected String run() {
        return forecastService.forecastFor(city);

void itCanBeUsedLike() {
    ForecastCommand command = new ForecastCommand("Krakow");
    String s = command.execute();
    Future<String> s = command.queue();
    Observable<String> s = command.observe();
  • That way when a resource (forecastService) is unavailable for a reason, Hystrix will eventually stop calling it making your responses faster. And preventing downstream service from an exhaustion.
August 04, Krakow, Vistulan Boulevards
August 04, Krakow, Vistulan Boulevards

Every time you call any external resource you should be prepared for problems. You can name it faults. If you rely on the network or the database you must ensure your system is not affected by their problems. Remember to prepare fault injection scenarios and implement them.

For instance, imagine a situation when you rely on a downstream service. And by average it takes 100-200 ms to response. But that service lives in some kind of environment. And many bad things can happen to him like:

  • resources (e.g. CPU) exhaustion on a machine (naughty neighborhood),
  • vast amount of requests - resulting in requests queuing,
  • slower responses from database,
  • network connection problems.

So it is highly probable that you’re not gonna receive responses in regular times. So the latency propagates also to your system and your clients are also affected. Imagine a scenario when you load a home page for your online shop and service responsible for loading images has delays. If you just wait for them to load a site, clients of your shop will simply go to another place.

On the other hand, you can cut that offending service, say sorry to a user but display the rest. For a user, it’s better to see a part of a web page instead of waiting an endless amount of seconds staring at a blank screen.

And now circuit breaker pattern comes on a stage. It’s an idea where you simply remove that connection, preventing its affection on the whole system. You just remove that single point of pain, not allowing other to suffer from it.

Hystrix is a very neat and easy-to-use implementation from Netflix. You can read a lot more on the project’s Wiki. To simply gather up benefits you get from it:

  1. it wraps your external calls,
  2. it supports: synchronous, asynchronous & reactive calls execution,
  3. it nicely handle errors - timeouts & exceptions,
  4. it can even provide you a cache of responses,
  5. it also supports batching of your requests,
  6. it’s also easy to test,
  7. it has very good documentation - with common patterns included.

I believe that if you write applications that depend on other external resources (like most of them), you should have Hystrix in your toolbelt. It’s written against faults which makes your system more resilient and respond faster.

I like the Hystrix library cause it is easy to use. It’s just a wrapper for your external calls. And it publishes very nice metrics for free. Oh, did I forget to mention about Hystrix Dashboard?

Have fun using it!