Circuit Breaker Pattern in PHP

Simple Circuit Breaker pattern implementation for your PHP project. Fully functional, practically no dependencies.

.com software
2 min readAug 8, 2022
Photo by Alina Grubnyak on Unsplash

Martin Fowler explains the definition of a Circuit Breaker on his page:

The basic idea behind the circuit breaker is very simple. You wrap a protected function call in a circuit breaker object, which monitors for failures. Once the failures reach a certain threshold, the circuit breaker trips, and all further calls to the circuit breaker return with an error, without the protected call being made at all. Usually you’ll also want some kind of monitor alert if the circuit breaker trips.

Imagine hundreds of simultaneous processes that call a fragile, remote API service. Without a circuit breaker, if there is a connection problem with the service, it would be called at least a hundred times. An absolute waste of resources.

Instead, a clever developer can introduce a circuit breaker pattern in the middle. If one process detects an outage, it can notify other processes that the service is out-of-order and should not call it, at least for some time.

To implement this pattern, all we need is a caching library. I use Symfony in my current project, but I’ll stick to the PSR-16 Cache abstraction though you can…

--

--

.com software

Father • PHP developer • entrepreneur • working for a €1bn unicorn startup as a backend engineer >>> https://bit.ly/dotcom-software