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 provide 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 Support level
Trigger on Redis pub sub messages Trigger Preview
Trigger on Redis lists Trigger Preview
Trigger on Redis streams Trigger Preview
Read a cached value Input Preview
Write a values to cache Output Preview

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

  1. Create a Java function project. You could use Maven: mvn archetype:generate -DarchetypeGroupId=com.microsoft.azure -DarchetypeArtifactId=azure-functions-archetype -DjavaVersion=8

  2. Add the extension bundle by adding or replacing the following code in your host.json file:

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

    Warning

    The Redis extension is currently only available in a preview bundle release.

  3. Add the Java library for Redis bindings to the pom.xml file:

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

Add the extension bundle by adding or replacing the following code in your host.json file:

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

Warning

The Redis extension is currently only available in a preview bundle release.

Redis connection string

Azure Redis triggers and bindings have a required property for the cache connection string. The connection string can be found on the Access keys menu in the Azure Managed Redis or Azure Cache for Redis portal. 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 three types of connections in your deployments: Connection string, System-assigned managed identity, and User-assigned managed identity

For local development, you can also use service principal secrets.

Use the appsettings to configure each of the following types of client authentication, assuming the Connection was set to Redis in the function.

Connection string

"Redis": "<cacheName>.<region>.redis.azure.net:10000,password=..."

System-assigned managed identity

"Redis:redisHostName": "<cacheName>.<region>.redis.azure.net",
"Redis:principalId": "<principalId>"

User-assigned managed identity

"Redis:redisHostName": "<cacheName>.<region>.redis.azure.net",
"Redis:principalId": "<principalId>",
"Redis:clientId": "<clientId>"

Service Principal Secret

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