You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This tutorial explains how to use Queues to handle rate limits of external APIs. In this tutorial, you will build an application that sends email notifications using [Resend](https://www.resend.com/), but you can use this pattern to handle rate limits of any external API.
23
+
This tutorial explains how to use Queues to handle rate limits of external APIs by building an application that sends email notifications using [Resend](https://www.resend.com/). However, you can use this pattern to handle rate limits of any external API.
24
24
25
-
Resend is a service that allows you to send emails from your application. It provides a simple API that you can use to send emails. Resend has a default [rate limit](https://resend.com/docs/api-reference/introduction#rate-limit) of 2 requests per second. You will use Queues to handle the rate limit of Resend.
25
+
Resend is a service that allows you to send emails from your application via an API. Resend has a default [rate limit](https://resend.com/docs/api-reference/introduction#rate-limit) of two requests per second. You will use Queues to handle the rate limit of Resend.
26
26
27
27
## Prerequisites
28
28
29
29
<Renderfile="prereqs"product="workers" />
30
30
31
-
4. Sign up for Resend and generate an API key by following the instructions on the [Resend documentation](https://resend.com/docs/dashboard/api-keys/introduction).
31
+
4. Sign up for [Resend](https://resend.com/) and generate an API key by following the guide on the [Resend documentation](https://resend.com/docs/dashboard/api-keys/introduction).
32
32
33
-
### Queues access
34
-
35
-
Additionally, you will need access to Cloudflare Queues.
33
+
5. Additionally, you will need access to Cloudflare Queues.
36
34
37
35
<Renderfile="enable-queues" />
38
36
39
-
## 1. Create new Workers application
37
+
## 1. Create a new Workers application
40
38
41
39
To get started, create a Worker application using the [`create-cloudflare` CLI](https://github.com/cloudflare/workers-sdk/tree/main/packages/create-cloudflare). Open a terminal window and run the following command:
42
40
@@ -58,28 +56,28 @@ To get started, create a Worker application using the [`create-cloudflare` CLI](
58
56
}}
59
57
/>
60
58
61
-
Then, move into your newly created directory:
59
+
Then, go to your newly created directory:
62
60
63
61
```sh frame="none"
64
62
cd resend-rate-limit-queue
65
63
```
66
64
67
65
## 2. Set up a Queue
68
66
69
-
You need to create a Queue and a binding to your Worker. Run the following command to create a Queue named `rate-limit-queue`.
67
+
You need to create a Queue and a binding to your Worker. Run the following command to create a Queue named `rate-limit-queue`:
70
68
71
69
```sh title="Create a Queue"
72
70
npx wrangler queues create rate-limit-queue
73
71
```
74
72
75
-
```txt title="Output"
73
+
```sh output
76
74
Creating queue rate-limit-queue.
77
75
Created queue rate-limit-queue.
78
76
```
79
77
80
-
### Add Queue bindings to wrangler.toml
78
+
### Add Queue bindings to `wrangler.toml`
81
79
82
-
Next, in your `wrangler.toml` file, add the following:
80
+
In your `wrangler.toml` file, add the following:
83
81
84
82
```toml
85
83
[[queues.producers]]
@@ -93,9 +91,9 @@ max_batch_timeout = 10
93
91
max_retries = 3
94
92
```
95
93
96
-
Adding the `max_batch_size` of 2 to the consumer queue is important because the Resend API has a default rate limit of 2 requests per second. This batch size allows the queue to process the message in the batch size of 2. If the batch size is less than 2, the queue will wait for 10 seconds to collect the next message. If no more messages are available, the queue will process the message in the batch. For more information, refer to the [Batching, Retries and Delays documentation](/queues/configuration/batching-retries)
94
+
It is important to include the `max_batch_size` of two to the consumer queue is important because the Resend API has a default rate limit of two requests per second. This batch size allows the queue to process the message in the batch size of two. If the batch size is less than two, the queue will wait for 10 seconds to collect the next message. If no more messages are available, the queue will process the message in the batch. For more information, refer to the [Batching, Retries and Delays documentation](/queues/configuration/batching-retries)
97
95
98
-
Your final `wrangler.toml` file should look similar to the one below.
96
+
Your final `wrangler.toml` file should look similar to the example below.
99
97
100
98
```toml title="wrangler.toml"
101
99
#:schema node_modules/wrangler/config-schema.json
@@ -117,7 +115,7 @@ max_retries = 3
117
115
118
116
## 3. Add bindings to environment
119
117
120
-
Add the bindings to the environment interface in `worker-configuration.d.ts`, so TypeScript correctly types the bindings. Type the queue as `Queue<any>`. The following step will show you how to change this type.
118
+
Add the bindings to the environment interface in `worker-configuration.d.ts`, so TypeScript correctly types the bindings. Type the queue as `Queue<any>`. Refer to the following step for instructions on how to change this type.
121
119
122
120
```ts title="worker-configuration.d.ts"
123
121
interfaceEnv {
@@ -127,7 +125,7 @@ interface Env {
127
125
128
126
## 4. Send message to the queue
129
127
130
-
The application will send a message to the queue when this Worker receives a request. For simplicity, you will send the email address as a message to the queue. A new message will be sent to the queue with a delay of 1 second.
128
+
The application will send a message to the queue when the Worker receives a request. For simplicity, you will send the email address as a message to the queue. A new message will be sent to the queue with a delay of one second.
131
129
132
130
```ts title="src/index.ts"
133
131
exportdefault {
@@ -145,15 +143,15 @@ export default {
145
143
};
146
144
```
147
145
148
-
This will accept requests to any subpath and forwards the request's body. It expects that the request body only contains an email. In production, you should check that the request was a `POST` request. Moreover, you should avoid sending such sensitive information (email) directly to the queue. Instead, you can send a message to the queue that contains a unique identifier for the user. Then, your consumer queue can use the unique identifier to look up the email address in a database and use that to send the email.
146
+
This will accept requests to any subpath and forwards the request's body. It expects that the request body to contain only an email. In production, you should check that the request was a `POST` request. You should also avoid sending such sensitive information (email) directly to the queue. Instead, you can send a message to the queue that contains a unique identifier for the user. Then, your consumer queue can use the unique identifier to look up the email address in a database and use that to send the email.
149
147
150
148
## 5. Process the messages in the queue
151
149
152
150
After the message is sent to the queue, it will be processed by the consumer Worker. The consumer Worker will process the message and send the email.
153
151
154
152
Since you have not configured Resend yet, you will log the message to the console. After you configure Resend, you will use it to send the email.
155
153
156
-
Add the `queue()` handler as shown below.
154
+
Add the `queue()` handler as shown below:
157
155
158
156
```ts title="src/index.ts" ins={1-3,17-28}
159
157
interfaceMessage {
@@ -187,23 +185,21 @@ export default {
187
185
};
188
186
```
189
187
190
-
The above `queue()` handler will log the email address to the console and send the email. It will also retry the message if the email sending fails. The `delaySeconds` is set to 5 seconds to avoid sending the email too quickly.
188
+
The above `queue()` handler will log the email address to the console and send the email. It will also retry the message if sending the email fails. The `delaySeconds` is set to five seconds to avoid sending the email too quickly.
191
189
192
-
To test the application, run the following command.
190
+
To test the application, run the following command:
193
191
194
192
```sh title="Start the development server"
195
193
npm run dev
196
194
```
197
195
198
-
Use the following cURL command to send a request to the application.
196
+
Use the following cURL command to send a request to the application:
0 commit comments