Flow Control
enables you to limit the number of calls made to your workflow by delaying the delivery.
There are two main use cases for Flow-Control in Workflow:
- Limiting the Workflow Environment: Controlling the execution environment.
- Limiting External API Calls: Preventing excessive requests to external services.
There are three parameters you can configure to achieve the desired behavior:
-
Rate and Period:
- Rate specifies the maximum number of calls allowed within a given period (default: 1 second, configurable via the
period
parameter). All calls sharing the sameFlowControl
key count toward this limit. Instead of rejecting calls that exceed the rate, QStash automatically queues and delays them to ensure the limit is respected. - Period is the time window over which the rate limit is enforced. Adjust this to control how frequently calls are allowed.
- Rate specifies the maximum number of calls allowed within a given period (default: 1 second, configurable via the
-
Parallelism:
Sets the maximum number of calls that can be executed concurrently. Unlike rate limiting, this parameter considers the duration of each call. If the parallelism limit is reached, additional calls are queued and will only start when an active call completes, ensuring that the number of simultaneous executions never exceeds the specified limit.
FlowControl
, you need to choose a key first. This key is used to count the number of calls made to your endpoint.
There are not limits to number of keys you can use.
The rate/parallelism limits are not applied per
url
, they are applied per Flow-Control-Key
.Keep in mind that rate/period and parallelism info are kept on each publish separately. That means
if you change the rate/period or parallelism on a new publish, the old fired ones will not be affected. They will keep their flowControl config.
During the period that old
publishes
has not delivered but there are also the publishes
with the new rates, QStash will effectively allow
the highest rate/period or highest parallelism. Eventually(after the old publishes are delivered), the new rate/period and parallelism will be used.Limiting the Workflow Environment
To limit the execution environment, you need to configure both theserve
and trigger
methods.
When configured, all the steps of the workflow will respect the limits.
Due to the nature of the Workflow SDK, QStash calls the serve
method multiple times. This means that to stay within the limits
of the deployed environments, the given rate will be applied to all calls going from QStash servers to the serve
method.
Note that if there are multiple Workflows running in the same environment, their steps can interleave, but overall rate and parallelism limits will be respected
if they share the same flowControl
key.
- In the
serve
method:
flowControl
documentation under serve
parameters.
- In the
trigger
method:
trigger
, see the documentation here.
Limiting External API Calls
To limit requests to an external API, usecontext.call
: