Description
n8n Rate Limit Node with Redis
This is a custom node for n8n that allows you to implement a rate-limiting mechanism in your workflows using a Redis instance. It's perfect for preventing downstream services from being overloaded or for controlling API usage on a per-user basis.
Features
- Flexible Time Windows: Configure limits per minute, hour, or day.
- Dynamic Keys: Use n8n expressions to create unique rate-limiting keys for different users, IP addresses, or any other data from your workflow.
- Custom Block Duration: Configure a specific duration to block requests after the limit is reached, independent of the rate limit window.
- Atomic Operations: Uses Lua scripts to ensure rate limiting and blocking operations are atomic and accurate to prevent race conditions.
- Dual Outputs: Easily branch your workflow based on whether the rate limit has been exceeded or not.
- Standard Redis Credentials: Uses a dedicated credential type for easy configuration.
Installation on local n8n instance
- Clone this repository.
- Navigate to the repository's root directory.
- Install dependencies:
npm install - Build the node:
npm run build - Link the node to your n8n installation:
- Run
npm linkin this project's directory. - Run
npm link n8n-nodes-rate-limitin your n8n installation directory.
- Run
- Restart your n8n instance.
Installation on n8n cloud
- Go to the n8n Settings.
- Select the "Community nodes" menu.
- Click on the "Install" button.
- Enter the package name:
n8n-nodes-rate-limit. - Click on the "Install" button.
- Restart your n8n instance.
How to Use
After installation, you will find the Rate Limit node under the "Transform" section.
Credentials
First, you'll need to configure your Redis credentials. Select the corresponding Redis credential from the credentials dropdown or create a new one and fill in your Redis connection details (host, port, password, etc.).
Parameters
- Redis Key: The unique key used to store the counter in Redis. You can use n8n expressions here to make the key dynamic. For example, to limit based on a user ID from an incoming webhook, you could use
rate-limit-{{ $json.body.userId }}. - Limit: The maximum number of requests allowed within the specified time window. For example,
100. - Time Period: The duration of the time window. For example,
15. - Time Unit: The unit for the time period. The options are
Minutes,Hours, orDays. - Redis Block Key: The unique key used to store the block status in Redis. Like the main key, this supports expressions (e.g.,
rate-limit-block-{{ $json.body.userId }}). - Block Time Period: The duration to block requests after the limit is reached.
- Block Time Unit: The unit for the block time period.
Example Configuration: To allow a user 100 requests every 15 minutes, but block them for 1 hour if they exceed that limit:
- Limit:
100 - Time Period:
15 - Time Unit:
Minutes - Block Time Period:
1 - Block Time Unit:
Hours
Outputs
The node has two outputs:
- Not Exceeded (Top Output): If the current request count for the key is less than or equal to the limit, the workflow data will be passed through this output.
- Exceeded (Bottom Output): If the limit has been surpassed, the data will be passed through this output. You can connect this to a node that sends an error message, for example.
License
MIT