# Managed Redis

Source: /azure/services/managed-redis/

## Introduction

Azure Managed Redis is a managed in-memory data store built on Redis Enterprise software. Applications use it as a cache, a session store, a vector and document store through its modules, and a message broker. A cluster serves one database, named `default`, that clients reach with standard Redis clients. For more information, see [What is Azure Managed Redis?](https://learn.microsoft.com/azure/redis/overview)

LocalStack for Azure provides a local environment for building and testing applications that use Azure Managed Redis.
Each database 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 database serves TLS or plaintext, as its client protocol sets, authenticates clients with access keys or with Microsoft Entra ID through access policy assignments, loads the modules you enable at creation, and enforces the cluster's public network access setting.

The supported APIs are listed in the [API Coverage](#api-coverage) section.

## Getting started

This guide is designed for users new to Azure Managed 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](/azure/getting-started/). Once the container is running, enable Azure CLI interception by running:

```bash
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:

```bash
lstk az stop-interception
```

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

:::note
The `az redisenterprise` commands come from the `redisenterprise` Azure CLI extension. Install it, or upgrade it, before you start:

```bash
az extension add --name redisenterprise --upgrade
```
:::

### Create a resource group

Create a resource group to contain your cluster:

```bash
az group create \
  --name rg-managed-redis-demo \
  --location westeurope
```

```bash title="Output"
{
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-managed-redis-demo",
  "location": "westeurope",
  "name": "rg-managed-redis-demo",
  "properties": {
    "provisioningState": "Succeeded"
  },
  ...
}
```

### Create a cluster

Create a Balanced B0 cluster and its default database in one command. The database serves TLS, authenticates with access keys, and loads the RedisJSON module:

```bash
az redisenterprise create \
  --cluster-name managed-redis-demo \
  --resource-group rg-managed-redis-demo \
  --location westeurope \
  --sku Balanced_B0 \
  --high-availability Disabled \
  --public-network-access Enabled \
  --access-keys-authentication Enabled \
  --client-protocol Encrypted \
  --clustering-policy EnterpriseCluster \
  --modules name=RedisJSON
```

```bash title="Output"
{
  "accessKeysAuthentication": "Enabled",
  "clientProtocol": "Encrypted",
  "clusteringPolicy": "EnterpriseCluster",
  "deferUpgrade": "NotDeferred",
  "evictionPolicy": "VolatileLRU",
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-managed-redis-demo/providers/Microsoft.Cache/redisEnterprise/managed-redis-demo/databases/default",
  "modules": [
    {
      "args": "",
      "name": "RedisJSON",
      "version": "8.2.9"
    }
  ],
  "name": "default",
  "port": 4510,
  "provisioningState": "Succeeded",
  "redisVersion": "7.4",
  "resourceGroup": "rg-managed-redis-demo",
  "resourceState": "Running",
  "type": "Microsoft.Cache/redisEnterprise/databases"
}
```

Show the cluster's host name:

```bash
az redisenterprise show \
  --cluster-name managed-redis-demo \
  --resource-group rg-managed-redis-demo \
  --query "{hostName:hostName, sku:sku.name, provisioningState:provisioningState, resourceState:resourceState}"
```

```bash title="Output"
{
  "hostName": "managed-redis-demo.redis.azure.localhost.localstack.cloud",
  "provisioningState": "Succeeded",
  "resourceState": "Running",
  "sku": "Balanced_B0"
}
```

Show the database's port and settings:

```bash
az redisenterprise database show \
  --cluster-name managed-redis-demo \
  --resource-group rg-managed-redis-demo \
  --query "{port:port, clientProtocol:clientProtocol, clusteringPolicy:clusteringPolicy, modules:modules[].name, accessKeysAuthentication:accessKeysAuthentication}"
```

```bash title="Output"
{
  "accessKeysAuthentication": "Enabled",
  "clientProtocol": "Encrypted",
  "clusteringPolicy": "EnterpriseCluster",
  "modules": [
    "RedisJSON"
  ],
  "port": 4510
}
```

The host name is an emulator host name rather than `managed-redis-demo.westeurope.redis.azure.net`, and the port is a port the emulator assigns to the database rather than 10000.

### Connect with an access key

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

```bash
export REDIS_HOST=$(az redisenterprise show --cluster-name managed-redis-demo --resource-group rg-managed-redis-demo --query hostName --output tsv)
export REDIS_PORT=$(az redisenterprise database show --cluster-name managed-redis-demo --resource-group rg-managed-redis-demo --query port --output tsv)
export REDISCLI_AUTH=$(az redisenterprise database list-keys --cluster-name managed-redis-demo --resource-group rg-managed-redis-demo --query primaryKey --output tsv)
```

Connect over TLS, then store and read a JSON document with the RedisJSON module:

```bash
redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" --tls PING
redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" --tls JSON.SET product:1 '$' '{"name":"Widget","price":19.99}'
redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" --tls JSON.GET product:1 '$.name'
```

```bash title="Output"
PONG
OK
["Widget"]
```

The database 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 database 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:

```bash
az redisenterprise database regenerate-key \
  --cluster-name managed-redis-demo \
  --resource-group rg-managed-redis-demo \
  --key-type Secondary \
  --output none
```

### Connect with Microsoft Entra ID

With Microsoft Entra authentication, the Redis user name is the object ID of your user or service principal, and the password is an access token for `https://redis.azure.com`.
Get a token, and read your object ID from its `oid` claim:

```bash
export REDIS_TOKEN=$(az account get-access-token --resource https://redis.azure.com --query accessToken --output tsv)
export PRINCIPAL_ID=$(python3 -c 'import base64, json, os; payload = os.environ["REDIS_TOKEN"].split(".")[1]; print(json.loads(base64.urlsafe_b64decode(payload + "=" * (-len(payload) % 4)))["oid"])')
```

Assign the `default` access policy to that object ID:

```bash
az redisenterprise database access-policy-assignment create \
  --cluster-name managed-redis-demo \
  --resource-group rg-managed-redis-demo \
  --database-name default \
  --name demo-user \
  --access-policy-name default \
  --object-id "$PRINCIPAL_ID" \
  --query "{name:name, accessPolicyName:accessPolicyName, provisioningState:provisioningState}"
```

```bash title="Output"
{
  "accessPolicyName": "default",
  "name": "demo-user",
  "provisioningState": "Succeeded"
}
```

Connect with the object ID and the token:

```bash
REDISCLI_AUTH="$REDIS_TOKEN" redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" --tls --user "$PRINCIPAL_ID" JSON.GET product:1 '$.price'
```

```bash title="Output"
[19.99]
```

### List the SKUs the cluster can scale to

List the Balanced SKUs the cluster can scale to:

```bash
az redisenterprise list-skus-for-scaling \
  --cluster-name managed-redis-demo \
  --resource-group rg-managed-redis-demo \
  --query "skus[?starts_with(name, 'Balanced_')].{name:name, sizeInGB:sizeInGB}" \
  --output table
```

```bash title="Output"
Name            SizeInGB
--------------  ----------
Balanced_B0     0.5
Balanced_B1     1.0
Balanced_B3     3.0
Balanced_B5     6.0
Balanced_B10    12.0
Balanced_B20    24.0
Balanced_B50    60.0
Balanced_B100   120.0
Balanced_B150   175.0
Balanced_B250   235.0
Balanced_B350   350.0
Balanced_B500   480.0
Balanced_B700   720.0
Balanced_B1000  960.0
```

### Flush the database

Flush the database to remove every key:

```bash
az redisenterprise database flush \
  --cluster-name managed-redis-demo \
  --resource-group rg-managed-redis-demo
redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" --tls DBSIZE
```

```bash title="Output"
0
```

### Delete the cluster

Delete the cluster and the resource group:

```bash
az redisenterprise delete \
  --cluster-name managed-redis-demo \
  --resource-group rg-managed-redis-demo \
  --yes
az group delete \
  --name rg-managed-redis-demo \
  --yes
```

## Features

- **Cluster lifecycle**: Create, show, list, update, and delete clusters in the Memory Optimized, Balanced, Compute Optimized, and Flash Optimized tiers. An update changes the tags, the SKU, the public network access setting, and the minimum TLS version, and can turn high availability on but not off.
- **Default database**: Create the `default` database with its client protocol, clustering policy, eviction policy, modules, access key setting, and upgrade deferral, show and list it, update its eviction policy, access key setting, and upgrade deferral, and delete it.
- **Real Redis server**: Each database 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 plaintext**: A database with the `Encrypted` client protocol serves TLS, with a certificate that covers its host name, and a database with the `Plaintext` client protocol serves plaintext.
- **Access keys**: Clients authenticate with the primary or the secondary key. Regenerating a key replaces only that key. Disabling access key authentication closes the sessions authenticated with a key and keeps the Microsoft Entra sessions, and enabling it again issues two new keys.
- **Microsoft Entra authentication**: Access policy assignments grant a user or a service principal the `default` access policy. The client authenticates with its object ID and an access token for `https://redis.azure.com`, and deleting the assignment revokes the access.
- **Modules**: A database loads the RediSearch, RedisJSON, RedisBloom, and RedisTimeSeries modules you enable when you create it. The commands of a module the database doesn't load answer `unknown command`. RediSearch requires the `EnterpriseCluster` or `NoCluster` clustering policy, and a Flash Optimized cluster offers only RedisJSON.
- **Clustering policies**: The `EnterpriseCluster` and `NoCluster` policies present a single endpoint, and the `OSSCluster` policy presents the Redis Cluster protocol to cluster-aware clients. A `NoCluster` database can move to `EnterpriseCluster` or `OSSCluster` and keeps its data and keys.
- **Public network access**: Setting the cluster's `publicNetworkAccess` to `Disabled` refuses new connections and closes existing ones.
- **Scaling**: List the SKUs a cluster can scale to, and change its SKU. A cluster can't scale between the in-memory tiers and Flash Optimized, or before its database exists.
- **Flush**: Flush the database to remove every key.

## Limitations

- **One Redis server per database**: A database has no shards and no replica, whatever its SKU and its `highAvailability` setting. With the `OSSCluster` policy, one Redis node holds every hash slot.
- **Redis 8.2**: Every database runs Redis 8.2 and its modules, the version Azure Managed Redis offers in preview, although it reports `redisVersion` 7.4, the version Azure creates by default. Commands added after Redis 7.4 work locally but fail on a Redis 7.4 database in Azure.
- **Module arguments**: A database applies `MAXSEARCHRESULTS` for RediSearch, `ERROR_RATE` and `INITIAL_SIZE` for RedisBloom, and `RETENTION_POLICY` for RedisTimeSeries, and refuses any other module argument.
- **Memory size isn't enforced**: The SKU's size sets no memory limit, so the eviction policy never evicts.
- **Unsupported features**: Active geo-replication and linked databases, import and export, data persistence, customer-managed keys, Private Link, migration from Azure Cache for Redis, the `notifyKeyspaceEvents` setting, and Redis version upgrades are not supported.

## Samples

Explore end-to-end examples in the [LocalStack for Azure Samples](https://github.com/localstack/localstack-azure-samples) repository.

## API Coverage


### Managed Redis API coverage

Source service: `managed-redis`. 20 of 31 tracked operations are implemented.

Service documentation: /azure/services/managed-redis/

| Operation | Status |
| --- | --- |
| AccessPolicyAssignment.CreateUpdate | Implemented |
| AccessPolicyAssignment.Delete | Implemented |
| AccessPolicyAssignment.Get | Implemented |
| AccessPolicyAssignment.List | Implemented |
| Databases.Create | Implemented |
| Databases.Delete | Implemented |
| Databases.Export | Not implemented |
| Databases.Flush | Implemented |
| Databases.ForceLinkToReplicationGroup | Not implemented |
| Databases.ForceUnlink | Not implemented |
| Databases.Get | Implemented |
| Databases.Import | Not implemented |
| Databases.ListByCluster | Implemented |
| Databases.ListKeys | Implemented |
| Databases.RegenerateKey | Implemented |
| Databases.Update | Implemented |
| Databases.UpgradeDbRedisVersion | Not implemented |
| Operations.List | Implemented |
| OperationsStatus.Get | Not implemented |
| PrivateEndpointConnections.Delete | Not implemented |
| PrivateEndpointConnections.Get | Not implemented |
| PrivateEndpointConnections.List | Not implemented |
| PrivateEndpointConnections.Put | Not implemented |
| PrivateLinkResources.ListByCluster | Not implemented |
| RedisEnterprise.Create | Implemented |
| RedisEnterprise.Delete | Implemented |
| RedisEnterprise.Get | Implemented |
| RedisEnterprise.List | Implemented |
| RedisEnterprise.ListByResourceGroup | Implemented |
| RedisEnterprise.ListSkusForScaling | Implemented |
| RedisEnterprise.Update | Implemented |
