Skip to content
Get Started for Free

Cache for Redis

Azure Cache for Redis is a managed in-memory data store based on Redis, offered in the Basic, Standard, and Premium tiers. Applications use it as a distributed cache, a session store, and a message broker. For more information, see What is Azure Cache for Redis?

LocalStack for Azure provides a local environment for building and testing applications that use Azure Cache for Redis. Each cache you create is backed by a real Redis server running in its own container, so your application runs its own Redis commands, with the same clients, against a real Redis engine. The cache serves TLS on its SSL port, and optionally plaintext on its non-TLS port, authenticates clients with the cache’s access keys, and enforces its firewall rules and public network access setting.

The supported APIs are listed in the API Coverage section.

This guide is designed for users new to Azure Cache for Redis and assumes basic knowledge of the Azure CLI, our lstk az proxy, and redis-cli.

Launch LocalStack using your preferred method. For more information, see Introduction to LocalStack for Azure.

If you start the emulator with the docker CLI or Docker Compose, publish TCP ports 4510-4559 on the same host ports and mount /var/run/docker.sock:/var/run/docker.sock so the emulator can start its Redis containers. The lstk CLI configures these settings automatically.

Once the container is running, enable Azure CLI interception by running:

Terminal window
lstk az start-interception

This command points the az CLI away from the public Azure management REST API and toward the LocalStack for Azure emulator API. To revert this configuration, run:

Terminal window
lstk az stop-interception

This reconfigures the az CLI to send commands to the official Azure management REST API.

Create a resource group to contain your cache:

Terminal window
az group create \
--name rg-redis-demo \
--location westeurope
Output
{
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-redis-demo",
"location": "westeurope",
"name": "rg-redis-demo",
"properties": {
"provisioningState": "Succeeded"
},
...
}

Create a Basic C0 cache that requires TLS 1.2 or later:

Terminal window
az redis create \
--name redis-demo-cache \
--resource-group rg-redis-demo \
--location westeurope \
--sku Basic \
--vm-size c0 \
--minimum-tls-version 1.2
Output
{
"enableNonSslPort": false,
"hostName": "redis-demo-cache.redis.azure.localhost.localstack.cloud",
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-redis-demo/providers/Microsoft.Cache/Redis/redis-demo-cache",
"minimumTlsVersion": "1.2",
"name": "redis-demo-cache",
"port": 6379,
"provisioningState": "Succeeded",
"publicNetworkAccess": "Enabled",
"sku": {
"capacity": 0,
"family": "C",
"name": "Basic"
},
"sslPort": 4510,
...
}

Show the cache’s host name and ports:

Terminal window
az redis show \
--name redis-demo-cache \
--resource-group rg-redis-demo \
--query "{hostName:hostName, sslPort:sslPort, enableNonSslPort:enableNonSslPort, sku:sku.name, provisioningState:provisioningState}"
Output
{
"enableNonSslPort": false,
"hostName": "redis-demo-cache.redis.azure.localhost.localstack.cloud",
"provisioningState": "Succeeded",
"sku": "Basic",
"sslPort": 4510
}

The host name is an emulator host name rather than redis-demo-cache.redis.cache.windows.net, and the SSL port is a port the emulator assigns to the cache rather than 6380.

Capture the host name, the SSL port, and the primary access key. redis-cli reads its password from the REDISCLI_AUTH environment variable:

Terminal window
export REDIS_HOST=$(az redis show --name redis-demo-cache --resource-group rg-redis-demo --query hostName --output tsv)
export REDIS_PORT=$(az redis show --name redis-demo-cache --resource-group rg-redis-demo --query sslPort --output tsv)
export REDISCLI_AUTH=$(az redis list-keys --name redis-demo-cache --resource-group rg-redis-demo --query primaryKey --output tsv)

Connect over TLS, then write and read a key:

