0
0
Azurecloud~5 mins

Cache-aside pattern in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes data is slow to get from a database. The cache-aside pattern helps by storing data in a fast cache only when needed. This way, your app checks the cache first and only goes to the database if the data is missing.
When you want to speed up reading data that does not change often.
When your app reads the same data many times and you want to avoid slow database calls.
When you want to keep your cache updated only when data is requested.
When you want to reduce load on your database during peak times.
When you want to control exactly when data is added or removed from the cache.
Config File - azure_cache_aside_example.json
azure_cache_aside_example.json
{
  "name": "myCache",
  "location": "eastus",
  "properties": {
    "sku": {
      "name": "Standard",
      "family": "C",
      "capacity": 1
    },
    "enableNonSslPort": false
  },
  "type": "Microsoft.Cache/Redis"
}

This JSON file defines an Azure Redis Cache instance named myCache in the East US region.

The sku section sets the cache to Standard tier with a small capacity, suitable for development or small workloads.

The enableNonSslPort is set to false to enforce secure connections.

Commands
This command creates an Azure Redis Cache instance named 'myCache' in the 'myResourceGroup' resource group located in East US. It uses the Standard SKU with a small VM size for caching.
Terminal
az redis create --name myCache --resource-group myResourceGroup --location eastus --sku Standard --vm-size C1
Expected OutputExpected
{ "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Cache/Redis/myCache", "location": "eastus", "name": "myCache", "properties": { "provisioningState": "Succeeded", "redisVersion": "6", "sku": { "name": "Standard", "family": "C", "capacity": 1 } }, "type": "Microsoft.Cache/Redis" }
--name - Sets the name of the Redis Cache instance.
--resource-group - Specifies the Azure resource group to contain the cache.
--sku - Chooses the pricing tier and features of the cache.
This command retrieves details about the Redis Cache instance to confirm it was created and is ready to use.
Terminal
az redis show --name myCache --resource-group myResourceGroup
Expected OutputExpected
{ "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Cache/Redis/myCache", "location": "eastus", "name": "myCache", "properties": { "provisioningState": "Succeeded", "redisVersion": "6", "sku": { "name": "Standard", "family": "C", "capacity": 1 } }, "type": "Microsoft.Cache/Redis" }
--name - Specifies the Redis Cache instance name.
--resource-group - Specifies the resource group of the cache.
This command gets the access keys needed to connect your app to the Redis Cache instance.
Terminal
az redis list-keys --name myCache --resource-group myResourceGroup
Expected OutputExpected
{ "primaryKey": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "secondaryKey": "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" }
--name - Specifies the Redis Cache instance name.
--resource-group - Specifies the resource group of the cache.
Key Concept

If you remember nothing else from this pattern, remember: check the cache first and only go to the database if the data is missing, then update the cache.

Common Mistakes
Not checking the cache before querying the database.
This defeats the purpose of caching and causes unnecessary database load.
Always check the cache first; only query the database if the cache does not have the data.
Not updating the cache after fetching data from the database.
The cache remains empty or stale, so future requests still hit the database.
After getting data from the database, store it in the cache for future fast access.
Not handling cache expiration or invalidation.
Cached data can become outdated, causing your app to use stale information.
Set expiration times or update the cache when data changes to keep it fresh.
Summary
Create an Azure Redis Cache instance to store frequently accessed data.
Use commands to verify the cache is ready and get access keys for your app.
Implement the cache-aside pattern by checking the cache first, then the database, and updating the cache with new data.