Azure Cache for Redis is a fully-managed, in-memory cache service that is based on the popular open-source Redis cache. It provides high-performance, scalable caching solutions for applications running on Azure. In this tutorial, we will walk you through the steps of creating an Azure Cache for Redis instance, configuring it, and accessing it through both the Azure portal and the Azure CLI.
Prerequisites
Before we begin, make sure you have the following prerequisites:
- An Azure subscription. If you don’t have one, you can sign up for a free trial at https://azure.com/free
- The Azure CLI installed. You can download it from https://docs.microsoft.com/en-us/cli/azure/install-azure-cli
Step 1: Create an Azure Cache for Redis Instance
The first step is to create an Azure Cache for Redis instance. Here are the steps:
- Open the Azure portal at https://portal.azure.com
- Click on “Create a resource” on the left-hand side of the screen
- Search for “Redis Cache” and select it from the list of results
- Click on the “Create” button on the Redis Cache blade
- Fill in the following information:
- Subscription: Select the subscription you want to use
- Resource Group: Choose a resource group or create a new one
- Name: Enter a unique name for your Redis instance
- Location: Choose the location for your Redis instance
- Pricing tier: Choose the pricing tier that meets your needs
- Virtual Network: Choose the virtual network you want to use (optional)
- Click on the “Create” button to create your Redis instance.
Alternatively, you can create the Redis instance using the Azure CLI with the following command:
az redis create --name <redis-name> --resource-group <resource-group-name> --location <location> --sku <sku-name> --vm-size <vm-size>
Step 2: Configure Your Redis Instance
Once your Redis instance is created, you can configure it by setting properties such as the maximum memory size, access keys, and network settings. Here’s how to configure your Redis instance through the Azure portal:
- Navigate to your Redis instance in the Azure portal
- Click on the “Access keys” tab
- Take note of the “Primary” and “Secondary” connection strings, which you will need later to access your Redis instance
- Click on the “Configuration” tab
- Click on the “Edit” button to make changes to your Redis instance configuration
- Configure your Redis instance by setting the desired properties
- Click on the “Save” button to save your changes
Alternatively, you can configure your Redis instance using the Azure CLI with the following command:
az redis update --name <redis-name> --resource-group <resource-group-name> --set <property>=<value>
For example, to set the maximum memory size to 2GB, you can run the following command:
az redis update --name myredis --resource-group myresourcegroup --set properties.redisConfiguration.maxmemory-gb=2
Step 3: Access Your Redis Instance
You can access your Redis instance through the Azure portal or the Azure CLI. Here’s how to do it:
Azure Portal
- Navigate to your Redis instance in the Azure portal
- Click on the “Access keys” tab
- Copy the “Primary” or “Secondary” connection string
- Use the connection string to connect to your Redis instance from your application or tool of choice.
Azure CLI
To access your Redis instance using the Azure CLI, you can use the redis-cli
command-line tool. Here are the steps:
- Open a command prompt or terminal window
- Install the Redis command-line tool by running the following command:
sudo apt-get install redis-tools
Note: This command is for Linux-based systems. If you are using Windows, you can download the Redis command-line tool from the Redis website at https://redis.io/download.
- Get the Redis instance hostname by running the following command:
az redis show --name <redis-name> --resource-group <resource-group-name> --query hostName --output tsv
- Get the Redis instance access key by running the following command:
az redis list-keys --name <redis-name> --resource-group <resource-group-name> --query primaryKey --output tsv
Note: You can also use the secondaryKey
instead of the primaryKey
if you want to use the secondary connection string.
- Connect to your Redis instance using the
redis-cli
command-line tool by running the following command:
redis-cli -h <redis-hostname> -a <redis-access-key>
- You should now be connected to your Redis instance and can start executing Redis commands.
Conclusion
Congratulations! You have successfully created an Azure Cache for Redis instance, configured it, and accessed it through both the Azure portal and the Azure CLI. Azure Cache for Redis is a powerful caching solution that can help improve the performance and scalability of your applications running on Azure. With the knowledge gained from this tutorial, you can now leverage Azure Cache for Redis to build more efficient and responsive applications.