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

jlmmns's avatar
Level 12

Redis cache for multiple sites on same server

I'm using Forge with multiple Laravel projects on the same server.

I've noticed that Redis Cache is the same instance for all those projects. When using multiple projects for different versions of the same app, cache tags/keys are getting overwritten.

At first I thought it would be separate according to the Laraval application token. But the app tokens are different and it doesn't make a difference.

Any ideas? (besides using separate servers)

0 likes
5 replies
willvincent's avatar
Level 54

Use a different prefix for each project... In config/cache.php:

   /*
    |--------------------------------------------------------------------------
    | Cache Key Prefix
    |--------------------------------------------------------------------------
    |
    | When utilizing a RAM based store such as APC or Memcached, there might
    | be other applications utilizing the same cache. So, we'll specify a
    | value to get prefixed to all our keys so we can avoid collisions.
    |
    */

    'prefix' => 'laravel',
6 likes
cashlion's avatar

I thought about this approach as well but flush() does not take the prefix into account.

Anyone have a better setup? In my, current, case, it's just staging and production so I can just switch to memcached for one, but I would really prefer to use Redis with both.

datune's avatar

I had the exact same problem, and I solved it as follows:

Redis allows you to have different databases, identified by a number. By default you have 16 databases (remember, this is 0-15, not 1-16!).

Open your config/database.php and specifiy a different database number, and problem solved ;-) As usual, Laravel has us covered!

Sidenote: This also allows us to use different configured connections for the session and cache drivers, which has the added benefit of not clearing out your session data when running cache:clear. Just open up config/session.php and change the connection value to whatever you named your new connection in database.php

Here is how my config/database.php:

    'redis' => [

        'client' => 'predis',

        'cluster' => false,

        'default' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_DB_NUM', 0),
            'read_timeout' => 60,
        ],

        'session' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_DB_SESSION_NUM', 1),
            'read_timeout' => 60,
        ],
    ],

Please note that REDIS_DB_NUM and REDIS_DB_SESSION_NUM are custom env variables that I like to use, there not laravel specific, just in case someone wonders...;-)

15 likes

Please or to participate in this conversation.