Redis Entra ID
The `redis-entraid` Python package simplifies authentication with Azure Managed Redis and Azure Cache for Redis using Microsoft Entra ID (formerly Azure Active Directory). It handles fetching and renewing authentication tokens in the background, building on `redis-py`. The current version is 1.1.2, released on March 26, 2026. This library appears to have a regular release cadence with several updates in the past year.
Common errors
-
ERR WRONGPASS invalid username-password pair
cause The Entra ID authentication token used by the Redis connection has expired and was not successfully renewed.fixThis error indicates a failure in the automatic token renewal. Verify that the `azure-identity` (or other underlying identity provider) configuration is correct and has the necessary permissions to refresh tokens. Check application logs for messages related to token acquisition failures. -
redis.exceptions.ConnectionError: [Errno 111] Connection refused
cause The Redis client attempted to connect without TLS/SSL, or the hostname/port is incorrect, or network connectivity issues.fixEnsure `ssl=True` is passed to the `redis.Redis` constructor. Double-check the `host` and `port` values for your Azure Redis instance. Confirm network access from your application to the Redis endpoint. -
An error occurred: Failed to acquire token! Identity provider request failed! Failed to acquire token! (or similar messages involving 'TokenRequestException')
cause The `azure-identity` library or the underlying MSAL client could not successfully obtain an Entra ID token, possibly due to incorrect credentials, insufficient permissions for the service principal/managed identity, or network issues contacting Azure AD endpoints.fixReview your Azure Entra ID application registration, service principal, or managed identity configuration. Ensure the client ID, tenant ID, and client secret (if applicable) are correct and that the identity has permissions to access the Redis cache. Verify network access to `login.microsoftonline.com` or your specific Azure AD authority.
Warnings
- breaking Python 3.9 support was dropped in `redis-entraid` version 1.1.0. If you are using Python 3.9, you must use `redis-entraid` version 1.0.0 or downgrade your Python version.
- gotcha Azure Redis with Entra ID authentication strictly requires TLS/SSL connections. Attempting to connect without `ssl=True` in the `redis.Redis` client configuration will result in connection errors.
- gotcha Long-lived connections may fail after an Entra ID token expires (typically ~24 hours) if the token renewal mechanism is not properly configured or if there are underlying issues preventing automatic refresh. This can manifest as `ERR WRONGPASS`.
Install
-
pip install redis-entraid
Imports
- Redis
from redis import Redis
- create_from_default_azure_credential
from redis_entraid.cred_provider import create_from_default_azure_credential
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from redis import Redis
from azure.identity import DefaultAzureCredential
from redis_entraid.cred_provider import create_from_default_azure_credential
# --- Environment Variables (for DefaultAzureCredential to pick up) ---
# Set these in your environment, e.g., in a .env file or directly:
# os.environ['AZURE_TENANT_ID'] = 'YOUR_TENANT_ID'
# os.environ['AZURE_CLIENT_ID'] = 'YOUR_CLIENT_ID'
# os.environ['AZURE_CLIENT_SECRET'] = 'YOUR_CLIENT_SECRET'
# OR ensure a Managed Identity is assigned to your application/VM.
# --- Redis Connection Details ---
# Your Azure Managed Redis or Azure Cache for Redis hostname and port
# Default port for Azure Managed Redis is 10000, for Azure Cache for Redis it's 6380.
REDIS_HOST = os.environ.get('REDIS_HOST', 'your_redis_cache_name.redis.cache.windows.net')
REDIS_PORT = int(os.environ.get('REDIS_PORT', 10000))
def connect_with_entra_id():
try:
# DefaultAzureCredential will attempt to authenticate via various methods
# including environment variables, managed identity, etc.
# Scopes are crucial for Entra ID authentication with Redis.
credential_provider = create_from_default_azure_credential(
("https://redis.azure.com/.default",)
)
# Azure enforces TLS for Entra ID authentication.
# decode_responses=True automatically decodes responses to Python strings.
r = Redis(
host=REDIS_HOST,
port=REDIS_PORT,
ssl=True,
decode_responses=True,
credential_provider=credential_provider
)
# Test the connection
if r.ping():
print(f"Successfully connected to Redis at {REDIS_HOST}:{REDIS_PORT} with Entra ID.")
r.set("mykey", "Hello from redis-entraid!")
value = r.get("mykey")
print(f"Retrieved value: {value}")
else:
print("Redis ping failed.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
connect_with_entra_id()