Terminal window
redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" --tls PING
redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" --tls SET greeting "Hello from LocalStack"
redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" --tls GET greeting
Output
PONG
OK
Hello from LocalStack

The cache presents the LocalStack public certificate, whose *.redis.azure.localhost.localstack.cloud entry covers the host name, so redis-cli and other clients verify the server against the public roots they already trust, as they do against Azure. When LocalStack cannot download that certificate, for example with SKIP_SSL_CERT_DOWNLOAD=1 or without outbound network access, the cache presents a certificate issued by the LocalStack root certificate authority instead, and a client needs that authority, for example with redis-cli --cacert.

Regenerate the secondary key. The primary key, and the sessions authenticated with it, stay valid:

Terminal window
az redis regenerate-keys \
--name redis-demo-cache \
--resource-group rg-redis-demo \
--key-type Secondary \
--output none

Enable the non-TLS port:

Terminal window
az redis update \
--name redis-demo-cache \
--resource-group rg-redis-demo \
--set enableNonSslPort=true \
--query "{enableNonSslPort:enableNonSslPort, port:port}"
Output
{
"enableNonSslPort": true,
"port": 4511
}

While the non-TLS port is enabled, port is a port the emulator assigns to the cache rather than 6379. Capture it and connect without TLS:

Terminal window
export REDIS_PLAINTEXT_PORT=$(az redis show --name redis-demo-cache --resource-group rg-redis-demo --query port --output tsv)
redis-cli -h "$REDIS_HOST" -p "$REDIS_PLAINTEXT_PORT" GET greeting
Output
Hello from LocalStack

Disable the non-TLS port again:

Terminal window
az redis update \
--name redis-demo-cache \
--resource-group rg-redis-demo \
--set enableNonSslPort=false \
--query "{enableNonSslPort:enableNonSslPort}"
Output
{
"enableNonSslPort": false
}

Create a firewall rule that admits only the 203.0.113.0/24 documentation range:

Terminal window
az redis firewall-rules create \
--name redis-demo-cache \
--resource-group rg-redis-demo \
--rule-name allow-office \
--start-ip 203.0.113.1 \
--end-ip 203.0.113.254
Output
{
"endIp": "203.0.113.254",
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-redis-demo/providers/Microsoft.Cache/Redis/redis-demo-cache/firewallRules/allow-office",
"name": "redis-demo-cache/allow-office",
"resourceGroup": "rg-redis-demo",
"startIp": "203.0.113.1",
"type": "Microsoft.Cache/Redis/firewallRules"
}

List the firewall rules:

Terminal window
az redis firewall-rules list \
--name redis-demo-cache \
--resource-group rg-redis-demo \
--query "[].{name:name, startIp:startIp, endIp:endIp}" \
--output table
Output
Name StartIp EndIp
----------------------------- ----------- -------------
redis-demo-cache/allow-office 203.0.113.1 203.0.113.254

Your client’s address is outside the rule, so the cache now refuses the connection:

Terminal window
redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" --tls PING
Output
Could not connect to Redis at redis-demo-cache.redis.azure.localhost.localstack.cloud:4510: SSL_connect failed: unexpected eof while reading

Delete the rule to admit every address again:

Terminal window
az redis firewall-rules delete \
--name redis-demo-cache \
--resource-group rg-redis-demo \
--rule-name allow-office
redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" --tls PING
Output
PONG

Set a maintenance window for Sundays at 02:00 UTC:

Terminal window
az redis patch-schedule create \
--name redis-demo-cache \
--resource-group rg-redis-demo \
--schedule-entries '[{"dayOfWeek":"Sunday","startHourUtc":2,"maintenanceWindow":"PT5H"}]'
Output
{
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-redis-demo/providers/Microsoft.Cache/Redis/redis-demo-cache/patchSchedules/default",
"location": "West Europe",
"name": "redis-demo-cache/default",
"resourceGroup": "rg-redis-demo",
"scheduleEntries": [
{
"dayOfWeek": "Sunday",
"maintenanceWindow": "5:00:00",
"startHourUtc": 2
}
],
"type": "Microsoft.Cache/Redis/PatchSchedules"
}

