Using Redis for Queues for Multiple Laravel Applications on a Single Server
Asked Answered
C

4

14

I have a production laravel application and the staging application running on the same server. I am running redis, which I am using as my queue driver. They are obviously connected to different databases. The question is if jobs that are pushed onto the reds queue from the staging application will interfere with the production db and vice versa.

Chrysalis answered 7/7, 2015 at 15:15 Comment(1)
Would you consider accepting my answer below if you feel that it is most helpful?Fairlead
C
11

You can set the prefix Laravel uses for the Redis cache. By default this is laravel, so all keys are stored as laravel:keyname and Laravel takes care of the logic to add/remove this prefix when saving/getting.

You can set the prefix in config/cache.php. You could leave the main one as laravel and set your staging app prefix to stage or something. This will mean multiple Laravel apps can use the same Redis instance.

Coherence answered 10/2, 2016 at 16:51 Comment(5)
I know you asked this over 6 months ago, but people might Google themselves here or whatever.Coherence
Yes, this is what I did.Chrysalis
This is ok for Redis as cache, although what about Redis as queue driver?Stillman
@Stillman is right. Changing the CACHE_PREFIX env value (which gets used here: github.com/laravel/laravel/commit/…) does not help with collisions of 2 Laravel apps both set to use Redis as queue driver and pointing to the same Redis installation on the same server. I'm still searching for a solution myself.Fairlead
See @Ryan's answer if anyone's still looking for a solution. It works for me - tested using php artisan queue:listen and php artisan tinker to dispatch a test job.Salty
F
17

I had this same problem, and it took me hours to find the solution.

https://laravel.com/docs/5.6/queues#driver-prerequisites says:

In order to use the redis queue driver, you should configure a Redis database connection in your config/database.php configuration file.

And then https://laravel.com/docs/5.6/redis#predis says:

In addition to the default host, port, database, and password server configuration options, Predis supports additional connection parameters that may be defined for each of your Redis servers. To utilize these additional configuration options, add them to your Redis server configuration in the config/database.php configuration file.

Reading the "connection parameters" page, I eventually found https://github.com/nrk/predis/wiki/Client-Options, which says that 'prefix' is a supported option.

So, you can edit your config/database.php configuration file to have:

'redis' => [
        'client' => 'predis',
        'cluster' => false,
        'options'=>[
            'prefix' => env('REDIS_PREFIX', 'YOUR_PREFIX_HERE')
        ],
        'default' => [
            'host'     => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port'     => env('REDIS_PORT', 6379),
            'database' => 0,
        ],
    ],

I'm not sure if you then need to restart Redis or Supervisord.

Fairlead answered 15/5, 2018 at 16:20 Comment(1)
As of Laravel 5.8 (and likely up to at least 6.4) this is the correct answer. If you're having this problem, I'd go one step further and leave the prefix within config/cache.php empty, and make database.redis.options.prefix your only entry point for a prefix string. By doing this, you do lose the : nicety, but you can just add in a : yourself ('prefix' => 'PREFIX:'). If you define both cache.prefix and the databases' options.prefix, your keys will double up and look like PREFIXPREFIX:.Wretch
C
11

You can set the prefix Laravel uses for the Redis cache. By default this is laravel, so all keys are stored as laravel:keyname and Laravel takes care of the logic to add/remove this prefix when saving/getting.

You can set the prefix in config/cache.php. You could leave the main one as laravel and set your staging app prefix to stage or something. This will mean multiple Laravel apps can use the same Redis instance.

Coherence answered 10/2, 2016 at 16:51 Comment(5)
I know you asked this over 6 months ago, but people might Google themselves here or whatever.Coherence
Yes, this is what I did.Chrysalis
This is ok for Redis as cache, although what about Redis as queue driver?Stillman
@Stillman is right. Changing the CACHE_PREFIX env value (which gets used here: github.com/laravel/laravel/commit/…) does not help with collisions of 2 Laravel apps both set to use Redis as queue driver and pointing to the same Redis installation on the same server. I'm still searching for a solution myself.Fairlead
See @Ryan's answer if anyone's still looking for a solution. It works for me - tested using php artisan queue:listen and php artisan tinker to dispatch a test job.Salty
R
7

As of Laravel 5.5.22 the cache prefix is using the APP_NAME environment variable. You can rely on that if that's enough or alternatively you may configure the prefix by using the CACHE_PREFIX environment variable that is not set by default.

<code>CACHE_PREFIX</code>

If you're using Laravel Horizon to manage your queues, you can simply override the HORIZON_PREFIX environment variable to let Horizon differentiate between your applications running side by side.

<code>HORIZON_PREFIX</code>

Radius answered 20/9, 2018 at 22:13 Comment(0)
P
0

You can set the default Laravel queue in .env for the Redis.

REDIS_QUEUE=your_app1_queuename

By default this is 'default',

this used in config/queue.php. This will mean multiple Laravel apps can use the same Redis instance.

Proceleusmatic answered 11/1, 2023 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.