Perform data operations in Azure Managed Redis
In this exercise, you create an Azure Managed Redis resource and build a Python console application that performs common data operations using the redis-py library. You work with Redis hash data structures to store and retrieve key-value pairs, manage key expiration with Time-To-Live (TTL) settings, and delete keys from the cache.
Tasks performed in this exercise:
- Download the project starter files
- Create an Azure Managed Redis resource
- Add code to the starter files to complete the console app
- Run the console app to perform data operations
This exercise takes approximately 30 minutes to complete.
Before you start
To complete the exercise, you need:
- An Azure subscription. If you don't already have one, you can sign up for one.
- Visual Studio Code on one of the supported platforms.
- Python 3.12 or greater.
- The latest version of the Azure CLI.
- The Azure CLI redisenterprise extension. You can install it by running the az extension add --name redisenterprise command.
Download project starter files and deploy Azure Managed Redis
In this section you download the starter files for the console app and use a script to deploy Azure Managed Redis to your subscription. The deployment takes 5-10 minutes, so you start it first and complete the app code while it provisions.
-
Open a browser and enter the following URL to download the starter file. The file will be saved in your default download location.
https://github.com/MicrosoftLearning/mslearn-azure-ai/raw/main/downloads/python/amr-data-operations-python.zip -
Copy, or move, the file to a location in your system where you want to work on the project. Then unzip the file into a folder.
-
Launch Visual Studio Code (VS Code) and select File > Open Folder... in the menu, then choose the folder containing the project files.
-
Open the azdeploy.py deployment script and change the two values at the top of the script to meet your needs, then save your changes. Note: Do not change anything else in the script.
"<your-resource-group-name>" # Resource Group name "<your-azure-region>" # Azure region for the resources -
In the menu bar select Terminal > New Terminal to open a terminal window in VS Code.
-
Run the following command to login to your Azure account. Answer the prompts to select your Azure account and subscription for the exercise.
az login -
Run the following command to ensure your subscription has the necessary resource provider for Azure Managed Redis.
az provider register --namespace Microsoft.Cache -
Run the following command to install the redisenterprise extension for Azure CLI.
az extension add --name redisenterprise -
Run the following command in the terminal to launch the deployment script.
python azdeploy.py -
When the script is running, enter 1 to launch the 1. Create Azure Managed Redis resource option.
This option creates the resource group if it doesn't already exist, then deploys Azure Managed Redis. The script waits for the deployment to finish and reports the result in the terminal.
-
Leave the deployment script running and continue to the next section to complete the app code while Azure Managed Redis provisions. Check the terminal periodically for completion or error messages.
Complete the app
In this section you add code to the main.py script to complete the console app. You run the app later in the exercise, after you confirm the Azure Managed Redis resource is fully deployed and create the .env file.
- Open the main.py file to begin adding code.
Note: The code blocks you add to the application should align with the comment for that section of the code.
Add the client connection
In this section, you add code to establish a connection to Azure Managed Redis using the redis-py library. The code reads the Redis endpoint from an environment variable and uses DefaultAzureCredential through the redis-entraid credential provider so the client authenticates with Microsoft Entra ID and refreshes its token automatically.
Tip: To maintain proper code indentation, paste the code flush with the left margin (column 1), select all of the pasted lines, and press Tab to align the block with the BEGIN / END markers. Press Shift+Tab to outdent if needed.
-
Locate the # BEGIN CONNECTION CODE SECTION comment and add the following code under the comment. Be sure to check for proper code alignment.
try: # Azure Managed Redis using Microsoft Entra ID authentication redis_host = os.getenv("REDIS_HOST") # create_from_default_azure_credential uses DefaultAzureCredential to # acquire and refresh a Microsoft Entra token for Redis. credential_provider = create_from_default_azure_credential( ("https://redis.azure.com/.default",), ) r = redis.Redis( host=redis_host, port=10000, # Azure Managed Redis uses port 10000 ssl=True, decode_responses=True, # Decode responses to strings credential_provider=credential_provider, socket_timeout=30, # Add timeout for better reliability socket_connect_timeout=30, ) print(f"Connected to Redis at {redis_host}") input("\nPress Enter to continue...") return r
Add code to store and retrieve data
In this section, you add code to work with Redis hash data structures using the hset and hgetall commands. The hset method stores multiple field-value pairs under a single key, while hgetall retrieves all fields and values for a given key.
-
Locate the # BEGIN STORE AND RETRIEVE CODE SECTION comment and add the following code under the comment. Be sure to check for proper code alignment.
def store_hash_data(r, key, value) -> None: """Store a hash data in Redis""" clear_screen() print(f"Storing hash data for key: {key}") result = r.hset(key, mapping=value) # Store hash data if result > 0: # New fields were added print(f"Data stored successfully under key '{key}' ({result} new fields added)") else: print(f"Data updated successfully under key '{key}' (all fields already existed)") input("\nPress Enter to continue...") def retrieve_hash_data(r, key) -> None: """Retrieve hash data from Redis""" clear_screen() print(f"Retrieving hash data for key: {key}") retrieved_value = r.hgetall(key) # Retrieve hash data if retrieved_value: print("\nRetrieved hash data:") for field, value in retrieved_value.items(): print(f" {field}: {value}") else: print(f"Key '{key}' does not exist.") input("\nPress Enter to continue...")
Add code to set and retrieve expiration
In this section, you add code to manage key expiration using the expire and ttl commands. The expire method sets a Time-To-Live (TTL) on a key, causing it to automatically expire after the specified number of seconds, while ttl retrieves the remaining time before a key expires.
-
Locate the # BEGIN EXPIRATION CODE SECTION comment and add the following code under the comment. Be sure to check for proper code alignment.
def set_expiration(r, key) -> None: """Set an expiration time for a key""" clear_screen() print("Set expiration time for a key") # Set expiration time, 1 hour equals 3600 seconds expiration = int(input("Enter expiration time in seconds (default 3600): ") or 3600) result = r.expire(key, expiration) # Set expiration time if result: print(f"Expiration time of {expiration} seconds set for key '{key}'") else: print(f"Key '{key}' does not exist. Expiration not set.") input("\nPress Enter to continue...") def retrieve_expiration(r, key) -> None: """Retrieve TTL of a key""" clear_screen() print(f"Retrieving the current TTL of {key}...") ttl = r.ttl(key) # Get current TTL if ttl == -2: # Key does not exist print(f"\nKey '{key}' does not exist.") elif ttl == -1: # No expiration set print(f"\nKey '{key}' has no expiration set (persists indefinitely).") else: print(f"\nCurrent TTL for '{key}': {ttl} seconds") input("\nPress Enter to continue...")
Add code to delete data
In this section, you add code to remove keys from Redis using the delete command. The delete method permanently removes a key and its associated value from the cache, freeing up memory and ensuring the data is no longer accessible.
-
Locate the # BEGIN DELETE CODE SECTION comment and add the following code under the comment. Be sure to check for proper code alignment.
def delete_key(r, key) -> None: """Delete a key""" clear_screen() print(f"Deleting key: {key}...") result = r.delete(key) # Delete the key if result == 1: print(f"Key '{key}' deleted successfully.") else: print(f"Key '{key}' does not exist.") input("\nPress Enter to continue...") -
Save your changes to the main.py file.
Verify resource deployment
In this section you verify the Azure Managed Redis deployment, create the database, configure Microsoft Entra ID access, and create the environment variable files with the Redis endpoint.
-
Return to the terminal running the deployment script. After the script reports that the Azure Managed Redis resource was created successfully, select Enter to return to the deployment menu.
-
When the deployment menu appears, enter 2 to run the 2. Check deployment status option. If the status shows Succeeded, proceed to the next step. If not, then wait a few minutes and try the option again.
-
Enter 3 to run the 3. Create database and configure access option. This creates the database, assigns a Microsoft Entra ID data access policy to your account so the app can connect using your identity, and creates the .env and .env.ps1 files with the REDIS_HOST endpoint.
-
Review the environment variable file for your shell to verify the values are present, then enter 4 to exit the deployment script.
Configure the Python environment
In this section, you create the Python environment and install the dependencies after completing the Azure deployment.
-
Run the following command in the VS Code terminal to create the Python environment.
python -m venv .venv -
Run the following command to activate the Python environment. Note: On Linux/macOS, use the Bash command. On Windows, use the PowerShell command. If using Git Bash on Windows, use source .venv/Scripts/activate.
Bash
source .venv/bin/activatePowerShell
.\.venv\Scripts\Activate.ps1 -
Run the following command in the VS Code terminal to install the dependencies.
pip install -r requirements.txt
Run the console app
In this section, you run the completed console application to perform various Redis data operations. The app provides a menu-driven interface that lets you store hash data, retrieve values, manage key expiration, and delete keys.
-
Run the appropriate command to load the environment variables into your terminal session from the file created by the deployment script.
Bash
source .envPowerShell
. .\.env.ps1Note: Keep the terminal open. If you close it and create a new terminal, you need to run this command again to reload the environment variables.
-
Run the following command in the terminal to start the console app. Refer to the commands from earlier in the exercise to activate the environment, if needed, before running the command.
python main.py -
The app has the following options. Select the 1. Store hash data to get started.
1. Store hash data 2. Retrieve hash data 3. Set expiration 4. Retrieve expiration (TTL) 5. Delete key 6. Exit -
Select the remaining options in order to run the different operations.
Note: You can run the options in any order you choose. For example, after storing the hash data you can retrieve the expiration information to learn there is no expiration set on the key.
The mock hash data used in the app is defined in the beginning of the main() function. You can update the code to use a different key, or add more values to the hash data.
Clean up resources
Now that you finished the exercise, you should delete the cloud resources you created to avoid unnecessary resource usage.
-
Run the following command in the VS Code terminal to delete the resource group, and all resources in the group. Replace <rg-name> with the name you choose earlier in the exercise. The command will launch a background task in Azure to delete the resource group.
az group delete --name <rg-name> --no-wait --yes
CAUTION: Deleting a resource group deletes all resources contained within it. If you chose an existing resource group for this exercise, any existing resources outside the scope of this exercise will also be deleted.
Troubleshooting
If you encounter issues while completing this exercise, try the following troubleshooting steps:
Verify Azure Managed Redis resource deployment
- Navigate to the Azure portal and locate your resource group.
- Confirm that the Azure Managed Redis resource shows a Provisioning State of Succeeded.
- Check that the resource has Public network access enabled.
Check code completeness and indentation
- Ensure all code blocks were added to the correct sections in main.py between the appropriate BEGIN/END comment markers.
- Verify that Python indentation is consistent (use spaces, not tabs) and that all code aligns properly within functions.
- Confirm that no code was accidentally removed or modified outside the designated sections.
Verify environment variables
- Check that both the .env and .env.ps1 files exist in the project folder and contain a valid REDIS_HOST value.
- Ensure both files are in the same directory as main.py.
Check authentication and access
- Confirm you are logged in to Azure CLI by running az account show.
- Ensure the deployment script's Create database and configure access option completed successfully so your account has a data access policy on the database.
- If the app reports an authentication error, wait a moment and try again, as the access policy assignment can take a short time to take effect.
Check Python environment and dependencies
- Confirm the virtual environment is activated before running the app.
- Verify that all packages from requirements.txt were installed successfully by running pip list.