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

elena's avatar

Can't manage redis connections dynamically

I'm working on a system where speed is the number one priority (milliseconds count). Since DB size is obviously inversely proportional to speed a while ago we decided to put a few bigger clients on their own databases, both mysql and redis and we decide which db to use, based on the url they're accessing through.

Now I'm building a microservice that needs to be able to access any client/db, but Lumen is giving me a hard time changing the redis database on runtime. Here's a sample that shows the problem:

    var_dump(config('database.redis.default.database'));  //Output -> 0
    app('cache')->put('lumen_rd_0','val',1); // Gets set in db0
    config(['database.redis.default.database' => 3]);
    var_dump(config('database.redis.default.database'));  //Output -> 3
    app('cache')->put('lumen_rd_3','val',1); // Should get set in db 3, but also goes in db 0

Any thoughts?

0 likes
12 replies
ohffs's avatar

When you say 'Lumen is giving me a hard time' - do you have the same code working ok in the main Laravel app? If so you might want to post to their github issues page :-) I've never tried to change the config dynamically in lumen so can't be much help apart from that I'm afraid :-/

elena's avatar

Yeah, so it's quite crazy actually... and I just figured this out: I can only write to a certain redis db in Laravel if that's the first interaction with the cache.

    Cache::get('any_key');
    \Config::set('database.redis.default.database', 2);
    Cache::put('laravel_key_db2', '2',1);  // Gets set in db 0, which is the one set in the config file

But this works nicely:

    \Config::set('database.redis.default.database', 2);
    Cache::put('laravel_key_db2', '2',1);  // Gets set in db 2

I'll submit a ticket... and maybe even try to fix it and make my first contribution to Laravel :) Let's see... But if someone has any ideas or suggestions in the meantime - please.. do let me know.

By the way... this doesn't happen in Lumen. Lumen always go to the database that was set in the config file.

Thanks

ohffs's avatar

That's an odd one! Hope you get it fixed! :-)

masterpowers's avatar

Ive been into redis lately , but yours was way off beyond ive been tackling... But if u wan performance you should go to phpredis rather than the default predis if your using laravel but in your case lumen , i havent check lumen yet.

You can follow this guide if you intend to use phpredis than predis but off course you will be using laravel https://laracasts.com/discuss/channels/tips/tutorial-guide-installing-php-redis-on-fresh-install-homestead-with-php7

I also i heard that lumen is insanely fast can handle around 3400 request per sections based on taylor otwell benchmarking the framework.

I Also i hear also that if your just building an api choosing lumen would be the best option, coz its slim and fast. but if your gonna use session , cookie , going for laravel would be the best...

You Said By the way... this doesn't happen in Lumen. Lumen always go to the database that was set in the config file.

Pretty much is the same as laravel it is getting info on the config

The Only thing i can see to achieve this is make a service provider that can add/edit config file on the fly cause using different connection is already available

$redis->connect('127.0.0.1', 6379);

Anyway thats all i can help out >.<

masterpowers's avatar

Something pop out of my mind thinking what is the best approach to achieve this

Have you ever thought of using polymorphism?

MarkLL's avatar
MarkLL
Best Answer
Level 7

Hi @elena have you tried the select( database# ) command? e.g.

Redis::command('select', 2);

The (laravel) RedisServiceProvider creates a singleton passing the settings ($app['config']['database.redis']).

As it's in a closure it won't actually be created till it's first use. Hence the behavior you are seeing. Once the PRedis client is created, I doubt that changing the config will make any difference.

1 like
elena's avatar

Thanks masterpowers. I kind of hacked around it, but ended up liking the result better. I changed both the main app and the microservice to use namespaced redis keys, making use of the "prefix". All good now :)

elena's avatar

That worked Mark! Thank you so much!

Redis::command('select', ['database'=>2]);

I was stuck until a bit ago, now I have 2 working solutions. Hmm... what do I do? :D Prefixes or databases?

Since we're grouping the clients by domain, anyway, I just ended up putting \Request::getHttpHost() in the configuration file itself in the main app. Super simple solution that works like a charm. Plus it's a nice way of organizing things, more visual than (integer) database names. And also -> one less thing to manage (no need to keep track of redis database number per company).

What do you think?

Peter Davis's avatar

I think there still might be a need to reset the predis connection. In our application we are forking a process and we'd like to have create a new connection per thread.

Please or to participate in this conversation.