RedisStreamTrigger Azure Function (preview)

The RedisStreamTrigger reads new entries from a stream and surfaces those elements to the function.

Tier Basic Standard, Premium Enterprise, Enterprise Flash
Streams Yes Yes Yes

Important

Redis triggers aren't currently supported for functions running in the Consumption plan.

Example

Execution mode Description
Isolated worker model Your function code runs in a separate .NET worker process. Use with supported versions of .NET and .NET Framework. To learn more, see Develop .NET isolated worker process functions.
In-process model Your function code runs in the same process as the Functions host process. Supports only Long Term Support (LTS) versions of .NET. To learn more, see Develop .NET class library functions.

The isolated process examples aren't available in preview.

//TBD

    @FunctionName("StreamTrigger")
    public void StreamTrigger(
            @RedisStreamTrigger(
                name = "entry",
                connectionStringSetting = "redisLocalhost",
                key = "streamTest",
                pollingIntervalInMs = 100,
                messagesPerWorker = 10,
                count = 1,
                deleteAfterProcess = true)
                String entry,
            final ExecutionContext context) {
            context.getLogger().info(entry);
    }

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",
      "deleteAfterProcess": false,
      "connectionStringSetting": "redisLocalhost",
      "key": "streamTest",
      "pollingIntervalInMs": 1000,
      "messagesPerWorker": 100,
      "count": 10,
      "name": "entry",
      "direction": "in"
    }
  ],
  "scriptFile": "index.js"
}

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",
      "deleteAfterProcess": false,
      "connectionStringSetting": "redisLocalhost",
      "key": "streamTest",
      "pollingIntervalInMs": 1000,
      "messagesPerWorker": 100,
      "count": 10,
      "name": "entry",
      "direction": "in"
    }
  ],
  "scriptFile": "run.ps1"
}

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",
      "deleteAfterProcess": false,
      "connectionStringSetting": "redisLocalhost",
      "key": "streamTest",
      "pollingIntervalInMs": 1000,
      "messagesPerWorker": 100,
      "count": 10,
      "name": "entry",
      "direction": "in"
    }
  ],
  "scriptFile": "__init__.py"
}

Attributes

Parameters Description Required Default
ConnectionStringSetting The name of the setting in the appsettings that contains cache connection string For example: <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

Annotations

Parameter Description Required Default
name entry Yes
connectionStringSetting The name of the setting in the appsettings that contains cache connection string For example: <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. These are processed in parallel. Optional 10
deleteAfterProcess Whether to delete the stream entries after the function has run. Optional false

Configuration

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
connectionStringSetting The name of the setting in the appsettings that contains cache connection string For example: <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

See the Example section for complete examples.

Usage

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 function instances is the ID of the function. For example, Microsoft.Azure.WebJobs.Extensions.Redis.Samples.RedisSamples.StreamTrigger for the StreamTrigger sample. Each function creates a new 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.

Output

Note

Once the RedisStreamTrigger becomes generally available, the following information will be moved to a dedicated Output page.

Output Type Description
StackExchange.Redis.ChannelMessage The value returned by StackExchange.Redis.
StackExchange.Redis.NameValueEntry[], Dictionary<string, string> The values contained within the entry.
string, byte[], ReadOnlyMemory<byte> The stream entry serialized as JSON (UTF-8 encoded for byte types) in the following format: {"Id":"1658354934941-0","Values":{"field1":"value1","field2":"value2","field3":"value3"}}
Custom The trigger uses Json.NET serialization to map the message from the channel from a string into a custom type.

Note

Once the RedisStreamTrigger becomes generally available, the following information will be moved to a dedicated Output page.

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