Laravel Circuit Breaker Pattern Example
This article will give you example of laravel circuit breaker pattern example. This article will give you simple example of circuit breaker pattern laravel. let’s discuss about laravel circuit breaker example. step by step explain circuit breaker pattern in laravel example. Follow bellow tutorial step of http circuit breaker pattern in laravel.
you can use circuit breaker pattern laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version 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 you can use circuit breaker pattern with php laravel.
Normal Use:
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 good way, because it might be that api fails or down time then it will in process. so that issue will be resolved by circuit breaker pattern. http provide timeout() that throw an exception. see bellow 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 request will fail and api downtime then it will request again for specific time as given by us. so best solution is bellow:
<?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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Google Chart Example Tutorial
- Laravel Firebase Push Notification Tutorial
- How to use Model Events in Laravel 8?
- Laravel Livewire Add or Remove Dynamically Input Fields Example
- Laravel Livewire File Upload Tutorial
- Laravel Multi Step Form Example Tutorial
- Laravel Collection Duplicates Method Example
- Laravel Eloquent WhereNotIn Query Example
- Example of unionAll in Query Builder Laravel
- How to Get Last Inserted Id in Laravel?