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

simonw's avatar

Redis prefix not working with any connections

Hi all,

Laravel 6.18.x

I've just configured Laravel to use Redis for our cache and session store, and would like to ensure the keys are appropriately prefixed. The prefix property in the database redis connections list does not work. All my keys are going in without it.

database.php

'redis' => [

        'client' => 'predis',
        'cluster' => env('REDIS_CLUSTER', false),

        'clusters' => [
            // copy cluster connections here if needed
            // ...
            'options' => [
                'cluster' => 'redis',
            ],
        ],

        'default' => [
            'scheme' => env('REDIS_SCHEME', 'tcp'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
            'prefix' => 'default:',
        ],

        'session' => [
            'scheme' => env('REDIS_SCHEME', 'tcp'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
            'prefix' => 'session:',
        ],

        'cache' => [
            'scheme' => env('REDIS_SCHEME', 'tcp'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
            'prefix' => 'cache:',
        ],

        'options' => [
            'parameters' => [
                'password' => env('REDIS_PASSWORD', null),
                'scheme' => env('REDIS_SCHEME', 'tcp'),
            ],
            'ssl' => ['verify_peer' => false],
        ],

    ],

and in session.php

'driver' => 'redis',

'connection' => 'session',

and in cache.php

'stores' => [
       ...
        'redis' => [
            'driver' => 'redis',
            'connection' => 'cache',
        ],
],

'prefix' => ''

And yet when I check these keys out on redis-cli:

127.0.0.1:6379> KEYS *
1) "b7ad7f2b04bd98f199a2b8c016e37e66c831b866"
2) "b7ad7f2b04bd98f199a2b8c016e37e66c831b866:timer"
3) "dFvLJTT7Bhi1zJu2MxQBDgtIq3A2ILmEokmK51Dm"

As you can see they are not getting prefixed at all. Here we have 2 caches and 1 session. I noticed changing prefix in the cache.php file actually works, but session doesn't have such a property, and why would these be taken in to account instead of the dedicated prefix property in the database.php redis config?

I want them to look like follows:

127.0.0.1:6379> KEYS *
1) "cache:b7ad7f2b04bd98f199a2b8c016e37e66c831b866"
2) "cache:b7ad7f2b04bd98f199a2b8c016e37e66c831b866:timer"
3) "session:dFvLJTT7Bhi1zJu2MxQBDgtIq3A2ILmEokmK51Dm"

Thanks

0 likes
1 reply

Please or to participate in this conversation.