title | description | author | zone_pivot_groups | ms.author | ms.service | ms.custom | ms.topic | ms.date |
---|---|---|---|---|---|---|---|---|
RedisStreamTrigger for Azure Functions |
Learn how to use RedisStreamTrigger Azure Function for Azure Cache for Redis. |
flang-msft |
programming-languages-set-functions-lang-workers |
franlanglois |
azure-functions |
devx-track-dotnet, devx-track-extended-java, devx-track-js, devx-track-python, ignite-2024 |
reference |
07/12/2024 |
The RedisStreamTrigger
reads new entries from a stream and surfaces those elements to the function.
Trigger Type | Azure Managed Redis | Azure Cache for Redis |
---|---|---|
Streams | Yes | Yes |
Important
When using Azure Managed Redis or the Enterprise tiers of Azure Cache for Redis, use port 10000 rather than port 6380 or 6379.
Important
Redis triggers aren't currently supported for functions running in the Consumption plan.
::: zone pivot="programming-language-javascript"
[!INCLUDE functions-nodejs-model-tabs-redis-preview]
::: zone-end
::: zone pivot="programming-language-python"
[!INCLUDE functions-python-model-tabs-redis-preview] ::: zone-end
Important
For .NET functions, using the isolated worker model is recommended over the in-process model. For a comparison of the in-process and isolated worker models, see differences between the isolated worker model and the in-process model for .NET on Azure Functions.
::: zone pivot="programming-language-csharp"
[!INCLUDE dotnet-execution]
using Microsoft.Extensions.Logging;
namespace Microsoft.Azure.Functions.Worker.Extensions.Redis.Samples.RedisStreamTrigger
{
internal class SimpleStreamTrigger
{
private readonly ILogger<SimpleStreamTrigger> logger;
public SimpleStreamTrigger(ILogger<SimpleStreamTrigger> logger)
{
this.logger = logger;
}
[Function(nameof(SimpleStreamTrigger))]
public void Run(
[RedisStreamTrigger(Common.connectionStringSetting, "streamKey")] string entry)
{
logger.LogInformation(entry);
}
}
}
[!INCLUDE functions-in-process-model-retirement-note]
using Microsoft.Extensions.Logging;
namespace Microsoft.Azure.WebJobs.Extensions.Redis.Samples.RedisStreamTrigger
{
internal class SimpleStreamTrigger
{
[FunctionName(nameof(SimpleStreamTrigger))]
public static void Run(
[RedisStreamTrigger(Common.connectionStringSetting, "streamKey")] string entry,
ILogger logger)
{
logger.LogInformation(entry);
}
}
}
::: zone-end ::: zone pivot="programming-language-java"
package com.function.RedisStreamTrigger;
import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.redis.annotation.*;
public class SimpleStreamTrigger {
@FunctionName("SimpleStreamTrigger")
public void run(
@RedisStreamTrigger(
name = "req",
connection = "redisConnectionString",
key = "streamTest",
pollingIntervalInMs = 1000,
maxBatchSize = 1)
String message,
final ExecutionContext context) {
context.getLogger().info(message);
}
}
::: zone-end ::: zone pivot="programming-language-javascript"
This sample uses the same index.js
file, with binding data in the function.json
file.
Here's the index.js
file:
module.exports = async function (context, entry) {
context.log(entry);
}
From function.json
, here's the binding data:
{
"bindings": [
{
"type": "redisStreamTrigger",
"connection": "redisConnectionString",
"key": "streamTest",
"pollingIntervalInMs": 1000,
"maxBatchSize": 16,
"name": "entry",
"direction": "in"
}
],
"scriptFile": "index.js"
}
[!INCLUDE functions-nodejs-model-tabs-redis-preview]
::: zone-end ::: zone pivot="programming-language-powershell"
This sample uses the same run.ps1
file, with binding data in the function.json
file.
Here's the run.ps1
file:
param($entry, $TriggerMetadata)
Write-Host ($entry | ConvertTo-Json)
From function.json
, here's the binding data:
{
"bindings": [
{
"type": "redisStreamTrigger",
"connection": "redisConnectionString",
"key": "streamTest",
"pollingIntervalInMs": 1000,
"maxBatchSize": 16,
"name": "entry",
"direction": "in"
}
],
"scriptFile": "run.ps1"
}
::: zone-end ::: zone pivot="programming-language-python"
The Python v1 programming model requires you to define bindings in a separate function.json file in the function folder. For more information, see the Python developer guide.
This sample uses the same __init__.py
file, with binding data in the function.json
file.
Here's the __init__.py
file:
import logging
def main(entry: str):
logging.info(entry)
From function.json
, here's the binding data:
{
"bindings": [
{
"type": "redisStreamTrigger",
"connection": "redisConnectionString",
"key": "streamTest",
"pollingIntervalInMs": 1000,
"maxBatchSize": 16,
"name": "entry",
"direction": "in"
}
],
"scriptFile": "__init__.py"
}
[!INCLUDE functions-python-model-tabs-redis-preview]
::: zone-end ::: zone pivot="programming-language-csharp"
Parameters | Description | Required | Default |
---|---|---|---|
Connection |
The name of the application setting that contains the cache connection string, such as: <cacheName>.redis.cache.windows.net:6380,password... |
Yes | |
Key |
Key to read from. | Yes | |
PollingIntervalInMs |
How often to poll the Redis server in milliseconds. | Optional | 1000 |
MessagesPerWorker |
The number of messages each functions worker should process. Used to determine how many workers the function should scale to. | Optional | 100 |
Count |
Number of elements to pull from Redis at one time. | Optional | 10 |
DeleteAfterProcess |
Indicates if the function deletes the stream entries after processing. | Optional | false |
::: zone-end ::: zone pivot="programming-language-java"
Parameter | Description | Required | Default |
---|---|---|---|
name |
entry |
Yes | |
connection |
The name of the application setting that contains the cache connection string, such as: <cacheName>.redis.cache.windows.net:6380,password... |
Yes | |
key |
Key to read from. | Yes | |
pollingIntervalInMs |
How frequently to poll Redis, in milliseconds. | Optional | 1000 |
messagesPerWorker |
The number of messages each functions worker should process. It's used to determine how many workers the function should scale to. | Optional | 100 |
count |
Number of entries to read from Redis at one time. Entries are processed in parallel. | Optional | 10 |
deleteAfterProcess |
Whether to delete the stream entries after the function has run. | Optional | false |
::: zone-end ::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-python"
The following table explains the binding configuration properties that you set in the function.json file.
function.json Properties | Description | Required | Default |
---|---|---|---|
type |
Yes | ||
deleteAfterProcess |
Optional | false |
|
connection |
The name of the application setting that contains the cache connection string, such as: <cacheName>.redis.cache.windows.net:6380,password... |
Yes | |
key |
The key to read from. | Yes | |
pollingIntervalInMs |
How often to poll Redis in milliseconds. | Optional | 1000 |
messagesPerWorker |
(optional) The number of messages each functions worker should process. Used to determine how many workers the function should scale | Optional | 100 |
count |
Number of entries to read from Redis at one time. These are processed in parallel. | Optional | 10 |
name |
Yes | ||
direction |
Yes |
::: zone-end
See the Example section for complete examples.
The RedisStreamTrigger
Azure Function reads new entries from a stream and surfaces those entries to the function.
The trigger polls Redis at a configurable fixed interval, and uses XREADGROUP
to read elements from the stream.
The consumer group for all instances of a function is the name of the function, that is, SimpleStreamTrigger
for the StreamTrigger sample.
Each functions instance uses the WEBSITE_INSTANCE_ID
or generates a random GUID to use as its consumer name within the group to ensure that scaled out instances of the function don't read the same messages from the stream.
::: zone pivot="programming-language-csharp,programming-language-java,programming-language-javascript,programming-language-powershell,programming-language-python"
Type | Description |
---|---|
byte[] |
The message from the channel. |
string |
The message from the channel. |
Custom |
The trigger uses Json.NET serialization to map the message from the channel from a string into a custom type. |
::: zone-end