Edit

Share via


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.

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

Install bundle

To be able to use this binding extension in your app, make sure that the host.json file in the root of your project contains this extensionBundle reference:

{
    "version": "2.0",
    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[4.0.0, 5.0.0)"
    }
}

In this example, the version value of [4.0.0, 5.0.0) instructs the Functions host to use a bundle version that is at least 4.0.0 but less than 5.0.0, which includes all potential versions of 4.x. This notation effectively maintains your app on the latest available minor version of the v4.x extension bundle.

When possible, you should use the latest extension bundle major version and allow the runtime to automatically maintain the latest minor version. You can view the contents of the latest bundle on the extension bundles release page.

If your app requires you to use a previous extension version, you might need to instead specify a previous bundle version. You can review the bundle releases to locate a bundle that contains a version of this extension that can be used by your app. For more information, see Azure Functions extension bundles. ::: zone-end

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>

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 mananged 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.

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>"