Skip to main content

Queues

Learning Focus

By the end of this lesson you will understand how Cloudflare Queues enable asynchronous processing between Workers and their free-tier limits.

What Are Cloudflare Queues?​

Cloudflare Queues is a message broker that allows Workers to send and consume messages asynchronously. Instead of processing everything in a single request, you can offload work to a queue and process it later in a separate Worker.

Use Cases​

Use CaseDescription
Email sendingQueue email tasks, process in background
Image processingResize/optimize images asynchronously
Webhook deliveryRetry failed webhook deliveries
Log aggregationBatch and process log entries
Data pipelineProcess data transformations step by step

Free Tier​

ResourceFree Plan
Messages sent1 million per month
Messages read1 million per month
Queues100

Basic Example​

Producer (Sends Messages)​

src/producer.ts
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const body = await request.json() as any;

// Send a message to the queue
await env.MY_QUEUE.send({
type: "email",
to: body.email,
subject: "Welcome!",
body: "Thanks for signing up.",
});

return Response.json({ status: "queued" });
},
};

Consumer (Processes Messages)​

src/consumer.ts
export default {
async queue(batch: MessageBatch, env: Env): Promise<void> {
for (const message of batch.messages) {
console.log(`Processing: ${JSON.stringify(message.body)}`);

// Process the message (e.g., send email, resize image)
// ...

// Acknowledge successful processing
message.ack();
}
},
};
wrangler.toml
name = "my-worker"
main = "src/producer.ts"

[[queues.producers]]
queue = "my-queue"
binding = "MY_QUEUE"

[[queues.consumers]]
queue = "my-queue"
max_batch_size = 10
max_batch_timeout = 30

Key Takeaways​

  • Queues enable asynchronous processing between Workers.
  • Free tier: 1M messages sent/read per month.
  • Messages are delivered in batches for efficiency.
  • Ideal for background tasks, retries, and data pipelines.

What's Next​