HTTP 503 Service temporarily unavailable
) until the end of a recovery (cooldown) time period.
Tyk can trigger events when the circuit breaker trips and when it resets. These events can be used for monitoring, alerting, or automation of recovery processes.
500
or above.
The circuit breaker is rate-based, meaning that it counts the number of failure responses received in a rolling sample window and trips if the failure rate exceeds the configured threshold.
The rolling sample window is set to 10 seconds and the circuit breaker is designed to trip only if a user-configurable minimum number of samples (requests) fail within the window period.
Thus, if the sample size is set to 100 and the failure rate is set to 0.5 (50%) then the circuit breaker will trip only when there have been a minimum of 100 requests made in the past 10 seconds of which at least 50 have failed (returned an HTTP 500
or higher error).
Once the breaker has been tripped it will remain open, blocking calls to the endpoint until a configurable cooldown (or return-to-service) period has elapsed. While the breaker is open, requests to the endpoint will return HTTP 503 Service temporarily unavailable
.
15/100 = 0.15
.
The cooldown period is the time that the circuit breaker will remain open after the error rate threshold has been met and the breaker has been tripped.
There is also an option to enable or disable the half-open state if this would be damaging to your system.
BreakerTripped
(or BreakerTriggered
) event should alert you to the fact that at least one host is failing.
BreakerTripped
eventBreakerReset
eventBreakerTriggered
eventBreakerTriggered
event, the state change will be indicated in the Status
field in the webhook template as follows:
CircuitEvent = 0
CircuitEvent = 1
.*
path.
If you’re using Tyk OAS APIs, then you can find details and examples of how to configure the circuit breaker middleware here.
If you’re using Tyk Classic APIs, then you can find details and examples of how to configure the circuit breaker middleware here.
Using the Circuit Breaker middleware with Tyk OAS APIs
Tyk’s circuit breaker middleware is configured at the endpoint level, where it monitors the rate of failure responses (HTTP 500 or higher) received from the upstream service. If that failure rate exceeds the configured threshold, the circuit breaker will trip and Tyk will block further requests to that endpoint (returning HTTP 503 Service temporarily unavailable
) until the end of a recovery (cooldown) time period.
When working with Tyk OAS APIs the circuit breaker is configured in the Tyk OAS API Definition. You can do this via the Tyk Dashboard API or in the API Designer.
If you’re using the legacy Tyk Classic APIs, then check out the Tyk Classic page.
operationId
defined in the OpenAPI Document that declares both the path and method for which the middleware should be added. Endpoint paths
entries (and the associated operationId
) can contain wildcards in the form of any string bracketed by curly braces, for example /status/{code}
. These wildcards are so they are human-readable and do not translate to variable names. Under the hood, a wildcard translates to the “match everything” regex of: (.*)
.
The circuit breaker middleware (circuitBreaker
) can be added to the operations
section of the Tyk OAS Extension (x-tyk-api-gateway
) in your Tyk OAS API Definition for the appropriate operationId
(as configured in the paths
section of your OpenAPI Document).
The circuitBreaker
object has the following configuration:
enabled
: enable the middleware for the endpointthreshold
: the proportion of requests that can error before the breaker is tripped, this must be a value between 0.0 and 1.0sampleSize
: the minimum number of requests that must be received during the rolling sampling window before the circuit breaker can tripcoolDownPeriod
: the period for which the breaker will remain open after being tripped before returning to service (seconds)halfOpenStateEnabled
: if set to true
then the circuit breaker will operate in half-open mode once it has been trippedGET /status/200
endpoint.
It will configure the circuit breaker so that if a minimum of 10 requests (sampleSize
) to this endpoint are received during the rolling sampling window then it will calculate the ratio of failed requests (those returning HTTP 500
or above) within that window.
threshold = 0.5
) then the breaker will be trippedcoolDownPeriod
)GET /status/200
will return HTTP 503 Service temporarily unavailable
halfOpenStateEnabled = true
) so when the threshold has been reached and the breaker is tripped, Tyk will periodically poll the upstream service to test if it has become available againHTTP 503 Service temporarily unavailable
) until the end of a recovery (cooldown) time period.
When working with Tyk Classic APIs the circuit breaker is configured in the Tyk Classic API Definition. You can do this via the Tyk Dashboard API or in the API Designer.
If you’re using the newer Tyk OAS APIs, then check out the Tyk OAS page.
If you’re using Tyk Operator then check out the configuring the Circuit Breaker in Tyk Operator section below.
circuit_breakers
object to the extended_paths
section of your API definition, with the following configuration:
path
: the endpoint pathmethod
: the endpoint HTTP methodthreshold_percent
: the proportion of requests that can error before the breaker is tripped, this must be a value between 0.0 and 1.0samples
: the minimum number of requests that must be received during the rolling sampling window before the circuit breaker can tripreturn_to_service_after
: the period for which the breaker will remain open after being tripped before returning to service (seconds)disable_half_open_state
: by default the Tyk circuit breaker will operate in half-open mode when working with Tyk Classic APIs, set this to true
if you want Tyk to wait the full cooldown period before closing the circuitGET
requests to the /status/200
endpoint. It will configure a sampling window (samples
) of 10 requests and calculate the ratio of failed requests (those returning HTTP 500 or above) within that window. If the ratio of failed requests exceeds 50% (threshold_percent = 0.5
) then the breaker will be tripped. After it has tripped, the circuit breaker will remain open for 60 seconds (return_to_service_after
). The circuit breaker will operate in half-open mode (disable_half_open_state = false
) so when open, Tyk will periodically poll the upstream service to test if it has become available again.
When the breaker has tripped, it will return HTTP 503 Service temporarily unavailable
in response to any calls to GET /status/200
.
BreakerTripped
and BreakerReset
events, but not the combined BreakerTriggered
event type. You should use API Designer > Advanced Options to add a Webhook plugin to your endpoint for each event.
/httpbin-timeout-breaker
and forwards requests upstream to http://httpbin.org. A hard timeout value of 2 seconds is configured for path /delay/{delay_seconds}
. This will return a 504 Gateway Timeout
response to the client if the upstream response is not received before expiry of the timer.
HTTP GET
requests to the /status/500
endpoint. It will configure a sampling window (samples) of 4 requests and calculate the ratio of failed requests (those returning HTTP 500 or above) within that window. If the ratio of failed requests exceeds 50% (threshold_percent = 0.5) then the breaker will be tripped. After it has tripped, the circuit breaker will remain open for 10 seconds (return_to_service_after). The circuit breaker will operate using the default half-open mode so when open, Tyk will periodically poll the upstream service to test if it has become available again.
When the breaker has tripped, it will return HTTP 503 Service temporarily unavailable in response to any calls to GET /status/500.