Hystrix and Sentinel circuit breaker degradation design concept


1 Basic introduction

Insert image description here

The principles of Sentinel and Hystrix are the same: when it is detected that a resource in the calling link has unstable performance, such as a long request response time or an increase in abnormal proportion, the call of this resource will be restricted to make the request faster. Failure to avoid affecting other resources and causing cascading failures.

Sentinel takes two approaches to this problem:

Limiting by the number of concurrent threads
Different from the resource pool isolation method, Sentinel reduces the impact of unstable resources on other resources by limiting the number of concurrent threads for resources. This not only eliminates the cost of thread switching, but also does not require you to pre-allocate the size of the thread pool. When a resource becomes unstable, for example, the response time becomes longer, the direct impact on the resource is the gradual accumulation of threads. When the number of threads accumulates on a specific resource to a certain number, new requests for that resource will be rejected. The stacked threads complete their tasks before continuing to receive requests.

Degrading resources for slow calls and exceptions
In addition to controlling the number of concurrent threads, Sentinel can also quickly circuit break unstable calls based on unstable factors such as response time and exceptions. When the response time of a dependent resource is too long, all access to the resource will be directly denied, and will not be gradually restored until the specified time window has passed.

System adaptive protection

Sentinel also provides system-dimensional adaptive protection capabilities. Preventing avalanches is an important part of system protection. When the system load is high, if requests continue to come in, the system may crash and become unable to respond. In a cluster environment, network load balancing will forward the traffic that should be carried by this machine to other machines. If other machines are also in an edge state at this time, the increased traffic will cause this machine to crash, and eventually the entire cluster will become unavailable.

In response to this situation, Sentinel provides a corresponding protection mechanism to achieve a balance between the system's inlet traffic and the system's load, ensuring that the system can handle the most requests within its capabilities.

Hystrix internally provides two modes of execution logic: semaphore and thread pool.

Insert image description here

img

2 The difference between Hystrix semaphore and thread pool

From hystrix official website
By default, Hystrix uses thread pool mode.
But what is the difference between the two, and how to choose in actual scenarios?

img

If you want to use semaphore mode, you need to configure parameters execution.isolation.strategy =ExecutionIsolationStrategy.SEMAPHORE.

2.1 Semaphore mode

In this mode, receiving requests and executing downstream dependencies are completed in the same thread. There is no performance overhead caused by thread context switching, so most scenarios should choose the semaphore mode. However, in the following case, the semaphore Pattern is not a good choice.

For example, an interface relies on three downstream services: serviceA, serviceB, and serviceC, and the data returned by these three services do not depend on each other. In this case, if the semaphore mode is used for the circuit breaker downgrade of A, B, and C, the interface consumes The time is equal to the sum of the time required to request services A, B, and C. This is undoubtedly not a good solution.

In addition, in order to limit the number of concurrent calls to downstream dependencies, Hystrix can be configured execution.isolation.semaphore.maxConcurrentRequests. When the number of concurrent requests reaches the threshold, the request thread can fail quickly and the execution is degraded.

img

The implementation is also very simple. A simple counter is executed when a request enters the fuse. tryAcquire()The counter is incremented by 1. If the result is greater than the threshold, false is returned, a semaphore rejection event occurs, and the downgrade logic is executed. When a request leaves the fuse, it is executed release()and the counter is decremented by 1.

2.2 Thread pool mode

In this mode, user requests will be submitted to their respective thread pools for execution, and the threads that execute each downstream service are separated to achieve resource isolation. When the thread pool has no time to process and the request queue is full, new incoming requests will fail quickly, which can avoid the spread of dependency problems.

Regarding the issues mentioned in the semaphore mode, the asynchronous execution of the thread pool can effectively improve the interface performance for the multiple downstream services it depends on.

Advantage

  • Reduce the impact when the dependent services fail. For example, an exception occurs in the ServiceA service, causing a large number of request timeouts and the corresponding thread pool to be full. At this time, the calls to ServiceB and ServiceC are not affected.
  • If the interface performance changes, you can easily and dynamically adjust the parameters of the thread pool or the timeout, provided that the Hystrix parameters are dynamically adjusted.

shortcoming

  • Requests are executed in the thread pool, which will definitely bring overhead caused by task scheduling, queuing and context switching.
  • Because it involves cross-threads, there is a problem of transferring ThreadLocal data. For example, the ThreadLocal variable initialized in the main thread cannot be obtained in the thread pool thread.

2.3 Note

Because Hystrix uses the thread pool mode by default, for each Command, a corresponding thread pool will be created during initialization. If there are many interfaces that need to be downgraded in the project, such as hundreds, I don’t know much about Hystrix. If you use the internal mechanism directly according to the default configuration, it may cause a lot of waste of thread resources.

3 Sentinel Introduction

Spring cloud Alibaba Sentinel:https://blog.csdn.net/ZGL_cyy/article/details/130874410

SpringCloud Sentinel actual current limiting and circuit breaker downgrade application:https://blog.csdn.net/ZGL_cyy/article/details/130874410

SpringCloud Sentinel integrates Gateway and real-time monitoring:https://blog.csdn.net/ZGL_cyy/article/details/130874653

Guess you like

Origin blog.csdn.net/ZGL_cyy/article/details/132702298