当前位置:网站首页>How to choose sentinel vs. hystrix current limiting?
How to choose sentinel vs. hystrix current limiting?
2022-07-24 11:20:00 【Program ape DD_】
Sentinel It is open source by ali middleware team , Lightweight high availability traffic control component for distributed service architecture , The main pointcut is traffic , Slave flow control 、 Fusing the drop 、 Multiple dimensions such as system load protection help users protect the stability of the service .
You may ask :Sentinel And the previous commonly used fuse degradation library Netflix Hystrix What are the similarities and differences ?
This article will analyze Sentinel and Hystrix Contrast , Help you choose the technology .
Overview
Let's take a look first Hystrix Official introduction of :
Hystrix is a library that helps you control the interactions between these distributed services by adding latency tolerance and fault tolerance logic. Hystrix does this by isolating points of access between the services, stopping cascading failures across them, and providing fallback options, all of which improve your system’s overall resiliency.
You can see Hystrix Our focus is on Isolation and Fuse Mainly fault-tolerant mechanism , Calls that time out or are blown will fail quickly , And can provide fallback Mechanism .
and Sentinel The focus of the project is :
Diversified flow control
Fusing the drop
System load protection
Real time monitoring and console
It can be seen that the problems solved by the two are quite different , Now let's compare them respectively .
Common characteristics
Comparison between resource model and execution model
Hystrix Command mode is adopted in the design of resource model , Call external resources and fallback Logic is encapsulated into a command object (HystrixCommand / HystrixObservableCommand), Its underlying implementation is based on RxJava Realized . Every Command When creating, specify commandKey and groupKey( Used to distinguish resources ) And the corresponding isolation strategy ( Thread pool isolation or Semaphore isolation ).
In the thread pool isolation mode, you need to configure the parameters corresponding to the thread pool ( Thread pool name 、 Capacity 、 Queue timeout ), then Command It will execute according to the specified fault-tolerant policy in the specified thread pool ; In the semaphore isolation mode, you need to configure the maximum concurrency , perform Command when Hystrix Will limit its concurrent calls .
Sentinel The design is simpler . comparison Hystrix Command Strong dependency isolation rules ,Sentinel The coupling between resource definition and rule configuration is lower .Hystrix Of Command The reason why it strongly depends on the configuration of isolation rules is that isolation rules will directly affect Command Implementation .
At the time of execution Hystrix Can parse Command Isolation rules to create RxJava Scheduler And schedule execution on it , If it is in thread pool mode Scheduler The bottom thread pool is the configured thread pool , If it is semaphore mode, it is simply wrapped into the execution of the current thread Scheduler. and Sentinel The execution model is not specified , It doesn't pay attention to how the application is executed .
Sentinel The principle of is very simple : According to the rules of the corresponding resource configuration, execute the corresponding flow restriction for the resource / Downgrade / Load protection strategy . stay Sentinel Resource definition and rule configuration are separated in . The user first passes through Sentinel API Define resources for the corresponding business logic ( Buried point ), Then you can configure rules when needed .
There are two ways of burying points :
try-catch The way ( adopt
SphU.entry(...)), The user is in catch Execute exception handling in the block / fallbackif-else The way ( adopt
SphO.entry(...)), When to return to false Exception handling is performed when / fallback
from 0.1.1 Version start ,Sentinel It also supports annotation based resource definition , You can specify exception handling functions and... Through annotation parameters fallback function .
from 0.2.0 Version start ,Sentinel Introduce asynchronous call link support , It is convenient to count the data of asynchronous call resources , Maintain asynchronous call links , At the same time, it has an adaptive asynchronous framework / Library capabilities .
Sentinel Provide diversified rule configuration methods . Except directly through loadRules API Register rules out of memory , Users can also register various external data sources to provide dynamic rules . Users can dynamically change the rule configuration according to the current real-time situation of the system , The data source will push the changes to Sentinel With immediate effect . in addition , The interview questions and answers of the distributed architecture series have been sorted out , WeChat search Java Technology stack , Send it in the background : interview , You can read it online .
Comparison of isolation design
Isolation is Hystrix One of the core functions of .Hystrix Provides two isolation strategies : Thread pool isolation (Bulkhead Pattern) Isolated from semaphores , One of the most recommended and commonly used is thread pool isolation .Hystrix Thread pool isolation creates different thread pools for different resources , Different service calls occur in different thread pools , Queue in the process pool 、 Timeout and other blocking conditions can quickly fail , And can provide fallback Mechanism . The advantage of thread pool isolation is that the isolation degree is relatively high , You can process the thread pool of a resource without affecting other resources , But the cost is the of thread context switching overhead The larger , Especially for low latency calls .
however , On the ground , Thread pool isolation does not bring many benefits . The first is that too many thread pools will greatly affect performance . Consider such a scenario , stay Tomcat And so on. Servlet Container usage Hystrix, In itself Tomcat The number of threads is very large ( Maybe dozens or hundreds ), If you add Hystrix Thread pool created for each resource , The total number of threads will be very large ( Hundreds of threads ), In this way, there will be a great loss in context switching .
in addition , The thorough isolation of thread pool mode makes Hystrix You can queue for different resource thread pools 、 The timeout conditions are handled separately , But this is actually a problem to be solved by timeout fusing and flow control , If the component has the ability of timeout fusing and flow control , Thread pool isolation is not so necessary .
Hystrix Semaphore isolation limits the number of concurrent calls to a resource . Such isolation is very lightweight , Only limit the number of concurrent calls to a resource , Instead of explicitly creating a thread pool , therefore overhead The relatively small , But the effect is good , Timeout failure is also supported .
Sentinel The function of semaphore isolation can be provided by the flow control of concurrent thread number mode . And combined with the fuse degradation mode based on response time , It can be automatically degraded when the average response time of unstable resources is relatively high , Prevent too many slow calls from filling up the concurrent number , Affect the whole system .
More comprehensive 、 More detailed Spring Cloud Alibaba Tutorials can be learned for free here :https://blog.didispace.com/spring-cloud-learning/
Fuse degradation comparison
Sentinel and Hystrix The fuse degradation function of is essentially based on fuse mode (Circuit Breaker Pattern).Sentinel And Hystrix Support based on failure ratio ( Abnormal rate ) The fuse is degraded , When the call reaches a certain order of magnitude and the failure rate reaches the set threshold, it will fuse automatically , At this time, all calls to this resource will be block, It's not until the specified time window has passed before the heuristic recovery . As mentioned above ,Sentinel It also supports fuse degradation based on average response time , It can automatically fuse when the service response time continues to soar , Refuse more requests , It didn't recover until a period of time . This can prevent cascading blocking caused by very slow calls .
Comparison of real-time index statistics
Hystrix and Sentinel The implementation of real-time index data statistics is based on sliding window .Hystrix 1.5 Previous versions implemented sliding windows through circular arrays , Through lock fit CAS Update the statistical information of each bucket .Hystrix 1.5 The implementation of real-time index statistics is reconstructed , The index statistical data structure is abstracted into a responsive flow (reactive stream) In the form of , It is convenient for consumers to use indicator information . At the same time, the bottom layer is transformed into based on RxJava Event driven mode , After the service call is successful / Failure / Publish the corresponding event when timeout , Through a series of transformation and aggregation, the real-time index statistical data stream is finally obtained , Can be fused or Dashboard consumption .
Sentinel At present, it abstracts Metric Indicator statistics interface , The bottom layer can have different implementations , The current default implementation is based on LeapArray High performance sliding window , It may be introduced later as needed reactive stream Such as implementation .
Sentinel The characteristics of
In addition to the common features of the two mentioned before ,Sentinel It also provides the following features :
Lightweight 、 High performance
Sentinel As a highly available flow control component with complete functions , Its core sentinel-core There is no unnecessary dependency , After packing, there is less than 200 KB, Very lightweight . Developers can safely introduce sentinel-core Don't worry about dependency . meanwhile ,Sentinel Provides a variety of extension points , Users can easily expand according to their needs , And it fits seamlessly Sentinel in .
introduce Sentinel The performance loss is very small . Only in business single machine level more than 25W QPS There will be some significant impact (5% - 10% about ), stand-alone QPS When it's not too big, the loss can be ignored .
flow control
Sentinel It can be used for different call relationships , With different operating indicators ( Such as QPS、 Number of concurrent calls 、 System load, etc ) Benchmarking , Control the flow of resource calls , Adjust the random request to the appropriate shape .
Sentinel Support diversified traffic shaping strategies , stay QPS When it is too high, it can automatically adjust the flow to a suitable shape . Commonly used :
Direct rejection mode : That is, the exceeded request is directly rejected .
Slow start preheating mode : When traffic surges , Control the rate of flow through , Let the flow slowly increase , Gradually increase to the upper limit of the threshold within a certain period of time , Give the cooling system a warm-up time , Avoid crushing the cold system .

