Search code examples
djangogoogle-cloud-rundjango-cachegoogle-cloud-memorystore

Caching on memory store from cloud run create error Unable to create a new session key. It is likely that the cache is unavailable


I have an application deployed on Cloud Run. It runs behind an HTTPS Load balancer. I want it to be able to cache some data using memory store service. I basically followed the documentation to use a serverless vpc connector but this exception keeps poping:

Unable to create a new session key. It is likely that the cache is unavailable.

I am guessing that my cloud run service can't access memorystore.

On Django I have:

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": f"redis://{CHANNEL_REDIS_HOST}:{CHANNEL_REDIS_PORT}/16",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
            "IGNORE_EXCEPTIONS": True,
        },
        "KEY_PREFIX": "api"
    }
}

where CHANNEL_REDIS_HOST is the IP from my memorystore primary endpoint and CHANNEL_REDIS_PORT is the port.

When I run this command:

gcloud redis instances describe instance_name --region region --format "value(authorizedNetwork)"

it returns projects/my_project/global/networks/default.

Then, on the VPC network, I clicked on 'default' and then on 'ADD SUBNET'. I created my subnet with IP Address range 10.0.0.0/28. Maybe the problem comes from this step as I do not get a lot about this all IP Communication thing..

When I run this command:

gcloud compute networks subnets describe my subnet

purpose is PRIVATE as intended and network is https://www.googleapis.com/compute/v1/projects/my_project/global/networks/default.

So I think that my memorystore instance and my subnet are able to connect.

Then, I created a serverless VPC connector, using the same region, the default network and the subnet I just created.

Finally on my service I set the VPC connector to the one I just created and I redeploy using Only route requests to private IPs through the VPC connector option, if I choose Route all traffic through the VPC connector my deployment fails, I think probably because I am behind a load balancer, anyway I do not want to route all traffic to my connector.

And after doing this, I still receive the error mention at the beginning of the message.. Any ideas ?

Thanks


Solution

  • So I think my issue was using the db 16. As the maximum number of database on memorystore is 16 it must be from 0 to 15. Changing it make it works.