Skip to content

Circuit Breaker

Joseph Szobody edited this page Mar 28, 2016 · 4 revisions

Configuring a circuit breaker for your service will provide fault handling, minimize stampede issues, provide auto-retry, and detailed logging.

Cache setup is required for the circuit breaker. If caching is not setup, your circuit breaker config will be ignored.

Circuit breaker flow

Specifying circuit breaker config in the main description

The circuit breaker config should use the circuitBreaker key in the master description array.

All circuit breaker parameters are optional, simply providing an empty array will enable the circuit breaker with default config options.

Example:

// Master description array
[
    ...
    'cache' => [
        ...
    ],
    'circuitBreaker' => [
        'failureThreshold' => 10,
        'failureInterval' => 300,
        'autoRetryInterval' => 300,
        'successThreshold' => 5,
        'handlers' => [
            'failure' => function($event, $breaker, $context) { // handle the event },
            'trip' => \Path\To\HandlerClass::class
        ]
    ]

Top level attributes

Name Value Required Default Description
failureThreshold int no 10 How many failures (within the failureInterval) will trip the breaker.
failureInterval int no 300 Number of seconds where we keep a count of failures. Failures older that this interval are forgotten.
autoRetryInterval int no 300 Once the breaker is tripped, wait this many seconds before retrying.
successThreshold int no 5 When retrying (after being tripped) this many successes must be reached before the breaker is fully closed and the service is considered operational.
handlers array no Array of callbacks for the four events (failure, success, trip, reset). Each callback can be a closure, or it can be a path to a class that has an __invoke method.

Event handlers

Your event handlers will receive three parameters:

Name Type Description
event string Will be failure, success, trip, or reset
breaker CircuitBreaker Circuit breaker instance
context string Content from the service response. Currently only the failure event includes context.

Example of a closure handler

'failure' => function($event, $breaker, $context) { // do something },

Example of a class handler

The class needs an invoke function:

Class MyHandler {
    public function __invoke($event, $breaker, $context) {
        // do something
    }
}

Then you can supply the class path:

'failure' => \Path\To\MyHandler::class,