How to use Azure Redis Cache with Python

This topic shows you how to get started with Azure Redis Cache using Python.


Install redis-py.

Create a Redis cache on Azure

To create a cache, first sign in to the Azure portal, and click Create a resource > Databases > Redis Cache.


If you don't have an Azure account, you can Open an Azure account for free in just a couple of minutes.

New cache


In addition to creating caches in the Azure portal, you can also create them using Resource Manager templates, PowerShell, or Azure CLI.

In New Redis Cache, specify the desired configuration for the cache.

Create cache

  • In Dns name, enter a unique cache name to use for the cache endpoint. The cache name must be a string between 1 and 63 characters and contain only numbers, letters, and the - character. The cache name cannot start or end with the - character, and consecutive - characters are not valid.
  • For Subscription, select the Azure subscription that you want to use for the cache. If your account has only one subscription, it is automatically selected and the Subscription drop-down isn't displayed.
  • In Resource group, select or create a resource group for your cache. For more information, see Using Resource groups to manage your Azure resources.
  • Use Location to specify the geographic location in which your cache is hosted. For the best performance, Microsoft strongly recommends that you create the cache in the same region as the cache client application.
  • Use Pricing tier to select the desired cache size and features.
  • Redis cluster allows you to create caches larger than 53 GB and to shard data across multiple Redis nodes. For more information, see How to configure clustering for a Premium Azure Redis Cache.
  • Redis persistence offers the ability to persist your cache to an Azure Storage account. For instructions on configuring persistence, see How to configure persistence for a Premium Azure Redis Cache.
  • Virtual Network provides enhanced security and isolation by restricting access to your cache to only those clients within the specified Azure Virtual Network. You can use all the features of VNet such as subnets, access control policies, and other features to further restrict access to Redis. For more information, see How to configure Virtual Network support for a Premium Azure Redis Cache.
  • By default, non-SSL access is disabled for new caches. To enable the non-SSL port, check Unblock port 6379 (not SSL encrypted).

Once the new cache options are configured, click Create. It can take a few minutes for the cache to be created. To check the status, you can monitor the progress on the startboard. After the cache has been created, your new cache has a Running status and is ready for use with default settings.

Cache created

Retrieve the host name and access keys

To connect to an Azure Redis Cache instance, cache clients need the host name, ports, and keys of the cache. Some clients may refer to these items by slightly different names. You can retrieve this information in the Azure portal or by using command-line tools such as Azure CLI.

Retrieve host name, ports, and access keys using the Azure Portal

To retrieve host name, ports, and access keys using the Azure Portal, browse to your cache in the Azure portal and click Access keys and Properties in the Resource menu.

Redis cache settings

Retrieve host name, ports, and access keys using Azure CLI

To retrieve the host name and ports using Azure CLI 2.0 you can call az redis show, and to retrieve the keys you can call az redis list-keys. The following script calls these two commands and echos the hostname, ports, and keys to the console.


# Retrieve the hostname, ports, and keys for contosoCache located in contosoGroup

# Retrieve the hostname and ports for an Azure Redis Cache instance
redis=($(az redis show --name contosoCache --resource-group contosoGroup --query [hostName,enableNonSslPort,port,sslPort] --output tsv))

# Retrieve the keys for an Azure Redis Cache instance
keys=($(az redis list-keys --name contosoCache --resource-group contosoGroup --query [primaryKey,secondaryKey] --output tsv))

# Display the retrieved hostname, keys, and ports
echo "Hostname:" ${redis[0]}
echo "Non SSL Port:" ${redis[2]}
echo "Non SSL Port Enabled:" ${redis[1]}
echo "SSL Port:" ${redis[3]}
echo "Primary Key:" ${keys[0]}
echo "Secondary Key:" ${keys[1]}

For more information about this script, see Get the hostname, ports, and keys for Azure Redis Cache. For more information on Azure CLI 2.0, see Install Azure CLI 2.0 and Get started with Azure CLI 2.0.

Enable the non-SSL endpoint

Some Redis clients don't support SSL, and by default the non-SSL port is disabled for new Azure Redis Cache instances. At the time of this writing, the redis-py client doesn't support SSL.

To enable the non-SSL port, browse to your cache in the Azure portal and click Advanced settings. Click No for Allow access only via SSL on the Advanced settings blade and then click Save.

Redis cache settings

Add something to the cache and retrieve it

>>> import redis
>>> r = redis.StrictRedis(host='<name>',
      port=6380, db=0, password='<key>', ssl=True)
>>> r.set('foo', 'bar')
>>> r.get('foo')

Replace <name> with your cache name and key with your access key.