PHPackages                             letscodehu/php-circuit-breaker - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. letscodehu/php-circuit-breaker

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

letscodehu/php-circuit-breaker
==============================

PHP Circuit Breaker component

0.2.9(9y ago)036MITPHPPHP &gt;=5.4.0

Since Feb 23Pushed 9y ago1 watchersCompare

[ Source](https://github.com/letscodehu/php-circuit-breaker)[ Packagist](https://packagist.org/packages/letscodehu/php-circuit-breaker)[ Docs](https://github.com/letscodehu/php-circuit-breaker)[ RSS](/packages/letscodehu-php-circuit-breaker/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (3)Versions (13)Used By (0)

What is php-circuit-breaker
===========================

[](#what-is-php-circuit-breaker)

[![Build Status](https://camo.githubusercontent.com/aeb323f6171c6d79b995fc9170b650f332609e169fa2467b1b70550ac331de20/68747470733a2f2f7472617669732d63692e6f72672f6c657473636f646568752f7068702d636972637569742d627265616b65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/letscodehu/php-circuit-breaker)

A component helping you gracefully handle outages and timeouts of external services (usually remote, 3rd party services).

It is a library providing extremely easy to use circuit breaker component. It does not require external dependencies and it has default storage implementations for APC and Memcached but can be extended multiple ways.

Frameworks support
==================

[](#frameworks-support)

This library does not require any particular PHP framework, all you need is PHP 5.4 or higher.

Symfony 2
---------

[](#symfony-2)

If you are using [Symfony 2](https://github.com/symfony/symfony) framework you should use [php-circuit-breaker-bundle](https://github.com/ejsmont-artur/php-circuit-breaker-bundle). It is a bundle i have created to wrap up php-circuit-breaker and integrate it with [Symfony 2](https://github.com/symfony/symfony) components and Dependency Injection.

Other Frameworks
----------------

[](#other-frameworks)

If you are using other frameworks and you would like to use php-circuit-breaker please let me know and we can try to build up an open source integration just like the one for [Symfony 2](https://github.com/symfony/symfony).

Motivation &amp; Benefits
=========================

[](#motivation--benefits)

- Allow application to detect failures and adapt its behaviour without human intervention.
- Increase robustness of services by addinf fail-safe functionality into modules.

Installation
============

[](#installation)

You can download sources and use them with your autoloader or you can use composer in which case all you nees is a require like this:

```
"require": {
    "ejsmont-artur/php-circuit-breaker": "*"
},

```

After that you should update composer dependencies and you are good to go.

Use Case - Non-Critical Feature
-------------------------------

[](#use-case---non-critical-feature)

- Your application has an Non-Critical Feature like: user tracking, stats, recommendations etc
- The optional feature uses remote service that causes outages of your application.
- You want to keep applicaton and core processes available when "Non-Critical Feature" fails.

Code of your application could look something like:

```
    $factory = new Ejsmont\CircuitBreaker\Factory();
    $circuitBreaker = $factory->getSingleApcInstance(30, 300);

    $userProfile = null;
    if( $circuitBreaker->isAvailable("UserProfileService") ){
        try{
            $userProfile = $userProfileService->loadProfileOrWhatever();
            $circuitBreaker->reportSuccess("UserProfileService");
        }catch( UserProfileServiceConnectionException $e ){
            // network failed - report it as failure
            $circuitBreaker->reportFailure("UserProfileService");
        }catch( Exception $e ){
            // something went wrong but it is not service's fault, dont report as failure
        }
    }
    if( $userProfile === null ){
        // for example, show 'System maintenance, you cant login now.' message
        // but still let people buy as logged out customers.
    }
```

Use Case - Payment Gateway
--------------------------

[](#use-case---payment-gateway)

- Web application depends on third party service (for example a payment gateway).
- Web application needs to keep track when 3rd party service is unavailable.
- Application can not become slow/unavailable, it has to tell user that features are limited or just hide them.
- Application uses circuit breaker before checkout page rendering and if particular payment gateway is unavailable payment option is hidden from the user.

As you can see that is a very powerful concept of selectively disabling feautres at runtime but still allowing the core business processes to be uninterrupted.

Backend talking to the payment service could look like this:

```
    $factory = new Ejsmont\CircuitBreaker\Factory();
    $circuitBreaker = $factory->getSingleApcInstance(30, 300);

    try{
        // try to process the payment
        // then tell circuit breaker that it went well
        $circuitBreaker->reportSuccess("PaymentOptionOne");
    }catch( SomePaymentConnectionException $e ){
        // If you get network error report it as failure
        $circuitBreaker->reportFailure("PaymentOptionOne");
    }catch( Exception $e ){
        // in case of your own error handle it however it makes sense but
        // dont tell circuit breaker it was 3rd party service failure
    }
```

Since you are recording failed and successful operations you can now use them in the front end as well to hide payment options that are failing.

Frontend rendering the available payment options could look like this:

```
    $factory = new Ejsmont\CircuitBreaker\Factory();
    $circuitBreaker = $factory->getSingleApcInstance(30, 300);

    if ($circuitBreaker->isAvailable("PaymentOptionOne")) {
        // display the option
    }
```

Dynamic proxy
=============

[](#dynamic-proxy)

In order to easily wrap a service class and protect it with circuit breaker there is a factory class:

```
$client = new Predis\Client("tcp://127.0.0.1:6379?read_write_timeout=0");
$factory = new Ejsmont\CircuitBreaker\Factory();
$cb = $factory->getRedisInstance($client, 30, 3600);

$proxy = CircuitBreakerProxyFactory::create('SomeSlowService', $circuitBreaker, ["constructor", "arguments"]);

$proxy->remoteCalls(); // the call is protected by the circuit breaker

```

the default wrapping functionality is as follows:

```
        $oldTimeout = ini_get("default_socket_timeout"); // as we are talking about remote calls, we get the old timeout

        if ($this->circuitBreaker->isAvailable($this->serviceName)) { // we are checking if the service is available
            try {
                ini_set("default_socket_timeout", $this->timeout); // override the timeout
                $returnValue = $method->invokeArgs($proxy, $args); // the original methodcall
                $this->circuitBreaker->reportSuccess($this->serviceName); // at the happy path we report the success
            } catch(\Exception $e) {
                $this->circuitBreaker->reportFailure($this->serviceName); // or we report the error
            }

        }
        ini_set("default_socket_timeout", $oldTimeout); // and set the timeout to the original value
```

You may override this by providing a 4th parameter to the create factory method:

```
$proxy = CircuitBreakerProxyFactory::create('SomeSlowService', $circuitBreaker, ["constructor", "arguments"], $ownMethodHook);

```

The 4th parameter must implement the 'guymers\\proxy\\MethodHook' interface.

Features
========

[](#features)

- Track multiple services through a single Circuit Breaker instance.
- Pluggable backend adapters, provided APC, Redis and Memcached by default.
- Customisable service thresholds. You can define how many failures are necessary for service to be considered down.
- Customisable retry timeout. You do not want to disable the service forever. After provided timeout circuit breaker will allow a single process to attempt
- Create a dynamic proxy wrapping around a service class with circuit breaker.

Performance Impact
==================

[](#performance-impact)

Overhead of the Circuit Breaker is negligible.

APC implementation takes roughly 0.0002s to perform isAvailable() and then reportSuccess() or reportFailure().

Memcache adapter is in range of 0.0005s when talking to the local memcached process.

Redis adapter is in range of 0.0002s when talking to the local Redis.

The only potential performance impact is network connection time. If you chose to use remote memcached server or implement your own custom StorageAdapter.

The proxy creation takes roughly 0.0007s. The further calls have no performance impact.

Running tests
=============

[](#running-tests)

- Tests are run via PHPUnit It is assumed to be installed via PEAR.
- Tests can be ran using phpunit alone or via ant build targets.
- The "ci" target generate code coverage repor, "phpunit" target does not.

You can run all tests by any of the following:

```
ant
ant phpunit
ant ci

```

You can run selected test case by running:

```
cd tests
phpunit Unit/Ejsmont/CircuitBreaker/Storage/Adapter/DummyAdapterTest.php

```

Details
-------

[](#details)

Before documentation gets updated you can read more on the concept of circuit breaker and its implementation on my blog

Some implementation details has changed but the core logic is still the same.

Author
------

[](#author)

- Artur Esjmont () via
- Krisztian Papp () via

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 58% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~132 days

Recently: every ~0 days

Total

12

Last Release

3420d ago

PHP version history (2 changes)0.0.1PHP &gt;=5.3.0

0.2.9PHP &gt;=5.4.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16595253?v=4)[Letscode.hu](/maintainers/letscodehu)[@letscodehu](https://github.com/letscodehu)

---

Top Contributors

[![ejsmont-artur](https://avatars.githubusercontent.com/u/345911?v=4)](https://github.com/ejsmont-artur "ejsmont-artur (29 commits)")[![letscodehu](https://avatars.githubusercontent.com/u/16595253?v=4)](https://github.com/letscodehu "letscodehu (21 commits)")

---

Tags

error handlingcircuit breakergraceful

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/letscodehu-php-circuit-breaker/health.svg)

```
[![Health](https://phpackages.com/badges/letscodehu-php-circuit-breaker/health.svg)](https://phpackages.com/packages/letscodehu-php-circuit-breaker)
```

###  Alternatives

[ejsmont-artur/php-circuit-breaker

PHP Circuit Breaker component

1691.0M4](/packages/ejsmont-artur-php-circuit-breaker)[francescomalatesta/laravel-circuit-breaker

A circuit breaker pattern implementation for the Laravel framework

2919.2k2](/packages/francescomalatesta-laravel-circuit-breaker)[gabrielanhaia/laravel-circuit-breaker

Laravel integration for PHP Circuit Breaker — multiple storage drivers, middleware, Artisan commands, and event system

491.6k](/packages/gabrielanhaia-laravel-circuit-breaker)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
