Skip to content

Latest commit

 

History

History
188 lines (130 loc) · 8.84 KB

File metadata and controls

188 lines (130 loc) · 8.84 KB
title Using Azure Functions for Azure Managed Redis
description Learn how to use Azure Functions Azure Managed Redis
author flang-msft
zone_pivot_groups programming-languages-set-functions-lang-workers
ms.author franlanglois
ms.service azure-functions
ms.custom devx-track-extended-java, devx-track-js, devx-track-python, ignite-2024
ms.topic reference
ms.date 07/11/2024

Overview of Azure functions for Azure Redis

This article describes how to use either Azure Managed Redis or Azure Cache for Redis with Azure Functions to create optimized serverless and event-driven architectures.

Azure Functions provides an event-driven programming model where triggers and bindings are key features. With Azure Functions, you can easily build event-driven serverless applications. Azure Redis services (Azure Managed Redis and Azure Cache for Redis) provide a set of building blocks and best practices for building distributed applications, including microservices, state management, pub/sub messaging, and more.

Azure Redis can be used as a trigger for Azure Functions, allowing you to initiate a serverless workflow. This functionality can be highly useful in data architectures like a write-behind cache, or any event-based architectures.

You can integrate Azure Redis and Azure Functions to build functions that react to events from Azure Redis or external systems.

Action Direction
Trigger on Redis pub sub messages Trigger
Trigger on Redis lists Trigger
Trigger on Redis streams Trigger
Read a cached value Input
Write a values to cache Output

Scope of availability for functions triggers and bindings

Tier Azure Cache for Redis (Basic, Standard, Premium, Enterprise, Enterprise Flash) Azure Managed Redis (Memory Optimized, Basic, Compute Optimized, Flash Optimized)
Pub/Sub Yes Yes
Lists Yes Yes
Streams Yes Yes
Bindings Yes Yes

Important

Redis triggers are currently only supported for functions running in either a Elastic Premium plan or a dedicated App Service plan.

::: zone pivot="programming-language-csharp"

Install extension

Functions run in an isolated C# worker process. To learn more, see Guide for running C# Azure Functions in an isolated worker process.

Add the extension to your project by installing this NuGet package.

dotnet add package Microsoft.Azure.Functions.Worker.Extensions.Redis

[!INCLUDE functions-in-process-model-retirement-note]

Functions run in the same process as the Functions host. To learn more, see Develop C# class library functions using Azure Functions.

Add the extension to your project by installing this NuGet package.

dotnet add package Microsoft.Azure.WebJobs.Extensions.Redis

::: zone-end
::: zone pivot="programming-language-javascript,programming-language-python,programming-language-powershell"
[!INCLUDE functions-install-extension-bundle]
::: zone-end
::: zone pivot="programming-language-java"

Update packages

Add the Azure Functions Java Redis Annotations package to your project by updating the pom.xml file to add this dependency:

<dependency>
  <groupId>com.microsoft.azure.functions</groupId>
  <artifactId>azure-functions-java-library-redis</artifactId>
  <version>1.0.0</version>
</dependency>

::: zone-end

Redis connection string

Azure Redis triggers and bindings have a required property that indicates the application setting or collection name that contains cache connection information. The Redis trigger or binding looks for an environmental variable holding the connection string with the name passed to the Connection parameter.

In local development, the Connection can be defined using the local.settings.json file. When deployed to Azure, application settings can be used.

When connecting to a cache instance with an Azure function, you can use one of these kinds of connections in your deployments:

A user-assigned managed identity must be associated with your function app, and that identity must also be granted explicit permissions in your cache service. For more information, see Use Microsoft Entra ID for cache authentication.

The built-in system-assigned managed identity must be enabled in your function app, and that identity must also be granted explicit permissions in your cache service. For more information, see Use Microsoft Entra ID for cache authentication.

The connection string can be found on the Access keys menu in the Azure Managed Redis or Azure Cache for Redis portal.

For optimal security, your function app should use Microsoft Entra ID with managed identities to authorize requests against your cache, if possible. Authorization by using Microsoft Entra ID and managed identities provides superior security and ease of use over shared access key authorization. For more information about using managed identities with your cache, see Use Microsoft Entra ID for cache authentication.

Connecting to the service using a service principal is only supported when running locally during development. A service principal linked to your account must be granted explicit permissions in your cache service.


These examples show the key name and value of app settings required to connect to each cache service based on the kind of client authentication, assuming that the Connection property in the binding is set to Redis.

"Redis__redisHostName": "<cacheName>.<region>.redis.azure.net",
"Redis__principalId": "<principalId>",
"Redis__clientId": "<clientId>"
"Redis__redisHostName": "<cacheName>.<region>.redis.azure.net",
"Redis__principalId": "<principalId>"
"Redis": "<cacheName>.<region>.redis.azure.net:10000,password=..."

Connections using Service Principal Secrets are only available during local development.

"Redis__redisHostName": "<cacheName>.<region>.redis.azure.net",
"Redis__principalId": "<principalId>",
"Redis__clientId": "<clientId>"
"Redis__tenantId": "<tenantId>"
"Redis__clientSecret": "<clientSecret>"
"Redis__redisHostName": "<cacheName>.redis.cache.windows.net",
"Redis__principalId": "<principalId>",
"Redis__clientId": "<clientId>"
"Redis__redisHostName": "<cacheName>.redis.cache.windows.net",
"Redis__principalId": "<principalId>"
"Redis": "<cacheName>.redis.cache.windows.net:6380,password=..."

Connections using Service Principal Secrets are only available during local development.

"Redis__redisHostName": "<cacheName>.redis.cache.windows.net",
"Redis__principalId": "<principalId>",
"Redis__clientId": "<clientId>"
"Redis__tenantId": "<tenantId>"
"Redis__clientSecret": "<clientSecret>"

Related content