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

Full Stack or Back-End Developer
Alexandria, VA; Tallahassee, FL; Orlando, FL
Marketing for Change
Full Stack Software Engineer
Atlanta, GA or Remote
Voxie
Laravel/PHP Developer
Chicago, IL
Neon One
Software Engineer, Web Applications
Vaughan, ON, Canada
Blast Motion
Contract Services Software Engineer (Laravel | Vue | Tailwind)
Remote
Riverbed Technology
Senior Laravel Developer
Wilmington, NC
GE Software Inc.
Senior Full Stack Developer (PHP | Vue | DDD)
Charleston, SC
MassageBook