|
| 1 | +--- |
| 2 | +title: "Rate Limiting" |
| 3 | +description: "Control how many runs can execute within a specific time window." |
| 4 | +--- |
| 5 | + |
| 6 | +While concurrency limits how many runs can execute at the exact same time, rate limiting allows you to control how many runs can execute within a specific time window. This is useful for interacting with external APIs that have strict rate limits (e.g., "100 requests per minute"). |
| 7 | + |
| 8 | +You can set rate limits on a task by using the `rateLimits` property. It accepts an array of rate limit configurations, where each configuration specifies a `limit` and a `window` (in seconds or as a string like `"1m"`). You can define both static and dynamic rate limits. |
| 9 | + |
| 10 | +## Static rate limits |
| 11 | + |
| 12 | +A static rate limit applies globally to the task. You define it using a `staticKey`. |
| 13 | + |
| 14 | +```ts /trigger/rate-limited.ts |
| 15 | +export const rateLimitedTask = task({ |
| 16 | + id: "rate-limited-task", |
| 17 | + rateLimits: [ |
| 18 | + { |
| 19 | + staticKey: "my-api", |
| 20 | + limit: 100, |
| 21 | + window: 60, // 100 runs per 60 seconds |
| 22 | + }, |
| 23 | + ], |
| 24 | + run: async (payload: any, { ctx }) => { |
| 25 | + //... |
| 26 | + }, |
| 27 | +}); |
| 28 | +``` |
| 29 | + |
| 30 | +## Dynamic rate limits |
| 31 | + |
| 32 | +A dynamic rate limit applies per-tenant or per-user, based on the payload. You define it using a `dynamicKey` which is a JSON path to a value in your payload. |
| 33 | + |
| 34 | +```ts /trigger/dynamic-rate-limited.ts |
| 35 | +export const dynamicRateLimitedTask = task({ |
| 36 | + id: "dynamic-rate-limited-task", |
| 37 | + rateLimits: [ |
| 38 | + { |
| 39 | + dynamicKey: "payload.userId", |
| 40 | + limit: 10, |
| 41 | + window: "1m", // 10 runs per minute per user |
| 42 | + } |
| 43 | + ], |
| 44 | + run: async (payload: { userId: string }, { ctx }) => { |
| 45 | + //... |
| 46 | + }, |
| 47 | +}); |
| 48 | +``` |
| 49 | + |
| 50 | +## Custom queues |
| 51 | + |
| 52 | +You can also apply rate limits to custom queues, which allows multiple tasks to share the same rate limit: |
| 53 | + |
| 54 | +```ts /trigger/rate-limited-queue.ts |
| 55 | +export const apiQueue = queue({ |
| 56 | + name: "api-queue", |
| 57 | +}); |
| 58 | + |
| 59 | +export const task1 = task({ |
| 60 | + id: "task-1", |
| 61 | + queue: apiQueue, |
| 62 | + rateLimits: [ |
| 63 | + { |
| 64 | + staticKey: "shared-api", |
| 65 | + limit: 50, |
| 66 | + window: 10, // 50 runs per 10 seconds |
| 67 | + }, |
| 68 | + ], |
| 69 | + run: async (payload) => { |
| 70 | + // ... |
| 71 | + }, |
| 72 | +}); |
| 73 | +``` |
| 74 | + |
| 75 | +## Overriding rate limits |
| 76 | + |
| 77 | +You can override queue rate limits dynamically from the Trigger.dev dashboard. Navigate to the **Queues** page in your project, select a queue, and use the UI to add, modify, or remove rate limits. |
0 commit comments