What is Circuit Breaker Design Pattern? How to Use Circuit Breaker Design Pattern in Laravel

tutspack laravel

Hello Artisan,

How are you doing? In this tutorial, I’ll explain circuit breaker design patterns in Laravel applications, and also I’ll show you some code that how we can use circuit breaker design patterns. Circuit breaker is a design pattern used in modern software development.

Circuit Breaker Design Pattern in Laravel

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. – Wikipedia

The circuit breaker is a design pattern that prevents request execution against unresponsive services.You will be happy to know that Laravel 8.x ships with all the ingredients needed to build a circuit breaker

  • An HTTP client
  • The RateLimiter

Let’s see some example of circuit breaker. Before the circuit breaker pattern our application looked something like this:

$response = Http::get('https://fake-api.com/api/search?q=circuit');

return view('contents.index', ['contents' => $response->body()];

The HTTP client will wait for a couple of seconds before throwing an exception to us so it’s a good practice to always define a timeout for our requests. Luckily Laravel’s client comes with a handy method to define the timeout of our request.

$response = Http::timeout(2)->get('https://fake-api.com/api/search?q=circuit');

return view('contents.index', ['contents' => $response->body()];

Well, all are fine but we are still not preventing our service to make a request to the unresponsive API.

$limiter = app(RateLimiter::class);
$actionKey = 'search_service';
$threshold = 10;

try {
    if ($limiter->tooManyAttempts($actionKey, $threshold)) {
        // Exceeded the maximum number of failed attempts.
        return $this->failOrFallback();
    $response = Http::timeout(2)->get('https://fake-api.com/api/search?q=circuit');

    return view('contents.index', ['contents' => $response->body()]);
} catch (\Exception $exception) {
    $limiter->hit($actionKey, Carbon::now()->addMinutes(15));

    return $this->failOrFallback();

By using Laravel’s RateLimiter you can count the failed attempts performed to the search API $limiter->hit($actionKey, Carbon::now()->addMinutes(15)).

By counting those attempts you can prevent our application from making any more requests to this API for a certain amount of time.

Before every request, we check if there are too many failed attempts $limiter->tooManyAttempts($actionKey, $threshold) and if the failed attempts have passed our threshold we will prevent our service to request the search API for 15 minutes.

Now you can use this method in your application. Hope it will help you.

Read More : REST API CRUD with JWT Authentication in Laravel

About Anisur Rahman Shahin

Hello. My name is Shahin. I'm a tech enthusiast guy. Personally, I’m an Optimistic and always in hurry kinda person. I'm a freelance web developer. I am working at Zakir Soft as Laravel Developer. My Portfolio website: https://tutspack.com

View all posts by Anisur Rahman Shahin →

Leave a Reply

Your email address will not be published.