Cloud Infrastructure Community's Space

Azure Caches for Redis with Node.JS

Azure Caches for Redis provides an in-memory data store based on the Redis software. Azure Cache for Redis is based on the popular open-source Redis cache. It gives you access to a secure, dedicated Redis server, managed by Microsoft and accessible from any application within Azure. For a more detailed overview, see Azure Cache for Redis.

In this article, we’re going to use Azure Caches for Redis as remote Redis server for our Node.js application. In the next article, we’ll learn How to use internal Azure Caches for Redis.

Prerequisites

Table of Contents

  1. Create Azure resource groups
  2. Create App service
  3. Create Azure Caches for Redis
  4. Update App service configurations
  5. Basic Azure Caches for Redis operations

1. Create Azure resource groups

WebApp_RG="simple-express"
WebApp_Name="simple-express-1"
WebApp_PlanName="simple-express-plan"
ImageName="hidetran/simple-express:latest"

CacheRG="simple-redis"
CacheName="simple-redis"
Sku="basic"
Size="C0"
Location="northeurope"

# create az resource group for WebApp
az group create --name $WebApp_RG --location $Location
# create az resource group for Azure Caches for Redis
az group create --name $CacheRG --location $Location

2. Create Azure app service

# create az app service plan & webapp
az appservice plan create -n $WebApp_PlanName -g $WebApp_RG --is-linux
az webapp create -n $WebApp_Name --plan $WebApp_PlanName -g $WebApp_RG -i $ImageName

3. Create Azure caches for Redis

az redis create --name $CacheName --resource-group $CacheRG --location $Location --sku $Sku --vm-size $Size

# get Azure Caches for Redis details
az redis show --name $CacheName --resource-group $CacheRG 

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

# Retrieve the keys for an Azure Redis Cache instance
primaryKey=$(az redis list-keys --name $CacheName --resource-group $CacheRG --query [primaryKey] --output tsv | tr -d '\r')

4. Update App service configurations

# config app settings
az webapp config appsettings set -g $WebApp_RG -n $WebApp_Name --settings ENV_NAME=production
az webapp config appsettings set -g $WebApp_RG -n $WebApp_Name --settings CACHES_URL=${caches[0]}
az webapp config appsettings set -g $WebApp_RG -n $WebApp_Name --settings CACHES_PORT=${caches[1]}
az webapp config appsettings set -g $WebApp_RG -n $WebApp_Name --settings CACHES_KEY=$primaryKey

az webapp restart -n $WebApp_Name -g $WebApp_RG

5. Basic Azure Caches for Redis operations

Let’s take a look at our simple-express, we have some methods which are /caches/verify, caches/create, caches/list

app.get("/caches/create", async (req, res) => {
  if (CACHES_URL != null && CACHES_KEY != null) {
    var cacheConnection = redis.createClient({ 
      url: "rediss://" + CACHES_URL + ":" + CACHES_PORT, 
      password: CACHES_KEY 
    })
    await cacheConnection.connect()
    for (let index = 0; index < 20; index++) {
      var keyData = `Index_${index}`
      var valueData = `The index ${index} ${new Date().toISOString()}`

      await cacheConnection.set(keyData, valueData)
      logger.info(`Key Index_${index} has been created on Azure Caches for Redis`)
    }
    msg = {
      error: false,
      message: 'Index_0 to Index_19 keys has been created on Azure Caches for Redis'
    }
    res.set('Content-Type', 'application/json');
    res.send(JSON.stringify(msg, null, 4))
  }
})

Leave a Reply

Your email address will not be published. Required fields are marked *

Press ESC to close