0
0
AzureHow-ToBeginner · 4 min read

How to Use Redis Cache in Azure: Simple Guide

To use Redis Cache in Azure, first create an Azure Cache for Redis instance in the Azure portal. Then connect your application to this cache using the provided connection string and use Redis commands to store and retrieve data quickly.
📐

Syntax

Using Azure Redis Cache involves these steps:

  • Create Cache: Set up an Azure Cache for Redis instance.
  • Connect: Use the connection string in your app.
  • Commands: Use Redis commands like SET and GET to store and retrieve data.
javascript
const redis = require('redis');

// Connect to Azure Redis Cache
const client = redis.createClient({
  url: 'rediss://:<access_key>@<hostname>:6380',
  socket: {
    tls: true,
    rejectUnauthorized: false
  }
});

async function run() {
  await client.connect();

  // Set a value
  await client.set('key', 'value');

  // Get a value
  const value = await client.get('key');
  console.log(value);

  await client.disconnect();
}

run();
💻

Example

This example shows how to connect to Azure Redis Cache and store then retrieve a value using Node.js.

javascript
import { createClient } from 'redis';

async function run() {
  const client = createClient({
    url: 'rediss://:<your_access_key>@<your_redis_hostname>:6380'
  });

  client.on('error', (err) => console.log('Redis Client Error', err));

  await client.connect();

  await client.set('greeting', 'Hello from Azure Redis!');
  const message = await client.get('greeting');

  console.log(message);

  await client.disconnect();
}

run();
Output
Hello from Azure Redis!
⚠️

Common Pitfalls

  • Using the wrong connection string format or missing TLS causes connection failures.
  • Not enabling TLS (secure connection) can block access since Azure Redis requires it.
  • Forgetting to handle async connection and commands leads to errors or no data returned.
  • Not closing the Redis client can cause resource leaks.
javascript
/* Wrong way: Missing TLS and wrong URL format */
const clientWrong = redis.createClient({
  url: 'redis://<hostname>:6379' // No access key and no TLS
});

/* Right way: Use TLS and full connection string */
const clientRight = redis.createClient({
  url: 'rediss://:<access_key>@<hostname>:6380'
});
📊

Quick Reference

StepDescription
Create Redis CacheUse Azure portal to create a Redis Cache instance.
Get Connection StringFind the access key and hostname in Azure portal.
Connect in AppUse Redis client libraries with TLS enabled.
Use CommandsStore and retrieve data with SET and GET commands.
Close ConnectionDisconnect client to free resources.

Key Takeaways

Create an Azure Cache for Redis instance in the Azure portal before using it.
Always connect using the full connection string with TLS enabled for security.
Use Redis commands like SET and GET to store and retrieve cached data.
Handle asynchronous calls properly to avoid errors and ensure data is fetched.
Close the Redis client connection when done to prevent resource leaks.