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 the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Laravel Developer
San Jose, CA
X3 Builders
Lead Front End Developer
Remote
continued
Full-Stack Laravel Developer
Cologne
Revive Interior
Intermediate PHP Developer (Full Stack | CakePHP | Laravel | Vue | jQuery)
Remote
continued
Senior PHP Developer (Full Stack | CakePHP | Laravel | Vue | jQuery)
Remote
continued
Laravel Developer
Amsterdam (partially remote possible)
Let's Book
Web Developer (Laravel)
Tweed Heads, New South Wales, AUSTRALIA
Tursa Employment & Training