Laravel Circuit Breaker Pattern Example

By Hardik Savani | October 28, 2020 | Category : Laravel


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 and laravel 8 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...



Tags : Laravel


Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. 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, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: