官术网_书友最值得收藏!

Circuit breaker pattern

This is an extremely useful pattern. Think again about a situation that you are trying to connect to and use the service, and the service is not available due to any reason. If the service is not going to come up in a short duration, it is no use to keep retrying for connection. Moreover, keeping other resources occupied while retrying wastes a lot of resources that could potentially be used elsewhere.

The circuit breaker pattern helps eliminate such wastage of resources. It can prevent applications from repeatedly trying to connect and use a service that is not available. It also helps applications to detect whether the service is up and running again and eventually allows applications to connect to it.

To implement the circuit breaker pattern, all requests to the service should pass through another service. This service that acts as a proxy to the original service. The purpose of this proxy service is to maintain a state machine and acts as a gateway to the original service. There are three states that it maintains. There can be more states that could be included based on application requirements.

The minimal states needed to implement this pattern are the following:

  • Open: It denotes that the service is down, and the application is shown an exception immediately instead of allowing it to retry or wait for the timeout. When the service is alive again, the state is transitioned to Half-Open state.
  • Closed: This state denotes that the service is healthy and that the application can go ahead and connect to it. Generally, a counter is maintained for maintaining the number of failures before it can transition to the Open state.
  • Half-Open: At some point, as the service is up and running, this state allows a limited number of requests to pass through it. This state is a litmus test checking if the request that passes through it is successful or not. If it is successful, the state is transitioned from Half-Open to Closed. This state can also implement a counter to allow a certain number of requests to be successful before it can transition to the Closed state.

The preceding mentioned states and their transition is shown here:

主站蜘蛛池模板: 从化市| 山丹县| 阿城市| 米林县| 梓潼县| 开封市| 舞钢市| 土默特右旗| 高碑店市| 永胜县| 亳州市| 崇左市| 石柱| 湖州市| 宾川县| 吉林市| 新安县| 揭西县| 巴里| 鲜城| 阳西县| 新源县| 嵊泗县| 固始县| 永福县| 竹山县| 肇州县| 荣成市| 库尔勒市| 贞丰县| 深水埗区| 贡山| 富民县| 楚雄市| 安顺市| 保靖县| 桑植县| 乳山市| 澎湖县| 龙州县| 朝阳市|