Uniform mode : utilize Leaky Bucket Uniform speed mode of algorithm implementation , Strictly control the time interval of request passing , Requests stacked at the same time will be queued , Requests exceeding the timeout are directly rejected .

Sentinel And support Current limiting based on call relationship , Including flow restriction based on caller 、 Flow restriction based on call chain entry 、 Associated flow limiting, etc , Depending on Sentinel Powerful call link statistics , It can provide accurate current limit of different dimensions .
Sentinel 0.2.0 Start supporting Hot spot parameter current limiting , It can count hotspot parameters in real time and control the flow of resource calls for hotspot parameters .
More comprehensive 、 More detailed Spring Cloud Alibaba Tutorials can be learned for free here :https://blog.didispace.com/spring-cloud-learning/
System load protection
Sentinel Protect the dimensions of the system , Load protection algorithm draws lessons from TCP BBR Thought . When the system load is high , If the request continues to enter , May cause system crash , 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 in an edge state at this time , This increased traffic will cause the machine to crash , Finally, the whole cluster is not available . In response to this situation ,Sentinel The corresponding protection mechanism is provided , Make the system's inlet flow and the system's load reach a balance , Ensure that the system processes the most requests within its capabilities .

Real time monitoring and control panel
Sentinel Provide HTTP API It is used to obtain real-time monitoring information , Such as calling link statistics 、 Cluster point information 、 Rule information, etc . If the user is using Spring Boot/Spring Cloud And used Sentinel Spring Cloud Starter, It can also be easily exposed through Actuator Endpoint To get some information about the runtime , Such as dynamic rules . future Sentinel It also supports standardized indicator monitoring API, It can easily integrate various monitoring systems and visualization systems , Such as Prometheus、Grafana etc. .
Sentinel Console (Dashboard) Provides machine discovery 、 Configuration rules 、 See real time monitoring 、 Check the calling link information and other functions , It makes it very convenient for users to view, monitor and configure .

