My symfony application has multiple instances that are running in seperate docker containers.
And I have configured my app.cache
to use redis:
framework:
cache:
app: cache.adapter.redis
I have the same prefix_seed
:
framework:
cache:
prefix_seed: 'dev'
As a result I’m getting in redis something like this:
1605259288.470950 [0 172.18.0.28:55044] "MGET" "HnMEIyUlZ+:workers.restart_requested_timestamp"
1605259288.471680 [0 172.18.0.28:55044] "SET" "HnMEIyUlZ+:workers.restart_requested_timestamp" "d:1605259288.471522;"
1605259314.483389 [0 172.18.0.29:42884] "MGET" "8TMgMtnOAG:workers.restart_requested_timestamp"
As you can see from the above 2 different instances are trying to fetch value from redis by the same key workers.restart_requested_timestamp
but the prefix is different even with the same prefix_seed
.
In this example I’m using messenger component, and I want to stop workers running everywhere by stop-workers
command (through the shared redis). But generally speaking this is related to cache configuration.
How to overcome this and tell both applications to use same pool? What is the configuration for this?
2
Answers
Finally, I found the solution. There's an option to create your own cache pool with mapped adapter to it. The main trick here is to pass the tag with
namespace
to your adapter (name is also should becache.pool
):That's it! All your keys in redis will be prefixed with
shared:
. Now you should pass your@cache.redis_shared_pool
to any service you want.And as for the messenger component, we should override the services (but I'm not sure this is the best way):
In the symfony framework bundle’s configuration definition you could find:
prefix_seed is exactly what you are looking for.
Check the documentation here: https://symfony.com/doc/current/reference/configuration/framework.html#prefix-seed