Throttle function

Learn more about the throttle workflow function within Knock's notification engine.

A throttle function allows you to limit the number of times a workflow is executed for a recipient within a given window. For example, in an alerting system, your recipients might only want to receive a single email per hour for a given alert. A throttle lets you express this logic within Knock.

Throttle functions are helpful when you want to control how often a workflow is executed for a recipient without needing to implement the logic within your own application layer.

How throttling works

Throttling works like a gate. When the throttle step is executed, the gate is checked; if the threshold over the window has been exceeded, then the workflow stops execution. If the threshold has not been met, then the workflow will proceed.

Throttle functions have 3 pieces of configuration:

  1. A throttle window: the length of the throttle period.
  2. A throttle threshold: the number of invocations allowed within the window. Defaults to 1 if none provided.
  3. A throttle key (optional): An optional value to specify as the throttle key for the workflow run.

Setting a throttle window

The throttle window determines how long a throttle is active for the recipient. The window opens the first time the throttle function is executed in a workflow run for a recipient.

Set a fixed throttle window

You can set a fixed duration throttle window using the "Throttle for a fixed window" option in the throttle step. The window accepts a relative duration, which can be specified in seconds, minutes, hours, or days.

Set a dynamic throttle window

You can also set the length of your throttle windows dynamically using a variable. You can use any of the data, recipient, actor, or environment variables associated with the workflow run to set your dynamic throttle window.

When specifying a dynamic window you can provide either:

  • An ISO-8601 timestamp (e.g. 2022-05-04T20:34:07Z) which must be a datetime in the future
  • A relative duration unit (e.g { "unit": "seconds", "value": 30 })

A dynamic interval must be available to be resolved via the key you specify on the given schema, meaning that if you specify a key of throttleWindow in your Data schema, your workflow trigger data must contain either an ISO-8601 timestamp or a valid duration unit.

A relative duration

When the key specified is missing or resolves to an invalid value, a corresponding error will be logged on the workflow run, and the throttle will be skipped.

Setting a throttle threshold

The throttle threshold determines how many invocations are allowed in the window before the threshold takes effect. By default, this value is set to 1, but you can change it as needed.

For example, if you want to say that you want to allow 5 invocations over a 1-minute window, then you would set the throttle threshold to 5.

Selecting a throttle key

A throttle function always runs per recipient. If you do not provide a throttle key, your throttle function will throttle for the executing step per recipient. If you do provide a throttle key, your throttle function will be evaluated for the key and executing step.

💡
A quick tip. Here's a helpful way to think about throttling. By default, the throttle function throttles on a key of recipient_id. When a throttle key is provided, it throttles on a key of concat(recipient_id, throttle_key).

Custom throttle keys must be shorter than 64 characters long after being JSON and URL encoded.

Frequently asked questions