ecology
Sentinel At present, it is aimed at Servlet、Dubbo、Spring Boot/Spring Cloud、gRPC And so on , Users only need to introduce corresponding dependencies and make simple configuration to enjoy Sentinel High availability traffic protection capability . future Sentinel More common frameworks will be adapted , And for Service Mesh The ability to provide cluster traffic protection .
summary
Finally, use a table to make a comparison and summary :
| Sentinel | Hystrix | |
|---|---|---|
| Isolation strategy | Semaphore isolation | Thread pool isolation / Semaphore isolation |
| Fuse degradation strategy | Based on response time or failure rate | Based on the failure rate |
| Real time target realization | The sliding window | The sliding window ( be based on RxJava) |
| Rule configuration | Support multiple data sources | Support multiple data sources |
| Extensibility | Multiple extension points | Plug in form |
| Annotation based support | Support | Support |
| Current limiting | be based on QPS, Support current limiting based on call relationship | Limited support |
| Traffic shaping | Support slow start 、 Uniform mode | I won't support it |
| System load protection | Support | I won't support it |
| Console | Open the box , Configurable rules 、 Check second level monitoring 、 Machine discovery, etc | Imperfect |
| Common frame adaptations | Servlet、Spring Cloud、Dubbo、gRPC etc. | Servlet、Spring Cloud Netflix |
Reference resources :https://github.com/alibaba/Sentinel/wiki/
We have created a high-quality technical exchange group , With good people , I will be excellent myself , hurriedly Click Add group , Enjoy growing up together . in addition , If you want to change jobs recently , Years ago, I spent 2 A wave of large factory classics were collected in a week , Those who are ready to change jobs after the festival can Click here to get !
Recommended reading
··································
Hello , I'm a procedural ape DD,10 Old driver developed in 、 Alibaba cloud MVP、 Tencent cloud TVP、 I have published books and started a business 、 State-owned enterprises 4 In the Internet 6 year . From ordinary developers to architects 、 Then to the partner . Come all the way , My deepest feeling is that I must keep learning and pay attention to the frontier . As long as you can hold on , Think more 、 Don't complain 、 Do it frequently , It's easy to overtake on a curve ! therefore , Don't ask me what I'm doing now, whether it's in time . If you are optimistic about one thing , It must be persistence to see hope , Instead of sticking to it when you see hope . believe me , Just stick to it , You must be better than now ! If you don't have any direction , You can pay attention to me first , Some cutting-edge information is often shared here , Help you accumulate the capital to overtake on the curve .
边栏推荐
- 【C】 Recursive and non recursive writing of binary tree traversal
- 【10】团队协作和跨团队协作
- Performance test summary (I) -- basic theory
- 关于【软件测试-自动化测试之面试技巧和注意事项】——侃侃而谈
- ctfshow ThinkPHP专题 1
- View the source code of idea Download
- Xilinx FPGA Microblaze AXI_ IIC usage and experience
- High speed ADC test experience
- 这才是开发者神器正确的打开方式!
- Linux redis download and installation
猜你喜欢

