Circuit Breaker Pattern in PHP

Circuit Breaker Pattern in PHP

Circuit Breaker PHP is a package by Leonardo Carmo which implements the Circuit breaker design pattern using Redis as the backend.

If you are not familiar with this pattern or want to learn more, check out Martin Fowler’s CircuitBreaker article.

The package provides a Redis adapter (using the PHP Redis module) that follows an interface, meaning you can create different adapters to work with this Circuit Breaker package.

Here’s an example from the readme of setting up an instance of the Circuit Breaker using the provided Redis adapter:

use LeoCarmo\CircuitBreaker\CircuitBreaker;

// Connect to redis
$redis = new \Redis();
$redis->connect('localhost', 6379);

$adapter = new \LeoCarmo\CircuitBreaker\Adapters\RedisAdapter($redis, 'my-product');

// Set redis adapter for CB
CircuitBreaker::setAdapter($adapter);

Here’s a basic example of some of the things you can do with this library:

// Check circuit status for service: `my-service`
if (! CircuitBreaker::isAvailable('my-service')) {  
  die('Circuit is not available!');  
}

// Usage example for success and failure  
try {  
  Service::execute('something');  
  CircuitBreaker::success('my-service');  
} catch (\ServiceException $e) {  
  CircuitBreaker::failure('my-service');  
  die($e->getMessage());  
}

You can learn more about this package, get full installation instructions, and view the source code on GitHub at leocarmo/circuit-breaker-php.

Filed in: News

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Senior Fullstack Developer / Architect (w/m/d)
Remote / Munich - Germany
envivo.select GmbH
Medior full stack developer (Laravel)
Deventer (or remote) in the Netherlands, Dutch speaking required.
MSML B.V.
Senior Full Stack PHP Developer (Laravel)
Remote
MAPPEN
Senior Laravel Developer
Remote
ACTO
Senior Full-Stack Engineer
[REMOTE: USA Only]
Curricula