Circuit Breaker

When a service is unstable, with frequent timeouts or errors, it may cause an avalanche of service calls. At this time, you can use a circuit breaker to make faulty services return errors in the time to improve user experience and system performance. OpenResty Edge supports three types of circuit breakers: Slow Request Ratio, Error Request Ratio, and Error request Count.

  • Strategy: strategy of the circuit breaker, Slow Request Ratio, Error Request Ratio, and Error request Count.
  • Statistics duration: the size of the time window to perform statistics, unit: second.
  • Fusing Duration: the fusing duration after the fusing is triggered. Requests within the fusing duration will return errors directly. Unit: second.
  • Detection Recovery Duration: the time in Half Open state, during which the circuit breaker will be Closed if the request succeeds or the Detection recovery timeout occurs; if the request fails, the fuse will continue. Unit: second.
  • Slow Request duration: When a request takes longer than this time, the request is considered a slow request. Unit: milliseconds.
  • Slow Request Ratio: the percentage of slow requests that trigger fusing.
  • Error Status Code: When a request status code is the selected status code, the request is considered to be an error request.
  • Error Requests Ratio: The ratio of error requests that triggered the fuse.
  • Error Requests Count: the number of error requests that triggered the fuse.
  • Minimal Requests Count: When the number of requests does not reach this value, the fuse will not be triggered even if the ratio of slow requests, the ratio of error requests, and the number of error requests reach the threshold value.
  • Fusing Action: Exit, Redirect.
  • Response Status Code: When “Exit” is selected for the fusing action, you can select the response status code.
  • Response Body: When “Exit” is selected for the fuse action, you can customize or use the default response body.

Example:

In this example, we have added two page rules:

  • One is used to Enable the circuit breaker.
  • The other is proxy upstream, where the upstream server will make the request wait 3 seconds and then return.

Example results.

$ curl test.com  -v -I
...
< HTTP/1.1 200 OK
...

$ curl test.com  -v -I
...
< HTTP/1.1 200 OK
...

$ curl test.com  -v -I
...
< HTTP/1.1 502 Bad Gateway
...

We sent three requests, the first two timed out (slow requests), and the third immediately returned an error because the minimum number of requests had been reached and the percentage of slow requests was greater than 50%.

Enable Circuit Breaker Event

If you want to see the relevant event on the Admin console when the circuit breaker state changes, it can be turned on by the following actions.

For more information about the events, please see this document.Events