fabiomsouto / phuse
A no-frills fuse for PHP
Requires
- php: >=7.1
- symfony/polyfill-apcu: ^1.7.0
Requires (Dev)
- phpunit/phpunit: ^7.5 || ^8.5 || ^9.6
Suggests
- ext-apcu: Needed for APC-backed fuses
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-01 22:21:51 UTC
README
This library implements fuses for PHP. It offers an APCu-backed fuse that is safe to use from many processes at once. It is greatly inspired by jlouis' implementation of fuses in erlang, which you can find here.
Installation
Installation is as simple as running
$ composer require fabiomsouto/phuse
You need PHP 7.1 or later, and the APCu extension.
PHP 8.6 needs an APCu version later than 5.1.28.
Keep in mind that APCu is disabled by default on the command line. If it's installed but disabled, the fuse can't store
its state, so it never blows. To use fuses in CLI scripts, set apc.enable_cli=1 in your php.ini.
Changelog
This project follows semantic versioning.
2.1.0
[Added]
- ApcuFuse: an APCu-backed fuse that is safe to use from many processes at once. Get one with
FuseBox::getApcuInstance. - Documentation: which PHP processes share a fuse, and what is unsafe about UnsafeAPCFuse.
[Fixed]
- Melting a blown fuse no longer delays its restart, nor notifies the observers again. Under sustained failures, the fuse could stay blown forever.
- A fuse that was blowing could be restarted right away, if another process checked it at the same time.
- A melt that happens after the restart period, but before the fuse is checked, now counts towards the new window.
- Observers are no longer skipped when another observer detaches itself, or triggers a notification, during an update.
- Deprecation notices in PHP 8.5.
- Documentation: a fuse tolerates
$Mmelts, and blows on the next one.melt()doesn't return a value.
[Changed]
- The minimum PHP version is now declared in composer.json. It is 7.1, which the code already needed since 2.0.0.
- Suggest the APCu extension instead of APC.
- The unit tests run on PHP 7.1 to 8.6, using GitHub Actions instead of Travis.
2.0.0
[Changed]
- Added the return types to class methods that override SplSubject methods to avoid E_DEPRECATED notices in php8.1, as the method signatures need to be equal to be considered compatible.
1.1.0
[Added]
- Compatibility with PHP7
[Changed]
- Improve phpdocs
1.0.1
[Added]
- Unit test for attaching/dettaching observers.
- Unit test for getters
- This makes us have 100% unit test coverage for the only fuse we offer at the moment :)
[Changed]
- Internally, the observers are now stored in an SplObjectStorage object.
1.0.0
[Added]
- A Fuse is now an SplSubject too. This way you can attach SplObserver instances to it, and get notified when the fuse melts or restarts.
- Added a phpunit.xml specification, so you can run the unit tests in a breeze.
[Changed]
- A couple of links in the README.md weren't working, fixed those.
- Added some assertions to some unit tests, for extra safety.
0.0.1
[Added]
- UnsafeAPCFuse implementation: an asynchronous fuse, that relies on APC to store state.
- FuseBox, a Factory to generate fuses
- Basic unit tests
What is a fuse?
A fuse is a component that short-circuits internally after melting, similarly to a real life fuse.
What can it do for me?
A fuse can help your application be more reliable when it needs to communicate with external systems. Let's say that your application A communicates with an application B. If B starts timing out, or throwing errors, how would your application behave? Specifically in the case of PHP, if you have a REST service, and it needs to synchronously hit an external endpoint, latencies in the external service will cause your own service to have increased latencies, locking up your workers, etc. A fuse can sit in between. Whenever you detect a fault, you melt your fuse. After a number of melts, the fuse blows, and it will not contact your service for a while, breaking this dependency chain. Your service can then fail a lot faster, or react accordingly (by contacting a backup service for example).
How do I use a fuse (tutorial)?
Easy peasy. Start by instantiating a fuse:
$M = 10; $T = 100; $R = 1000; $fuse = FuseBox::getApcuInstance("database", $M, $T, $R);
This will setup a fuse, named database. $M is the number of melts the fuse can withstand. $T is the
time interval during which these melts can occur. So in this case, the fuse tolerates 10 melts in 100ms, and the 11th one
blows it. After the fuse blows, it will only restart after $R ms, in this case 1000ms. Melting a blown fuse has no effect.
After your fuse is setup, your app can ask about its state:
$M = 10; $T = 100; $R = 1000; $fuse = FuseBox::getApcuInstance("database", $M, $T, $R); if ($fuse->ok()) { // do whatever }
This is the happy situation. But now let's say that you're doing a POST to some endpoint, and it errors out:
$M = 10; $T = 100; $R = 1000; $fuse = FuseBox::getApcuInstance("database", $M, $T, $R); ... try { $result = $curl->post($url, $data); } catch (Exception $e) { $fuse->melt(); }
You're signaling the fuse that something's wrong. If the fuse melts too many times in a certain time frame, it blows, and stays that way until it cools down.
Which fuse should I use?
Use FuseBox::getApcuInstance. It only uses atomic APCu operations, so concurrent melts are all counted, and exactly one
process blows or restarts the fuse, and notifies its observers.
FuseBox::getUnsafeApcInstance is still available, but processes running in parallel can overwrite each other's data. A
burst of failures might not blow the fuse, a newer blow might be undone, and more than one process might notify its
observers. The two fuses don't share their state, so a fuse starts afresh when you switch.
Which processes share a fuse?
Fuses are shared by the processes that share the APCu memory:
| Setup | Is the fuse shared? |
|---|---|
| PHP-FPM, with nginx or Apache (mod_proxy_fcgi) | Yes, between all the pools of the same PHP-FPM master |
| Apache prefork with mod_php | Yes, between all the Apache processes |
| Apache with mod_fcgid | No, each PHP process has its own fuse |
| Command line | No, each script has its own fuse, and it's lost when the script ends |
Keep in mind that:
- Names must be unique among everything that shares a fuse. If two applications in the same PHP-FPM master both have a
databasefuse, it's the same fuse. - Fuses are not shared between servers.
- Restarting or reloading PHP resets the fuses, and so does clearing APCu. APCu might also drop them if it runs out of memory.
How do I monitor the fuses?
Every fuse that inherits from the Fuse interface is also an SplSubject. As such, you can create a SplObserver and register it along the fuse. Each time the fuse blows or restarts, the Observer will be notified:
class AFuseObserver implements SplObserver { public function update(SplSubject $subject): void { $blown = $subject->blown(); echo "This fuse is now "; echo $blown? "blown\n" : "not blown\n"; } } ... $M = 10; $T = 100; $R = 1000; $fuse = FuseBox::getApcuInstance("database", $M, $T, $R); $observer = new AFuseObserver(); $fuse->attach($observer); // when the fuse melts you will see something in your terminal $fuse->melt(); // outputs "This fuse is now blown" when the threshold is reached ... // after 1000ms... $fuse->ok(); // outputs "This fuse is now not blown" when the restart period ends
Keep in mind, notifying observers can take a long time. As such, keep the update call lean and make sure you're not registering a lot of observers in each fuse, as notifying all of them is an O(n) operation.
Performance
These results are from PHP 8.5.13 and APCu 5.1.28, on a Linux laptop with 6 cores and 12 threads. Expect different numbers on your hardware, but a similar picture. The benchmarks and all the results are in this repository.
What does a fuse buy me?
This simulates 8 workers that get 400 requests per second. Each request calls a dependency that takes 5 ms, until it goes
down for 3 seconds, and calls time out after 250 ms. The fuse has $M = 3, $T = 1000 and $R = 1000.
| No fuse | ApcuFuse | |
|---|---|---|
| Requests slower than 100 ms, out of 4,000 | 1,544 | 309 |
| Slowest response | 2.8 s | 0.5 s |
| Slow requests after the dependency is back | for 0.9 s | none |
Without a fuse, all the workers wait for the dependency, and requests queue up behind them. With a fuse, only a few requests wait for it, each time that the fuse restarts. The others fail fast.
Choose $M and $T for your workload. Workers that wait for a timeout can't melt the fuse more often than they time
out: at most about workers * $T / timeout times in a window, or once per worker if $T is shorter than the timeout. $M
must be lower than that. With the defaults ($M = 10, $T = 100), the fuse never blows in this simulation, as 8 workers
can't melt it more than 10 times in 100 ms.
What does each call cost?
With $M = 10, for a single process:
| ApcuFuse | UnsafeAPCFuse | |
|---|---|---|
ok() on a good fuse |
0.11 µs | 0.10 µs |
ok() on a blown fuse |
0.21 µs | 0.32 µs |
melt() on a good fuse |
1.57 µs | 1.65 µs |
melt() on a blown fuse |
0.22 µs | 0.33 µs |
The cost of melt() depends on $M, as it looks at as many melts. This is with $M melts in the window:
$M |
ApcuFuse | UnsafeAPCFuse |
|---|---|---|
| 1 | 0.90 µs | 0.99 µs |
| 10 | 1.56 µs | 1.65 µs |
| 100 | 7.99 µs | 7.86 µs |
| 1,000 | 72 µs | 69 µs |
What if many processes use the same fuse?
They wait for each other, as APCu locks its memory. This is the worst case, where the processes do nothing but call ok()
on a good fuse. Together, 12 processes make 8.4 million calls per second with ApcuFuse.
Are concurrent melts counted?
ApcuFuse counts all of them. UnsafeAPCFuse loses melts that happen at about the same time, e.g. when many requests time out together.
Tests
Some PHPUnit tests are provided. To run them, you need APCu enabled on the command line (on PHP 7.1 and 7.2, use
composer update, as the lock file is for later versions):
$ composer install $ php -d apc.enable_cli=1 vendor/bin/phpunit
If you want to contribute, make sure to provide unit tests for your contributions.