Laravel Cloud is here! Zero-config managed infrastructure for Laravel apps. Deploy now.

Circuit Breaker for Laravel

Last updated on by

Circuit Breaker for Laravel image

The circuit-breaker package by @algoyounes brings the circuit breaker pattern to Laravel. It tracks failures for a named service and prevents cascading failures when a downstream service is unavailable:

  • Three-state circuit tracking: closed, open, and half-open
  • Named circuits to isolate state per service
  • Lifecycle callbacks (i.e., onOpen, onSuccess)
  • Guzzle middleware integration via X-Circuit-Key header
  • And more...

Circuit State Callbacks

Each service gets its own named circuit. You can attach callbacks to respond when the circuit changes state or when a call succeeds or fails:

$circuit = $this->circuitManager->forService('payment-service');
 
$circuit->onOpen(function (CircuitTransition $transition) {
Log::warning('Payment service circuit opened', [
'state' => $transition->getState(),
]);
});
 
$circuit->onSuccess(function (CircuitResult $result, CircuitTransition $transition) {
// Called when a wrapped call succeeds
});
 
$circuit->onFailure(function (CircuitResult $result, CircuitTransition $transition) {
// Called when a wrapped call fails
});

Each callback is triggered by a specific event: for example, onOpen runs when the circuit transitions to the open state (blocking calls due to failures), while onSuccess is triggered after a successful wrapped call. The service-name key isolates state per service, letting you track different external dependencies independently.

Running Operations

Once a circuit is configured, wrap your service calls with run(). The circuit breaker handles whether to allow, block, or test the call based on the current state:

$circuit->run(function () {
return Http::post('https://payments.example.com/charge', $payload);
});

You can also use the CircuitManager directly to skip setting up a CircuitBuilder instance:

$this->circuitManager->run('payment-service', function () {
return Http::post('https://payments.example.com/charge', $payload);
});

Guzzle Middleware

If your project uses Guzzle directly, the package includes middleware that reads a X-Circuit-Key request header to identify which circuit to apply:

use AlgoYounes\CircuitBreaker\Middleware\GuzzleMiddleware;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
 
$stack = HandlerStack::create();
$stack->push(GuzzleMiddleware::create());
 
$client = new Client(['handler' => $stack]);
 
$response = $client->get('https://api.example.com', [
'headers' => [
'X-Circuit-Key' => 'external-api',
],
]);

You can learn more about this package on GitHub at algoyounes/circuit-breaker.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review

The latest

View all →
RedBerry to Host Georgia's First Laravel Meetup in Tbilisi image

RedBerry to Host Georgia's First Laravel Meetup in Tbilisi

Read article
Interruptible Jobs in Laravel 13.7.0 image

Interruptible Jobs in Laravel 13.7.0

Read article
A Free Shift to Check If Your App is Ready for Laravel Cloud image

A Free Shift to Check If Your App is Ready for Laravel Cloud

Read article
Laravel Idempotency: HTTP Idempotency Middleware for Laravel image

Laravel Idempotency: HTTP Idempotency Middleware for Laravel

Read article
Polyscope for Windows is Now Available image

Polyscope for Windows is Now Available

Read article
Laravel Sluggable image

Laravel Sluggable

Read article