phpsymfonysymfony4azure-redis-cachepredis

How to connect a symfony application to Azure redis cache?


The symfony application is currently connected to a redis cache in a docker container. I have created a new Redis Cache on Azure and I am trying to connect it to my symfony application which is running in another docker container. According to symfony, redis://[pass@][ip|host|socket[:port]][/db-index] is the format of the connection string which is present in the .env file. In my case it is:

REDIS_URL=redis://mypassword@myrediscache.redis.cache.windows.net:6380

The application is unable to connect to the redis server on Azure. The application uses predis v1.1.1 There is a Cacheservice.php service where the predis client is being created.

$this->client  = new Predis\Client($this->containerInterface->getParameter('REDIS_URL'));

For the existing (redis in container) setup, it is working fine.

REDIS_URL = redis://redis:6379

But when I change it to REDIS_URL=redis://mypassword@myrediscache.redis.cache.windows.net:6379, the application is unable to connect. However, if I hardcode the connection values, in a service where the predis client is being created, the application is able to connect.

The below snippet is hard-coded to connect to port 6380 (SSL enabled)

$this->client = new Predis\Client([
            'scheme' => 'tls',
            'ssl' => ['verify_peer' => true],
            'host' => 'myrediscache.redis.cache.windows.net',
            'port' => 6380,
            'password' => 'mypassword'
            ]);

The below snippet is hard-coded to connect to port 6379 (non SSL)

 $this->client = new Predis\Client([
                'host' => 'myrediscache.redis.cache.windows.net',
                'port' => 6380,
                'password' => 'mypassword'
                ]);

Please help me to put these values in the .env file instead of hardcoding it. By the way, here are some additional places where redis is referred. snc_redis.yaml file:

snc_redis:
    clients:
        default:
            type: predis
            alias: default
            dsn: "%env(REDIS_URL)%"

services.yaml file:

parameters:
    REDIS_URL: '%env(resolve:REDIS_URL)%'

I have enabled the 6379 port for the Azure redis cache


Solution

  • I was able to connect by doing the following changes :-

    In CacheService.php (In your case it may differ. Where the predis client is created and defined)

    $host = (string)$this->containerInterface->getParameter('REDIS_HOST');
            $port = (int)$this->containerInterface->getParameter('REDIS_PORT');
            $password = (string)$this->containerInterface->getParameter('REDIS_PASSWORD'); 
            $this->client = new Predis\Client([
                    'host' => $host,
                    'port' => $port,
                    'password' => $password,
                    'scheme' => 'tls',
                    'ssl' => ['verify_peer' => true]
                ]);
    

    Do the following changes in services.yaml

    parameters:
        REDIS_PASSWORD: '%env(resolve:REDIS_PASSWORD)%'
        REDIS_HOST: '%env(resolve:REDIS_HOST)%'
        REDIS_PORT: '%env(resolve:REDIS_PORT)%'
    

    And last but not least, in the .env

    REDIS_PASSWORD=mypassword
    REDIS_HOST=myrediscache.redis.cache.windows.net
    REDIS_PORT=6380
    

    Note: 1) The password shouldn't be encoded into utf-8. You should use the same exact password(primary key that you obtain from the azure portal). 2) Also, if you are going to use the port 6379(non ssl port), remove the scheme and ssl options from the connection.

    Also make sure to remove the REDIS_URL references from services.yaml and .env .

    Hope this works for others as well :-)