Protect your Micro-service architecture using Netflix-Hystrix and Spring-Boot

I am a fan of microservices architectures and In my last architecture design I wanted to give a shot to the recent Netflix project called Hystrix.

Hystrix is Netflix implementation for the circuit-breaker design.

My microservice architecture built by Spring Boot components. NetFlix and Spring has new project Spring Cloud Netflix.

In that project you can find various Netflix components built-in.

So when should I use Hystrix?

Let’s say you service has API calls. We all know that API calls could be risky. specially when we have flows that depends on them(Connection Timeout, Connections Hangs, various failures)

Now I am paranoid when it comes to failures. Sometimes I spend more time thinking about the failures than the invocation itself.
Hystrix actually helps me to make my app safer.

We do know how to deal with errors but Hystrix made it easier for us.

Bit of Hands on: Let’s say we got this API call:

public String invokeRemoteService(String input) {
  //invoking remote Service

Now we probably going to make our code dirty by adding timeout to the connections, surrounding it with catch exceptions – maybe even forget to do any failure handling.

Hystrix giving us the ability to have a fallback method.

The fallback method can work with many modes(Async,Sync,observable, Invoke on the same/other Thread and more)

What’s nice here is that it force you to think within a failure-mode. So it’s not just the easy implementation but more like a methodology.

I am going to demonstrate the basic mode – the sync mode.

I am using Gradle. So first Added dependencies to your build.gradle file:

compile ''
compile ''

Now Annotate your configuration with:

public class Application  {
    public static void main(String[] args) {, args);
        System.out.printf("Started My service app");

After that let’s decorate the invoking method with HystrixCommand annotation:

@HystrixCommand(fallbackMethod = "defaultInvokeRemoteService")
public String invokeRemoteService(String input) {
    //invoking remote Service
public String defaultInvokeRemoteService(String input) {
//raise alert
//getNextServer from Eureka and invoke the method

//return data from a local cache

Thats easy. All other modes adds abit more logic. But I am sure you’ll be just fine.

Let’s not forget Hystrix dashboard:

With that dashboard we can actually monitor all our api calls and see if any of them is actually a circuit breaker.


Hystrix will monitor your API’s and will decide (by default/custom criteria wither an API call is a circuit breaker or not)

There are additional features. I just demonstrated the main idea and the basic ones.