Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Fuse current limit
Fuse current limit
Current limiting: The principle is to monitor the QPS or the number of concurrent threads of the application traffic, and control it when the traffic reaches the specified threshold, so as to avoid the system being washed away by the instantaneous traffic peak and ensure the high availability of the application. Protect your system from external crashes.

Fuse: When calling a remote service, the back-end service is bound to fail (timeout or exception), which prevents the application from constantly trying the service that may fail overtime, and can realize the execution of the application without waiting for the downstream service to correct the wrong service.

Downgrading refers to sacrificing non-core business functions to ensure the stable operation of core functions. Through the background switch control, some non-mainstream business functions are degraded, which reduces system dependence and performance loss, thus improving the overall throughput of the cluster.

? Hystrix focuses on the fault-tolerant mechanism of isolation and fuse. If the call times out or fuse will quickly fail, it can provide a fallback mechanism. Hystrix provides two isolation strategies: thread pool mode isolation and semaphore isolation.

Thread pool isolation: different thread pools are created for different resources, and different service calls occur in different thread pools, which can quickly fail when the thread pools are blocked, such as queuing and timeout, and can provide a fallback mechanism. The advantage is that the isolation is relatively high, and a resource is handled separately; The cost is that thread context switching costs a lot, which will fragment machine resources, especially low-latency calls.

Semaphore isolation: It is simpler and cheaper to limit the number of concurrent calls to a resource. However, the disadvantage is that slow calls can't be degraded automatically, and they can only wait for the client to time out, so cascading blocking may still occur.

The fuse aging function of Sentinel and Hystrix is essentially based on the circuit breaker mode. Sentinel and Hystrix both support fuze degradation based on failure rate (anomaly rate). When the call reaches a certain order of magnitude and the failure rate reaches a set threshold, it will automatically blow. At this point, all calls to the resource will be blocked until the specified time window has passed. Sentinel also supports fuse degradation based on average response time, which can automatically fuse when the service response time continues to soar, rejecting more requests and not recovering until a period of time. This can prevent cascading congestion caused by very slow calls.

Hystrix and Sentinel are both based on sliding windows.

Sentinel: Lightweight and high performance, it can control the flow of resource calls according to different call relationships and different operating indicators (such as QPS, number of concurrent calls, system load, etc.). ) and adjust the random request to an appropriate shape.

Fuse strategy: average response time (degree _ grade _ RT): If the interface calls RT within a time window for more than a certain time, the next time window will be automatically fused; DEGRADE_GRADE_EXCEPTION_RATIO: when the abnormal ratio of the call time per second exceeds a certain threshold, it will automatically fuse; Degree _ grade _ exception _ count: when the number of resource anomalies in recent 1 minute exceeds the threshold, it will be blown.

The principle of flow control is to monitor the QPS or the number of concurrent threads of the application flow, and control the flow when it reaches the specified threshold, so as to avoid the system being washed away by the instantaneous flow peak and ensure the high availability of the application.

Traffic shaping Policy: Direct Rejection Mode: Reject redundant requests directly.

Slow-start preheating mode: when the flow surges, control the flow, let the flow increase slowly, and gradually increase to the upper limit of the threshold within a certain period of time, so as to give the cooling system a preheating time to avoid the cooling system being crushed.

Unified mode: The unified mode implemented in leaky bucket strictly controls the time interval for requests to pass, and at the same time queues the accumulated requests. Requests exceeding the timeout period will be directly rejected.

Common current limiting algorithms:

① Reverse current limiting algorithm

Limit the number of requests that can be received per second through the counter. During this period, all requests that exceed the threshold will be rejected.

② sliding window algorithm (used by sentinel)

The sliding window algorithm divides the time period into n small periods (windows), records the number of visits in each small period respectively, and then slides the window forward according to time to count the number of calls in the time window.

③ leaky bucket current limiting algorithm

Implement a container with fixed capacity, put it directly into the leaky bucket when the access request arrives, and discard it if the current capacity has reached the upper limit (current limit value) (triggering the current limit policy). The leaky bucket releases the access request at a fixed rate (that is, the request passes) until the leaky bucket is empty. In fact, message middleware uses the idea of leaky bucket and finite flow. No matter how much the producer requires, the message processing ability depends on the consumer.

④ Token bucket current limiting algorithm

Token bucket is one of the most commonly used algorithms in network traffic shaping and rate limiting. For each request, you need to get a token from the token bucket. If you don't get the token, you need to trigger the current limiting policy.