Laravel with Packages | Laravel Circuit Breaker Pattern Example

This article will give you an example of a laravel circuit breaker pattern example. This article will provide you with a simple example of the circuit breaker pattern laravel. let’s discuss the laravel circuit breaker example. step by step explain the circuit breaker pattern in the laravel example. Follow the below tutorial step of the HTTP circuit breaker pattern in laravel.

you can use circuit breaker pattern laravel 6, laravel 7, laravel 8, and laravel 9 versions too.

Wikipedia says about Circuit Breaker Pattern, “Circuit breaker is a design pattern used in software development. It is used to detect failures and encapsulates the logic of preventing a failure from constantly recurring, during maintenance, temporary external system failure or unexpected system difficulties.”.

I will show you how to use the circuit breaker patterns with PHP laravel.

we normally call HTTP request using HTTP facade as like bellow:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class APIController extends Controller
{

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$response = Http::get('https://myapp.app/api/admins');
$content = json_decode($response->body());

dd($content);
}
}

Use with Circuit Breaker Pattern:

but I think it’s not a good way because it might be that API fails or downtime then it will in process. so that issue will be resolved by circuit breaker pattern. HTTP provides a timeout() that throws an exception. see below example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class APIController extends Controller
{

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$response = Http::timeout(10)->get('https://myapp.app/api/admins');
$content = json_decode($response->body());

dd($content);
}
}

Best Solution with Circuit Breaker Pattern:

Now let’s see if the request will fail and API downtime then it will request again for a specific time as given by us. so the best solution is below:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Cache\RateLimiter;
use Exception;

class APIController extends Controller
{

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$limiter = app(RateLimiter::class);
$actionKey = 'service_name';
$threshold = 5;

try {
if ($limiter->tooManyAttempts($actionKey, $threshold)) {
return $this->failOrFallback();
}
$response = Http::timeout(3)->get('https://myapp.app/api/admins');
$content = json_decode($response->body());

dd($content);
} catch (Exception $exception) {
$limiter->hit($actionKey, Carbon::now()->addMinutes(15));
return $this->failOrFallback();
}
}
}

I hope it can help you…

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store