Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Obydul's avatar
Level 3

Redis fallback: how to change cache driver from redis to array

Hi Artisans,

I want to change the cache driver from redis to array if the Redis server goes down.

# from
CACHE_DRIVER=redis
# to
CACHE_DRIVER=array

What's the best way to do that? I'm using Laravel 9.52.

0 likes
2 replies
LaryAI's avatar
Level 58

To handle a fallback from Redis to an array cache driver in Laravel, you can create a custom cache manager that checks the availability of the Redis server and switches to the array driver if Redis is down. Here’s a step-by-step solution:

  1. Create a Custom Cache Manager:

    First, create a custom cache manager by extending the default CacheManager class. This custom manager will handle the logic for switching between Redis and array drivers.

    namespace App\Cache;
    
    use Illuminate\Cache\CacheManager;
    use Illuminate\Support\Facades\Redis;
    
    class CustomCacheManager extends CacheManager
    {
        protected function createRedisDriver(array $config)
        {
            try {
                // Attempt to connect to Redis
                Redis::connection()->ping();
                return parent::createRedisDriver($config);
            } catch (\Exception $e) {
                // If Redis is down, fallback to array driver
                return $this->createArrayDriver($config);
            }
        }
    }
    
  2. Register the Custom Cache Manager:

    Next, you need to register this custom cache manager in the service container. You can do this in the AppServiceProvider.

    namespace App\Providers;
    
    use Illuminate\Support\ServiceProvider;
    use App\Cache\CustomCacheManager;
    
    class AppServiceProvider extends ServiceProvider
    {
        /**
         * Register any application services.
         *
         * @return void
         */
        public function register()
        {
            $this->app->singleton('cache', function ($app) {
                return new CustomCacheManager($app);
            });
        }
    
        /**
         * Bootstrap any application services.
         *
         * @return void
         */
        public function boot()
        {
            //
        }
    }
    
  3. Update the Cache Configuration:

    Ensure your config/cache.php is set to use the redis driver by default.

    'default' => env('CACHE_DRIVER', 'redis'),
    
  4. Clear Configuration Cache:

    If you have configuration caching enabled, make sure to clear it so that your changes take effect.

    php artisan config:clear
    

With this setup, your application will attempt to use the Redis cache driver. If the Redis server is down, it will automatically fall back to using the array cache driver. This ensures that your application continues to function even if Redis is unavailable.

kobee313's avatar

Hello, did you find any solution for this problem ?

Please or to participate in this conversation.