How to use Azure Redis Cache with Node.js

Azure Redis Cache gives you access to a secure, dedicated Redis cache, managed by Microsoft. Your cache is accessible from any application within Microsoft Azure.

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

Prerequisites

Install node_redis:

npm install redis

This tutorial uses node_redis. For examples of using other Node.js clients, see the individual documentation for the Node.js clients listed at Node.js Redis clients.

Create a Redis cache on Azure

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

Note

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

New cache

Note

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

In the New Redis Cache blade, 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 will be automatically selected and the Subscription drop-down will not be 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.

#/bin/bash

# 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.

Connect to the cache securely using SSL

The latest builds of node_redis provide support for connecting to Azure Redis Cache using SSL. The following example shows how to connect to Azure Redis Cache using the SSL endpoint of 6380. Replace <name> with the name of your cache and <key> with either your primary or secondary key as described in the previous Retrieve the host name and access keys section.

 var redis = require("redis");

  // Add your cache name and access key.
var client = redis.createClient(6380,'<name>.redis.cache.windows.net', {auth_pass: '<key>', tls: {servername: '<name>.redis.cache.windows.net'}});
Note

The non-SSL port is disabled for new Azure Redis Cache instances. If you are using a different client that doesn't support SSL, see How to enable the non-SSL port.

Add something to the cache and retrieve it

The following example shows you how to connect to an Azure Redis Cache instance, and store and retrieve an item from the cache. For more examples of using Redis with the node_redis client, see http://redis.js.org/.

 var redis = require("redis");

  // Add your cache name and access key.
var client = redis.createClient(6380,'<name>.redis.cache.windows.net', {auth_pass: '<key>', tls: {servername: '<name>.redis.cache.windows.net'}});

client.set("key1", "value", function(err, reply) {
        console.log(reply);
    });

client.get("key1",  function(err, reply) {
        console.log(reply);
    });

Output:

OK
value

Next steps