I have a django application that I have just migrated to MS Azure from Digital Ocean, and the app runs perfectly on Azure. However, I’m struggling to implement a cache backend with an Azure Cache for Redis service.
I have the following configuration in settings.py:
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": f"redis://<my_redis_service_name>.redis.cache.windows.net:6380,password={secrets.AZURE_REDIS_PASSWORD},ssl=True,abortConnect=False",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"COMPRESSOR": "django_redis.compressors.zlib.ZlibCompressor",
},
}
}
I have replaced my redis service name with <my_redis_service_name>
.
With this setup and DEBUG=True, I receive the error:
ValueError at /accounts/login/
Redis URL must specify one of the following schemes (redis://, rediss://, unix://)
Strange since redis://
is in the url. I have verified the keys, and ensured the redis service firewall accepts my VM’s IP4 address, as well as making sure port 6380 is open on the VM. I have also tried various permutations of the connection string, such as:
"LOCATION": f'redis://:{secrets.AZURE_REDIS_PASSWORD}@<my_redis_service_name>.redis.cache.windows.net:6380,ssl=True,abortConnect=False',
No luck. If it’s relevant, I’m using Django 4.0.7, Python 3.8, django-cachalot, and jazzband’s django-redis package. Azure Cache for Redis states that it’s using Redis version 6.0.14. For what it’s worth, my development environment works fine with a local daemonized redis cache at "LOCATION": "redis://127.0.0.1:6379/1",
. Any help appreciated.
2
Answers
After trying many, many permutations of the connection string and options, I finally hit on a version that worked:
Note the
rediss://
schema with the extra 's' for SSL, which is analogous toHTTPS
, and the:
before{secrets.AZURE_REDIS_PASSWORD}
, which would normally follow a username (there is no username on Azure redis). Hopefully this helps someone, because as of this writing I have not found documented examples of django redis configs using Azure redis. Also added/0
after the port to denote the default redis database.Since
SSL is mandatory
on AZURE REDIS you need to do something like this:NOTE: As mentioned in django_redis Readme we use
rediss://
if we want a SSL connection