lukaszaleckas / laravel-circuit-breaker
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 1
pkg:composer/lukaszaleckas/laravel-circuit-breaker
Requires
- php: ^8.0
- ext-json: *
- laravel/framework: ^8.83|^9.0
Requires (Dev)
- mockery/mockery: ^1.5
- nunomaduro/larastan: ^1.0
- orchestra/testbench: ^6.24
- phpstan/phpstan-mockery: ^1.0
- phpunit/phpunit: ^9.5
- slevomat/coding-standard: ^6.4
- squizlabs/php_codesniffer: ^3.6
This package is auto-updated.
Last update: 2025-11-11 00:18:13 UTC
README
Circuit breaker is a design pattern used in software development. It is used to detect failures and encapsulates the logic of preventing a failure from constantly recurring, during maintenance, temporary external system failure or unexpected system difficulties.
Source: Wikipedia
Further explanation
Let's explain what this does using an example, as they are much better than definitions, in my opinion.
Let's say you have two microservices A and B:
- You are calling microservice
BfromAand have a 30s timeout configured. Bfails to respond and you receive a timeout error.- You keep getting requests to
Aand keep failing with a timeout fromB. - Request queue of
Agets filled up. - Requests to
Astart to timeout.
In this case it would be much better to track request failures
from A to B and return an error immediately.
Circuit Breaker pattern does just that - on A you are tracking
request failures to B and if failure count, in the particular
time window, exceeds threshold, you skip doing those requests
and return an error immediately.
Installation
- Run:
composer require lukaszaleckas/laravel-circuit-breaker
Service provider should be automatically registered, if not add
LaravelCircuitBreaker\CircuitBreakerServiceProvider::class
to your application's app.php.
- Publish
circuit-breaker.phpconfig file:
php artisan vendor:publish --tag=circuit-breaker
Usage
Inject LaravelCircuitBreaker\CircuitBreakerService through constructor.
Use getCircuitBreaker with service name passed as a parameter
to get a circuit breaker instance.
Method usage:
- Use
isClosedin your, for example, HTTP client to determine if requests can be made. - Use
registerFailurewhen you receive an error. - Use
registerSuccesswhen you receive a success response. This is used to close the circuit when/if it's half open.