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

ajay01's avatar
Level 2

onOneServer() method of task scheduler break with php artisan cache:clear

My laravel application is behind load balancer and 2 EC2 instances are running simultaneously. Before I was managing cronjob on only one server but this is not ideal way because if load increased or something unusual error happen and if 3rd EC2 instance initialized then cronjob will start on that server too and my cronjob is performing critical task like stripe charge or expire subscriptions, and if both server perform cronjob task then user will charge on stripe multiple time.

So to tackle this situation I have implimented distributed locking using Redis. I changed my default cache driver from file to redis in .env file. CACHE_DRIVER=redis and in Console/Kernel.php added onOneServer method like this.

$schedule->command('subscription:autorenew')->everyMinute()->withoutOverlapping()->onOneServer();

now even both server has crontab enabled it is performing task on only one server but my deployment pipeline has command php artisan cache:clear due to that lock is being released even task is in progress, and second server also start performing the same task at same time.

Is there any laravel built in way where I can set my default CACHE_DRIVER=file and still use redis for distributed lock so that php artisan cache:clear do not affect the redis cache lock?

0 likes
5 replies
Snapey's avatar

i assume you dont want to artisan down during deploy?

1 like
ajay01's avatar
Level 2

Hi @Snapey yes I don't want to go on maintenance mode during deployment. But I'm curious to know dose artisan down can help in this issue?

ajay01's avatar
Level 2

@Snapey I can cosider this options as final workaround if not get any solution. but ideally I will not prefer artisan down.

Niush's avatar

Although I have not tested it, something like this is possible.

Then setup redis cache connection with different database id:

// config/database.php

return [

    // ...

    'redis' => [

        'cache' => [
            // ...
        ],
        
        'cache_for_schedule' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'username' => env('REDIS_USERNAME'),
            'password' => env('REDIS_PASSWORD'),
            'port' => env('REDIS_PORT', '6379'),
            'database' => env('REDIS_SCHEDULE_DB', '3'), // Unique to other connection
        ],

    ],

];

Now only specifying store with php artisan cache:clear redis_for_schedule will clear the lock.

Typically I wouldn't even run cache:clear on new deployment. But rather version and breaking cache keys. E.g. user:1:orders-list:v2

Please or to participate in this conversation.