Overview of Azure functions for Azure Cache for Redis

This article describes how to use 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 Cache for Redis provides a set of building blocks and best practices for building distributed applications, including microservices, state management, pub/sub messaging, and more.

Azure Cache for 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 Cache for Redis and Azure Functions to build functions that react to events from Azure Cache for 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 Basic Standard, Premium Enterprise, Enterprise Flash
Pub/Sub Yes Yes Yes
Lists Yes Yes Yes
Streams Yes Yes Yes
Bindings Yes 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 --prerelease

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 Cache for 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 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>.redis.cache.windows.net:6380,password=..."

System-assigned managed identity

"Redis:redisHostName": "<cacheName>.redis.cache.windows.net",
"Redis:principalId": "<principalId>"

User-assigned managed identity

"Redis:redisHostName": "<cacheName>.redis.cache.windows.net",
"Redis:principalId": "<principalId>",
"Redis:clientId": "<clientId>"

Service Principal Secret

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