Laravel Circuit Breaker Pattern Example

https://www.itsolutionstuff.com/post/laravel-circuit-breaker-pattern-exampleexample.html

Laravel Circuit Breaker Pattern Example

you can use circuit breaker pattern laravel 6, laravel 7, laravel 8 and laravel 9 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:

Read Also: Laravel Signature Pad Example Tutorial

<?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...

Last updated