Circuit Breaker

Circuit Breaker is an outbound policy. Dataplanes whose configuration is modified are in the sources matcher.

This policy will look for errors in the live traffic being exchanged between our data plane proxies and it will mark a data proxy as an unhealthy if certain conditions are met and - by doing so - making sure that no additional traffic can reach an unhealthy data plane proxy until it is healthy again.

Circuit breakers - unlike active Health Checks - do not send additional traffic to our data plane proxies but they rather inspect the existing service traffic. They are also commonly used to prevent cascading failures in our services.

Like a real-world circuit breaker when the circuit is closed then traffic between a source and destination data plane proxy is allowed to freely flow through it, and when it is open then the traffic is interrupted.

The conditions that determine when a circuit breaker is closed or open are being configured in what we call “detectors”. This policy provides 5 different types of detectors and they are triggered on some deviations in the upstream service behavior. All detectors could coexist on the same outbound interface.

Once one of the detectors has been triggered the corresponding data plane proxy is ejected from the set of the load balancer for a period equal to baseEjectionTime. Every further ejection of the same data plane proxy will further extend the baseEjectionTime multiplied by the number of ejections: for example the 4th ejection will be lasting for a period of time of 4 * baseEjectionTime.

Available detectors:

Usage

As usual, we can apply sources and destinations selectors to determine how circuit breakers will be applied across our data plane proxies.

For example:

  1. apiVersion: kuma.io/v1alpha1
  2. kind: CircuitBreaker
  3. mesh: default
  4. metadata:
  5. name: circuit-breaker-example
  6. spec:
  7. sources:
  8. - match:
  9. kuma.io/service: web
  10. destinations:
  11. - match:
  12. kuma.io/service: backend
  13. conf:
  14. interval: 5s
  15. baseEjectionTime: 30s
  16. maxEjectionPercent: 20
  17. splitExternalAndLocalErrors: false
  18. thresholds:
  19. maxConnections: 2
  20. maxPendingRequests: 2
  21. maxRequests: 2
  22. maxRetries: 2
  23. detectors:
  24. totalErrors:
  25. consecutive: 20
  26. gatewayErrors:
  27. consecutive: 10
  28. localErrors:
  29. consecutive: 7
  30. standardDeviation:
  31. requestVolume: 10
  32. minimumHosts: 5
  33. factor: 1.9
  34. failure:
  35. requestVolume: 10
  36. minimumHosts: 5
  37. threshold: 85

We will apply the configuration with kubectl apply -f [..].

  1. type: CircuitBreaker
  2. mesh: default
  3. name: circuit-breaker-example
  4. sources:
  5. - match:
  6. kuma.io/service: web
  7. destinations:
  8. - match:
  9. kuma.io/service: backend
  10. conf:
  11. interval: 1s
  12. baseEjectionTime: 30s
  13. maxEjectionPercent: 20
  14. splitExternalAndLocalErrors: false
  15. thresholds:
  16. maxConnections: 2
  17. maxPendingRequests: 2
  18. maxRequests: 2
  19. maxRetries: 2
  20. detectors:
  21. totalErrors:
  22. consecutive: 20
  23. gatewayErrors:
  24. consecutive: 10
  25. localErrors:
  26. consecutive: 7
  27. standardDeviation:
  28. requestVolume: 10
  29. minimumHosts: 5
  30. factor: 1.9
  31. failure:
  32. requestVolume: 10
  33. minimumHosts: 5
  34. threshold: 85

We will apply the configuration with kumactl apply -f [..] or via the HTTP API.

The example demonstrates a complete configuration. A CircuitBreaker can also be configured in a simpler way by leveraging the default values of Envoy for any property that is not explicitly defined, for instance:

  1. apiVersion: kuma.io/v1alpha1
  2. kind: CircuitBreaker
  3. mesh: default
  4. metadata:
  5. name: circuit-breaker-example
  6. spec:
  7. sources:
  8. - match:
  9. kuma.io/service: web_default_svc_80
  10. destinations:
  11. - match:
  12. kuma.io/service: backend_default_svc_80
  13. conf:
  14. detectors:
  15. totalErrors: {}
  16. standardDeviation: {}

We will apply the configuration with kubectl apply -f [..].

  1. type: CircuitBreaker
  2. mesh: default
  3. name: circuit-breaker-example
  4. sources:
  5. - match:
  6. kuma.io/service: web
  7. destinations:
  8. - match:
  9. kuma.io/service: backend
  10. conf:
  11. detectors:
  12. totalErrors: {}
  13. standardDeviation: {}

We will apply the configuration with kumactl apply -f [..] or via the HTTP API.

In this example when we get five errors in a row of any type (5 is default Envoy value for totalErrors.consecutive) the data plane proxy will be ejected for 30s the first time, 60s for the second time, and so on.

In the current version of Kuma destinations only supports the service tag.

interval

Time interval between ejection analysis sweeps. Defaults to 10s.

baseEjectionTime

The base time that a data plane proxy is ejected for. The real time is equal to the base time multiplied by the number of times the data plane proxy has been ejected. Defaults to 30s.

maxEjectionPercent

The maximum percent of an upstream Envoy cluster that can be ejected due to outlier detection. Defaults to 10% but will eject at least one data plane proxy regardless of the value.

splitExternalAndLocalErrors

Activates Split Mode.

Split Mode: There are two types of errors that might occur in a circuit breaker:

  • Locally originated: errors triggered locally when estabilishing a connection at the TCP layer (ie: connection refused, connection reset).
  • Externally originated: errors triggered remotely like a 5xx error in the response.

If Split Mode is off, Kuma won’t distinguish errors by their origin and they will be counted together. If Split Mode is on, different parameters can be used to fine tune the detectors. All detectors counts errors according to the state of this parameter.

Detectors

Below is a list of available detectors that can be configured in Kuma.

Total Errors

Errors with status code 5xx and locally originated errors, in Split Mode just errors with status code 5xx.

  • consecutive - how many consecutive errors in a row will trigger the detector. Defaults to 5.

Gateway Errors

Subset of totalErrors related to gateway errors (502, 503 or 504 status code).

  • consecutive - how many consecutive errors in a row will trigger the detector. Defaults to 5.

Local Errors

Taken into account only in Split Mode, number of locally originated errors.

  • consecutive - how many consecutive errors in a row will trigger the detector. Defaults to 5.

Standard Deviation

Detection based on success rate, aggregated from every data plane proxy in the Envoy cluster.

  • requestVolume - ignore data plane proxies with a number of requests less than requestVolume. Defaults to 100.
  • minimumHosts - ignore counting the success rate for an Envoy cluster if the number of data plane proxies with required requestVolume is less than minimumHosts. Defaults to 5.
  • factor - resulting threshold equals to mean - (stdev * factor). Defaults to 1.9.

Failures

Detection based on success rate with an explicit threshold (unlike standardDeviation).

  • requestVolume - ignore data plane proxies with a number of requests less than requestVolume. Defaults to 50.
  • minimumHosts - ignore counting the success rate for an Envoy cluster if the number of data plane proxies with required requestVolume is less than minimumHosts. Defaults to 5.
  • threshold - eject the data plane proxy if its percentage of failures is greater than - or equal to - this value. Defaults to 85.

Thresholds

Alongside the detectors, CircuitBreaker allows configuring thresholds:

  • maxConnections - the maximum number of connections that Envoy will make to the upstream cluster. If not specified, the default is 1024.
  • maxPendingRequests - the maximum number of pending requests that Envoy will allow to the upstream cluster. If not specified, the default is 1024.
  • maxRequests - the maximum number of parallel requests that Envoy will make to the upstream cluster. If not specified, the default is 1024.
  • maxRetries - the maximum number of parallel retries that Envoy will allow to the upstream cluster. If not specified, the default is 3.

Matching

CircuitBreaker is an Outbound Connection Policy. The only supported value for destinations.match is kuma.io/service.

Builtin Gateway support

Circuit Breaker policies are supported on the builtin gateway like any other dataplane.

Non-mesh traffic

When passthrough mode is activated any non-mesh traffic is passing Envoy without applying the CircuitBreaker policies. Read more about Non-mesh traffic.