1

Short:

In Redis Clusters, you are only allowed to use one Database.

What happens if you call the command php artisian cache:clear?

Details:

Normally, when you are calling the command

php artisian cache:clear

your session and queue Redis database are not deleted, because of the use of different databases:

    'default' => [
        'url' => env('REDIS_URL'),
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', '6379'),
        'database' => env('REDIS_DB', '0'),
    ],

    'cache' => [
        'url' => env('REDIS_URL'),
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', '6379'),
        'database' => env('REDIS_CACHE_DB', '1'),
    ],

You can see that Redis uses the cache connection and cache connection of Redis has set the database to 1 by default.

Now if I want to migrate to Redis Clusters, this part is getting tricky. Because Redis Clusters only allows you to have one database.

According to this blog post my configuration has to look like this:

//the redis driver
'redis' => [
    'client' => env('REDIS_CLIENT', 'phpredis'),
    'options' => [
        'cluster' => 'redis',
    ],
    //the `default` connection of the redis driver
    //Note: The Laravel Redis facade is hard coded to use this connection unless its connection is explicitly specified
    'default' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', '6379'),
        'database' => '0',
        `prefix` => `d:`
    ],
    //the `cache` connection of the redis driver
    'cache' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', '6379'),
        'database' => '0',
        `prefix` => `c:`
    ],
    //the `queue` connection of the redis driver
    'queue' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', '6379'),
        'database' => '0',
        `prefix` => 'q:',
    ],
    //the `session` connection of the redis driver
    'session' => [
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', '6379'),
        'database' => '0',
        #since config/session.php does not have an app level prefix we can change
        #this prefx to `myapp:s:` if we share redis server between apps
        `prefix` => `s:`
    ],
],

As you might see, the database is set to 0. What happens when I call the command php artisian cache:clear?

If you look at the official documentations https://laravel.com/docs/7.x/redis#configuration and scroll down to the part: Configuring Clusters you see that database is also set to 0. However, one thing is unclear. It uses the array key clusters.

So my question that arises with that: How do you cache:clear your application when you use Redis Clusters?

Philipp Mochine
  • 4,351
  • 10
  • 36
  • 68
  • Do you mean how do you clear cache without deleting any session or queue data ? "Redis Cluster does not support multiple databases like the stand alone version of Redis. There is just database 0 and the SELECT command is not allowed." Because of that it will be impossible to delete with a single command without deleting at all. – Ersoy Aug 26 '20 at 21:17
  • @Ersoy exactly this is what I mean. So you are saying it is just not possible. You are not able to delete the cache with redis clusters without deleting sessions and queue data, right? – Philipp Mochine Aug 27 '20 at 07:41
  • 1
    Yes. I think you need to run some delete commands(on different nodes) with prefixes or use different redis instances for session and queue. – Ersoy Aug 27 '20 at 15:17
  • @Ersoy cache:clear fires the command 'flushdb'. Not sure how you can delete only with specific prefixes. Could you point me documentation or something? But thanks nonetheless! – Philipp Mochine Aug 27 '20 at 15:26
  • 1
    https://stackoverflow.com/questions/4006324/how-to-atomically-delete-keys-matching-a-pattern-using-redis there are some working examples under this question. I used something like this `redis-cli KEYS "*:foo:*" | xargs redis-cli DEL` – Ersoy Aug 27 '20 at 15:28
  • @PhilippMochine I am facing the exact issue , how did u fix this ? – Manish Chauhan Jun 30 '22 at 10:05

0 Answers0