PHPackages                             omidrezasalari/circuit-breaker-bundle - 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. omidrezasalari/circuit-breaker-bundle

ActiveSymfony-bundle[Utility &amp; Helpers](/categories/utility)

omidrezasalari/circuit-breaker-bundle
=====================================

A Symfony bundle implementing the Circuit Breaker pattern.

v1.1.0(1y ago)210PHPPHP &gt;=8.1

Since Jan 23Pushed 1y ago1 watchersCompare

[ Source](https://github.com/omidrezasalari/CircuitBreakerBundle)[ Packagist](https://packagist.org/packages/omidrezasalari/circuit-breaker-bundle)[ RSS](/packages/omidrezasalari-circuit-breaker-bundle/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (3)Versions (4)Used By (0)

Circuit Breaker Bundle
======================

[](#circuit-breaker-bundle)

This package is a **Symfony bundle** that implements the Circuit Breaker pattern. It helps protect your services from repeated requests when failures occur. The package is designed to allow you to easily change the storage mechanism (e.g., Redis, APCu, or custom implementations).

Features
--------

[](#features)

- **Circuit Breaker Pattern:** Prevents repeated requests after multiple failures.
- **Flexible Storage:** By default, it uses APCu, but you can switch to Redis or any custom storage implementation.
- **Configurable:** Dynamically configure failure thresholds and timeout periods via Symfony configuration files.
- **Symfony 6+ Compatible:** Integrates seamlessly with modern Symfony projects.

---

Installation
------------

[](#installation)

### 1. APCu Implementation (Optimized and Persistent in Memory)

[](#1-apcu-implementation-optimized-and-persistent-in-memory)

APCu is an internal PHP cache that stores data in shared memory and can be accessed across different requests on a server.

#### 1.1 Install and Enable APCu

[](#11-install-and-enable-apcu)

Before using APCu as a storage mechanism for the Circuit Breaker bundle, ensure that APCu is installed and enabled on your server.

**1.2 Check if APCu is installed:**

```
php -m | grep apcu
```

#### If it's not installed, you can install it with the following commands:

[](#if-its-not-installed-you-can-install-it-with-the-following-commands)

- For Linux (Ubuntu/Debian):

```
sudo apt install php-apcu

```

- For macOS:

```
brew install php-apcu

```

#### 1.3 Enable APCu for CLI (Command Line Interface):

[](#13-enable-apcu-for-cli-command-line-interface)

Edit your `php.ini` file to enable APCu for the CLI:

```
apc.enable_cli = 1
```

### 2. Install the package via Composer, run the following command:

[](#2-install-the-package-via-composer-run-the-following-command)

```
composer require omidrezasalari/circuit-breaker-bundle
```

After installing, register the bundle in your `config/bundles.php`:

```
return [
    // Other bundles...
    Omidrezasalari\CircuitBreakerBundle\CircuitBreakerBundle::class => ['all' => true],
];
```

---

Configuration
-------------

[](#configuration)

The circuit breaker bundle uses a configurable failure threshold and timeout period. You can configure these parameters in your `config/services.yaml`.

```
#.env
REDIS_HOST=localhost
REDIS_PORT=6379
FAILURE_THRESHOLD=5
TIMEOUT_PERIOD=60
```

```
# config/services.yaml
parameters:
  redis_host: '%env(REDIS_HOST)%'
  redis_port: '%env(REDIS_PORT)%'
  circuit_breaker.failure_threshold: '%env(FAILURE_THRESHOLD)%'
  circuit_breaker.timeout_period: '%env(TIMEOUT_PERIOD)%'

services:
  Omidrezasalari\CircuitBreakerBundle\Service\RedisStorage:
    arguments:
      $host: '%redis_host%'
      $port: '%redis_port%'

  Omidrezasalari\CircuitBreakerBundle\Service\ApcuStorage: ~

  Omidrezasalari\CircuitBreakerBundle\Service\CircuitBreaker:
    arguments:
      $failureTreshHold: '%circuit_breaker.failure_threshold%'
      $timeoutPeriod: '%circuit_breaker.timeout_period%'
```

### Package Configuration File (circuit\_breaker.yaml):

[](#package-configuration-file-circuit_breakeryaml)

This file is placed under the `config/packages/` directory and allows users to define custom configurations for the package. The file looks like this

```
# config/packages/circuit_breaker.yaml
circuit_breaker:
  storage_service: 'Omidrezasalari\CircuitBreakerBundle\Service\ApcuStorage'
  failure_threshold: 5
  timeout_period: 60
```

### Configuration Details:

[](#configuration-details)

#### Storage Service:

[](#storage-service)

By default, the storage\_service is set to `ApcuStorage`. If you prefer to use `RedisStorage`, simply change this value in `circuit_breaker.yaml`:

```
circuit_breaker:
  storage_service: 'Omidrezasalari\CircuitBreakerBundle\Service\RedisStorage'
  failure_threshold: 5
  timeout_period: 60
```

#### `failure_threshold` and `timeout_period` parameters:

[](#failure_threshold-and-timeout_period-parameters)

These values control how many failures are allowed before the circuit breaker trips and for how long it stays open.

### Custom Storage

[](#custom-storage)

The bundle comes with a default `ApcuStorage` implementation. If you want to use a different storage solution, implement the `StorageInterface` and pass it to the `CircuitBreaker` service.

```
namespace App\Service;

use Omidrezasalari\CircuitBreakerBundle\Service\StorageInterface;

class CustomStorage implements StorageInterface
{
    // Implement required methods: get, set, increment, expire
}
```

---

Usage
-----

[](#usage)

Here’s an example of how to use the CircuitBreaker service:

```
use Omidrezasalari\CircuitBreakerBundle\Service\CircuitBreaker;

class YourService
{
    private CircuitBreaker $circuitBreaker;

    public function __construct(CircuitBreaker $circuitBreaker)
    {
        $this->circuitBreaker = $circuitBreaker;
    }

    public function someOperation()
    {
        if ($this->circuitBreaker->isOpen('your_service')) {
            throw new \Exception("Circuit breaker is open. Operation not allowed.");
        }

        try {
            // Perform the operation
        } catch (\Exception $e) {
            $this->circuitBreaker->attemptFailure('your_service');
            throw $e;
        }

        // If operation succeeds
        $this->circuitBreaker->attemptSuccess('your_service');
    }
}
```

---

API
---

[](#api)

### `isOpen(string $serviceName): bool`

[](#isopenstring-servicename-bool)

Checks whether the circuit breaker for the given service is open. Returns `true` if open, `false` if not.

### `attemptSuccess(string $serviceName): void`

[](#attemptsuccessstring-servicename-void)

Called when a service operation is successful. Resets the failure count and closes the circuit breaker.

### `attemptFailure(string $serviceName): void`

[](#attemptfailurestring-servicename-void)

Called when a service operation fails. Increments the failure count. If the failure threshold is reached, the circuit breaker will open.

---

Running Tests
-------------

[](#running-tests)

To run the tests for the bundle, you can use PHPUnit:

```
vendor/bin/phpunit
```

---

Contributing
------------

[](#contributing)

If you'd like to contribute to the development of this bundle, feel free to fork the repository, create a branch, and submit a pull request. Please ensure your code follows the coding standards and includes tests for any new features or bug fixes.

---

License
-------

[](#license)

This bundle is open-source and available under the [MIT License](LICENSE).

---

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance45

Moderate activity, may be stable

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~21 days

Total

3

Last Release

432d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/70a4524c1d529872be7f3ff871bb51368b0705ab0407a5ca213c1ebea996669a?d=identicon)[omidrezasalari](/maintainers/omidrezasalari)

---

Top Contributors

[![omidrezasalari](https://avatars.githubusercontent.com/u/48174239?v=4)](https://github.com/omidrezasalari "omidrezasalari (1 commits)")

---

Tags

circuit-breakerphpsoftware-developmentsymfonysystem-design

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/omidrezasalari-circuit-breaker-bundle/health.svg)

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

###  Alternatives

[winzou/state-machine-bundle

Bundle for the very lightweight yet powerful PHP state machine

34010.4M15](/packages/winzou-state-machine-bundle)[wallabag/wallabag

open source self hostable read-it-later web application

12.6k2.2k](/packages/wallabag-wallabag)[stfalcon/tinymce-bundle

This Bundle integrates TinyMCE WYSIWYG editor into a Symfony2 project.

2692.9M24](/packages/stfalcon-tinymce-bundle)[symfony/ai-bundle

Integration bundle for Symfony AI components

30282.3k6](/packages/symfony-ai-bundle)[sylius/inventory-bundle

Flexible inventory management for Symfony applications.

19176.7k4](/packages/sylius-inventory-bundle)[netgen/content-browser

Netgen Content Browser is a Symfony bundle that provides an interface which selects items from any kind of backend and returns the IDs of selected items back to the calling code.

14112.1k8](/packages/netgen-content-browser)

PHPackages © 2026

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