Robot framework official tutorial (I) getting started
![08 [AIO programming]](/img/a6/156cb97e653190c76f22c88b758fef.png)
08 [AIO programming]

Talk about software testing - automated testing framework

Redis 100 million level data storage scheme hash slot partition

Value and technical thinking of vectorization engine for HTAP

RS485 communication OSI model network layer

Installing Oracle Xe with Linux

Over the weekend, I had a dinner with the technology gurus and talked about the "golden nine and silver ten" peak of the software testing industry [the trend of involution has been formed]

网络爬虫之短信验证

Fiddler packet capture tool summary
随机推荐
[golang] golang implements the string interception function substr
UNIX C language POSIX thread creation, obtaining thread ID, merging thread, separating thread, terminating thread, thread comparison
《Nature》论文插图复刻第3期—面积图(Part2-100)
CSDN会员的魅力何在?我要他有什么用?
Pytorch learning -- using gradient descent method to realize univariate linear regression
视频回放 | 如何成为一名优秀的地学和生态学领域的国际期刊审稿人?
Classification and introduction of arm and series processors
周末和技术大咖们聚餐,聊到了软件测试行业的“金九银十”高峰【内卷之势已然形成】
The solution of permission denied
这个应该是全网最全的接口测试工具之postman
16 tips for system administrators to use iptables
Publish local image to private library
MySQL查询字段匹配某个规则的记录
【反序列化漏洞-01】序列化与反序列化简介
In BS4.String and Difference between text
Taking advantage of the momentum, oceanbase promotes the lean growth of digital payment
【Golang】golang实现sha256加密函数
只会“点点点”,凭什么让开发看得起你?
Idea runs the wordcount program (detailed steps)
网络爬虫之短信验证