What is Hystrix?

The Chinese name for Hystrix is “porcupine”. The porcupine is covered with spines to protect itself from predators, representing a defense mechanism. This is exactly the same as hystrix itself, so the Netflix team named the framework Hystrix and used the corresponding cartoon character as the logo.

In a distributed system, many dependencies will inevitably fail, such as timeouts, exceptions, etc. How to ensure that the failure of a dependency does not lead to the failure of the entire service is Hystrix’s task. Hystrix provides fuses, isolation, Fallback, cache, monitoring, and more to keep the system usable in the event of a simultaneous failure of one or more dependencies.

Why do we need Hystrix?

In large and medium-sized distributed systems, the system usually has many dependencies (HTTP,hession,Netty,Dubbo, etc.), as shown below:

Under high concurrent access, the stability of these dependencies has a great impact on the system, but dependencies have many uncontrollable problems, such as slow network connection, busy resources, temporarily unavailable, offline services, etc.

Dependency I with QPS of 50 becomes unavailable, but other dependencies are still available.

When a dependency I blocks, most servers’ thread pools BLOCK, affecting the stability of the entire online service. The diagram below:

Applications in complex distributed architectures have many dependencies and will inevitably fail at some point. High concurrency dependencies fail without isolation, and the current application service is at risk of being dragged down.

For example, a system that relies on 30 SOA services, each 99.99% available. 0.3% means 3,000,00 failures for 100 million requests, which translates into approximately 2 hours of service instability per month. As the number of service dependencies increases, the probability of service instability increases exponentially.Copy the code

Solution: To isolate dependencies,Hystrix is a framework for dealing with dependency isolation, as well as governance and monitoring of dependent services.

Netflix developed and successfully used Hystrix on the following scale:

The Netflix API processes 10+ billion HystrixCommand executions per day using thread isolation.   
Each API instance has 40+ thread-pools with 5-20 threads in each (most are set to 10).
Copy the code

How does Hystrix address dependency isolation?

  1. Hystrix uses the Command pattern HystrixCommand(Command) to wrap the dependent call logic, with each Command executed in a separate thread/under signal authorization.
  2. You can configure the dependent call timeout period. The timeout period is generally set to slightly higher than 99.5% average time. When the call times out, the fallback logic is returned or executed directly.
  3. Provide a small thread pool (or signal) for each dependency, and the call will be rejected immediately if the thread pool is full, with no queuing by default. Speed up the failure determination time.
  4. Dependent call results: success, failure (throw exception), timeout, thread rejection, short circuit. Fallback logic is executed when the request fails (exception, rejection, timeout, short circuit).
  5. Provides fuse components that can be run automatically or manually called to stop the current dependence for a period of time (10 seconds). The fuse default error rate threshold is 50%, beyond which it will run automatically.
  6. Provides statistics and monitoring for near real-time dependency.

Hystrix relies on the isolation architecture as shown below:

Hystrix in action

Maven:

< the dependency > < groupId > com.net flix. Hystrix < / groupId > < artifactId > hystrix - core < / artifactId > < version > 1.5.13 < / version > </dependency>Copy the code

Too much source code, not one post up, here only show the main test source code.

public static void main(String[] args) {
    System.out.println(test("javastack"));
}

private static String test(String name) {
    HystrixUtil.HystrixReqConfig hc = HystrixUtil.HystrixReqConfig.withGroupKey("TestGroup").withTimeout(3)
            .withUnit(TimeUnit.SECONDS).withPassNum(64);

    String result = HystrixUtil.getExcuteResult(new HystrixCallableService<String>() {

        @Override
        public String execute() {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "success " + name;
        }

        @Override
        public String fallback() {
            return "fallback " + name;
        }

    }, hc);

    return result;
}
Copy the code

This set a 3-second timeout to enter the circuit breaker.

Sleep for 5 seconds in the test program, enter the fuse and output:

fallback javastack
Copy the code

Sleep for 2 seconds in test program, enter normal flow and output:

success javastack
Copy the code

If the fuse test is successful, even if one service fails, the normal operation of the whole system will not be affected.

Get all the packaged source code can follow the wechat public number below.

Recommended reading

Dry goods: 2TB architect four-stage video tutorial

Interview: the most complete Java multithreaded interview questions and answers

Interview: the most comprehensive ali advanced Java interview questions in history

Interview: The most complete Spring interview questions in history

Tutorial: The most complete Spring Boot complete video tutorial

Books: 15 must-read books for advanced Java architects

Tools: Recommended an online creation flow chart, mind mapping software

Share Java dry goods, high concurrency programming, hot technology tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot news.