Laravel 10 - Laravel Circuit Breaker Pattern Example

Hello Artisan, today I'll show you example of laravel circuit breaker pattern example. Let's explore a step-by-step explanation of the circuit breaker pattern in Laravel with examples.

Follow the tutorial steps for the circuit breaker pattern in Laravel provided below. In your Laravel project, you can easily implement the Circuit Breaker pattern for versions 6, 7, 8, 9, and 10. This design pattern helps detect and handle failures, preventing recurring issues during maintenance, temporary external system failures, or unexpected system difficulties. I'll guide you through using the Circuit Breaker pattern in PHP Laravel.

Table of Contents

  1. Normal Use
  2. Use with Circuit Breaker Pattern
  3. Best Solution with Circuit Breaker Pattern

Normal Use:

We typically make HTTP requests using the Http facade, like this:

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

I believe there's a better way to handle this, as relying on the API without any safeguards could lead to issues if the API fails or experiences downtime. To address this concern, we can use the circuit breaker pattern. In the context of HTTP, you can use the timeout() function to set a timeout and handle exceptions appropriately. Here's an 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, if the request fails or the API experiences downtime, it will attempt the request again after a specific time, as specified by us. So, the optimal solution is as follows:

<?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();
        }
    }
}

That's it for today. I hope it'll be helpful in upcoming project.