Comic: What is a service circuit breaker

Reprinted from the  comic: What is a service fuse

What is a service circuit breaker?


The concept of fusing comes from the circuit breaker in electronic engineering . In the Internet system, when the downstream service responds slowly or fails due to excessive access pressure, the upstream service can temporarily cut off the call to the downstream service in order to protect the overall availability of the system.


This measure of sacrificing parts and preserving the whole is called fusing .


What will happen to our system if circuit breakers are not taken? Let's look at a chestnut.


There are three services A, B, and C in the current system. Service A is upstream, service B is midstream, and service C is downstream. Their call chain is as follows :




Once the downstream service C becomes unavailable for some reason, there is a backlog of requests, and the request thread of service B is also blocked. Thread resources are gradually exhausted, making service B also unavailable. Immediately afterwards, service A also became unavailable, and the entire call chain was dragged down.




A cascading failure of a call link like this is called an avalanche .



As the so-called scraping bone cures poison, a strong man breaks his wrist. At this time, our circuit breaker mechanism is needed to save the entire system. The general process of the circuit breaker mechanism is the same as the exam strategy just mentioned:





Two points need to be explained here:


1. Turn on the fuse

Within a fixed time window, when the interface call timeout ratio reaches a threshold, the circuit breaker will be turned on. After entering the fuse state, subsequent calls to the service interface no longer go through the network, and the local default method is directly executed to achieve the effect of service degradation .


2. Fusing recovery

Fuse cannot be permanent. When the specified time has elapsed, the service will reply from the fuse state and accept the caller's remote call again.







Practical application of service fuse



Spring Cloud Hystrix is ​​implemented based on Netflix's open source framework Hystrix, which implements a series of service protection functions such as service fuse and thread isolation .


对于熔断机制的实现,Hystrix设计了三种状态:


1.熔断关闭状态(Closed)

服务没有故障时,熔断器所处的状态,对调用方的调用不做任何限制。


2.熔断开启状态(Open)

在固定时间窗口内(Hystrix默认是10秒),接口调用出错比率达到一个阈值(Hystrix默认为50%),会进入熔断开启状态。进入熔断状态后,后续对该服务接口的调用不再经过网络,直接执行本地的fallback方法


3.半熔断状态(Half-Open)

在进入熔断开启状态一段时间之后(Hystrix默认是5秒),熔断器会进入半熔断状态。所谓半熔断就是尝试恢复服务调用,允许有限的流量调用该服务,并监控调用成功率。如果成功率达到预期,则说明服务已恢复,进入熔断关闭状态;如果成功率仍旧很低,则重新进入熔断关闭状态。



三个状态的转化关系如下图:








Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325550062&siteId=291194637