Show the schedule:

Terminal window
az redis patch-schedule show \
--name redis-demo-cache \
--resource-group rg-redis-demo \
--query "scheduleEntries"
Output
[
{
"dayOfWeek": "Sunday",
"maintenanceWindow": "5:00:00",
"startHourUtc": 2
}
]

Reboot the cache. A reboot doesn’t remove its data:

Terminal window
az redis force-reboot \
--name redis-demo-cache \
--resource-group rg-redis-demo \
--reboot-type AllNodes
redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" --tls DBSIZE
Output
{
"message": "The requested reboot operation for 'Value' has been successfully scheduled and should be executed shortly."
}
1

Flush the cache to remove every key:

Terminal window
az redis flush \
--name redis-demo-cache \
--resource-group rg-redis-demo \
--yes
redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" --tls DBSIZE
Output
{
"status": "Succeeded",
...
}
0

Delete the cache and the resource group:

Terminal window
az redis delete \
--name redis-demo-cache \
--resource-group rg-redis-demo \
--yes
az group delete \
--name rg-redis-demo \
--yes
  • Cache lifecycle: Create, show, list, update, and delete caches in the Basic, Standard, and Premium tiers, and check whether a cache name is available.
  • Real Redis server: Each cache runs a Redis server in its own container, so clients connect with the Redis protocol and run Redis commands against a real Redis engine.
  • TLS and non-TLS ports: The cache serves TLS on its SSL port, with a certificate that covers its host name, and plaintext on its non-TLS port while enableNonSslPort is true. Enabling or disabling the non-TLS port keeps the data, the keys, and the TLS endpoint.
  • Access keys: Clients authenticate with the primary or the secondary key. az redis regenerate-keys replaces only the key you name, and setting disableAccessKeyAuthentication turns key authentication off and closes the sessions authenticated with a key.
  • Firewall rules: Create, show, list, and delete firewall rules. While a cache has rules, it admits only clients whose address falls in one of them, and closes existing sessions from other addresses.
  • Public network access: Setting publicNetworkAccess to Disabled refuses new connections and closes existing ones.
  • Scaling: Changing the SKU follows the scaling rules of Azure: a cache can’t move to a lower tier, from Basic straight to Premium, change size while moving from Basic to Standard, or scale down to C0.
  • Administration: Reboot a cache, which restarts its Redis server and keeps its data, flush a cache, and manage its update schedule.
  • One Redis server per cache: A cache has no replica and no shards. Premium clustering, shard counts, replicas per primary, and availability zones are stored but not provisioned.
  • Redis 8.2 instead of Redis 6: Every cache runs Redis 8.2, while Azure Cache for Redis runs Redis 6, so commands added in Redis 7 and later work locally but fail in Azure. The Redis modules, which Azure Cache for Redis doesn’t offer, are not loaded.
  • Memory size isn’t enforced: The SKU’s size sets no memory limit, so eviction never happens.
  • Redis configuration is not applied: Settings in redisConfiguration, such as maxmemory-policy and notify-keyspace-events, are stored and returned but not applied to the Redis server.
  • Update schedules are metadata-only: Patch schedules are stored and returned, and no maintenance takes place.
  • Firewall rules see the address that reaches the emulator: When LocalStack runs in Docker, a connection from your machine reaches the cache from the gateway address of the emulator’s Docker network, such as 172.17.0.1, rather than from your machine’s address, and firewall rules apply to that address.
  • Unsupported features: Microsoft Entra authentication and access policies, geo-replication and linked servers, import and export, data persistence, virtual network injection, and Private Link are not supported.

Explore end-to-end examples in the LocalStack for Azure Samples repository.

21 of 34 operations implemented

Operation ▲Implemented ▼
Page 1 of 0
Was this page helpful?