$npx -y skills add jackspace/ClaudeSkillz --skill cloudflare-queuesComplete knowledge domain for Cloudflare Queues - flexible message queue for asynchronous processing and background tasks on Cloudflare Workers. Use when: creating message queues, async processing, background jobs, batch processing, handling retries, configuring dead letter queue
| 1 | # Cloudflare Queues |
| 2 | |
| 3 | **Status**: Production Ready ✅ |
| 4 | **Last Updated**: 2025-10-21 |
| 5 | **Dependencies**: cloudflare-worker-base (for Worker setup) |
| 6 | **Latest Versions**: wrangler@4.43.0, @cloudflare/workers-types@4.20251014.0 |
| 7 | |
| 8 | --- |
| 9 | |
| 10 | ## Quick Start (10 Minutes) |
| 11 | |
| 12 | ### 1. Create a Queue |
| 13 | |
| 14 | ```bash |
| 15 | # Create a new queue |
| 16 | npx wrangler queues create my-queue |
| 17 | |
| 18 | # Output: |
| 19 | # ✅ Successfully created queue my-queue |
| 20 | |
| 21 | # List all queues |
| 22 | npx wrangler queues list |
| 23 | |
| 24 | # Get queue info |
| 25 | npx wrangler queues info my-queue |
| 26 | ``` |
| 27 | |
| 28 | ### 2. Set Up Producer (Send Messages) |
| 29 | |
| 30 | **wrangler.jsonc:** |
| 31 | |
| 32 | ```jsonc |
| 33 | { |
| 34 | "name": "my-producer", |
| 35 | "main": "src/index.ts", |
| 36 | "compatibility_date": "2025-10-11", |
| 37 | "queues": { |
| 38 | "producers": [ |
| 39 | { |
| 40 | "binding": "MY_QUEUE", // Available as env.MY_QUEUE |
| 41 | "queue": "my-queue" // Queue name from step 1 |
| 42 | } |
| 43 | ] |
| 44 | } |
| 45 | } |
| 46 | ``` |
| 47 | |
| 48 | **src/index.ts (Producer):** |
| 49 | |
| 50 | ```typescript |
| 51 | import { Hono } from 'hono'; |
| 52 | |
| 53 | type Bindings = { |
| 54 | MY_QUEUE: Queue; |
| 55 | }; |
| 56 | |
| 57 | const app = new Hono<{ Bindings: Bindings }>(); |
| 58 | |
| 59 | // Send single message |
| 60 | app.post('/send', async (c) => { |
| 61 | const body = await c.req.json(); |
| 62 | |
| 63 | await c.env.MY_QUEUE.send({ |
| 64 | userId: body.userId, |
| 65 | action: 'process-order', |
| 66 | timestamp: Date.now(), |
| 67 | }); |
| 68 | |
| 69 | return c.json({ status: 'queued' }); |
| 70 | }); |
| 71 | |
| 72 | // Send batch of messages |
| 73 | app.post('/send-batch', async (c) => { |
| 74 | const items = await c.req.json(); |
| 75 | |
| 76 | await c.env.MY_QUEUE.sendBatch( |
| 77 | items.map((item) => ({ |
| 78 | body: { userId: item.userId, action: item.action }, |
| 79 | })) |
| 80 | ); |
| 81 | |
| 82 | return c.json({ status: 'queued', count: items.length }); |
| 83 | }); |
| 84 | |
| 85 | export default app; |
| 86 | ``` |
| 87 | |
| 88 | ### 3. Set Up Consumer (Process Messages) |
| 89 | |
| 90 | **Create consumer Worker:** |
| 91 | |
| 92 | ```bash |
| 93 | # In a new directory |
| 94 | npm create cloudflare@latest my-consumer -- --type hello-world --ts |
| 95 | cd my-consumer |
| 96 | ``` |
| 97 | |
| 98 | **wrangler.jsonc:** |
| 99 | |
| 100 | ```jsonc |
| 101 | { |
| 102 | "name": "my-consumer", |
| 103 | "main": "src/index.ts", |
| 104 | "compatibility_date": "2025-10-11", |
| 105 | "queues": { |
| 106 | "consumers": [ |
| 107 | { |
| 108 | "queue": "my-queue", // Queue to consume from |
| 109 | "max_batch_size": 10, // Process up to 10 messages at once |
| 110 | "max_batch_timeout": 5 // Or wait max 5 seconds |
| 111 | } |
| 112 | ] |
| 113 | } |
| 114 | } |
| 115 | ``` |
| 116 | |
| 117 | **src/index.ts (Consumer):** |
| 118 | |
| 119 | ```typescript |
| 120 | export default { |
| 121 | async queue( |
| 122 | batch: MessageBatch, |
| 123 | env: Env, |
| 124 | ctx: ExecutionContext |
| 125 | ): Promise<void> { |
| 126 | console.log(`Processing batch of ${batch.messages.length} messages`); |
| 127 | |
| 128 | for (const message of batch.messages) { |
| 129 | console.log('Message:', message.id, message.body, `Attempt: ${message.attempts}`); |
| 130 | |
| 131 | // Your processing logic here |
| 132 | await processMessage(message.body); |
| 133 | } |
| 134 | |
| 135 | // Implicit acknowledgement: if this function returns without error, |
| 136 | // all messages are automatically acknowledged |
| 137 | }, |
| 138 | }; |
| 139 | |
| 140 | async function processMessage(body: any) { |
| 141 | // Process the message |
| 142 | console.log('Processing:', body); |
| 143 | } |
| 144 | ``` |
| 145 | |
| 146 | ### 4. Deploy and Test |
| 147 | |
| 148 | ```bash |
| 149 | # Deploy producer |
| 150 | cd my-producer |
| 151 | npm run deploy |
| 152 | |
| 153 | # Deploy consumer |
| 154 | cd my-consumer |
| 155 | npm run deploy |
| 156 | |
| 157 | # Test by sending a message |
| 158 | curl -X POST https://my-producer.<your-subdomain>.workers.dev/send \ |
| 159 | -H "Content-Type: application/json" \ |
| 160 | -d '{"userId": "123", "action": "welcome-email"}' |
| 161 | |
| 162 | # Watch consumer logs |
| 163 | npx wrangler tail my-consumer |
| 164 | ``` |
| 165 | |
| 166 | --- |
| 167 | |
| 168 | ## Complete Producer API |
| 169 | |
| 170 | ### `send()` - Send Single Message |
| 171 | |
| 172 | ```typescript |
| 173 | interface QueueSendOptions { |
| 174 | delaySeconds?: number; // Delay delivery (0-43200 seconds / 12 hours) |
| 175 | } |
| 176 | |
| 177 | await env.MY_QUEUE.send(body: any, options?: QueueSendOptions); |
| 178 | ``` |
| 179 | |
| 180 | **Examples:** |
| 181 | |
| 182 | ```typescript |
| 183 | // Simple send |
| 184 | await env.MY_QUEUE.send({ userId: '123', action: 'send-email' }); |
| 185 | |
| 186 | // Send with delay (10 minutes) |
| 187 | await env.MY_QUEUE.send( |
| 188 | { userId: '123', action: 'reminder' }, |
| 189 | { delaySeconds: 600 } |
| 190 | ); |
| 191 | |
| 192 | // Send structured data |
| 193 | await env.MY_QUEUE.send({ |
| 194 | type: 'order-confirmation', |
| 195 | orderId: 'ORD-123', |
| 196 | email: 'user@example.com', |
| 197 | items: [{ sku: 'ITEM-1', quantity: 2 }], |
| 198 | total: 49.99, |
| 199 | timestamp: Date.now(), |
| 200 | }); |
| 201 | ``` |
| 202 | |
| 203 | **CRITICAL:** |
| 204 | - Message body must be JSON serializable (structured clone algorithm) |
| 205 | - Maximum message size: **128 KB** (including ~100 bytes internal metadata) |
| 206 | - Messages >128 KB will fail - split them or store in R2 and send reference |
| 207 | |
| 208 | - |