Documentation

Cloudflare Queues & Background Jobs

ready

Asynchronous message queuing, background worker jobs, granular ack/retry delivery, and Dead-Letter Queues on Cloudflare Workers.

Source reference: cli/internal/serverless/cloudflare/queues.go, shared/nextcompile/entry.go


Overview

Web applications frequently offload heavy or non-critical operations (sending transactional emails, generating invoices, processing analytics events, running AI embeddings) out of the user-facing request path.

Cloudflare Queues provides reliable, at-least-once message delivery between Cloudflare Workers without managing Redis or Kafka clusters.


Configuration in nextdeploy.yml

Declare your queues under resources.queues (including any Dead-Letter Queue) and wire producer + consumer settings under bindings.queues:

version: "1.0"
target_type: serverless

app:
  name: my-app
  domain: app.example.com
  environment: production

serverless:
  provider: cloudflare
  cloudflare:
    compatibility_date: "2026-05-01"
    compatibility_flags:
      - nodejs_compat
    resources:
      queues:
        - name: transactional-emails
        - name: email-dlq
    bindings:
      queues:
        producers:
          - name: EMAIL_QUEUE
            queue: transactional-emails
        consumers:
          - queue: transactional-emails
            max_retries: 5
            max_batch_size: 10
            max_batch_timeout: 5
            max_concurrency: 4
            retry_delay: 30
            dead_letter_queue: email-dlq

Publishing Messages from Next.js Server Actions

Access the Queue binding via globalThis.__nextdeployEnv in Server Actions or Route Handlers:

// app/actions/signup.ts
'use server';

export async function registerUser(formData: FormData) {
  const email = formData.get('email') as string;
  const env = (globalThis as { __nextdeployEnv?: CloudflareEnv }).__nextdeployEnv;

  await env?.EMAIL_QUEUE.send({
    type: 'WELCOME_EMAIL',
    recipient: email,
    timestamp: Date.now(),
  });

  return { success: true };
}

Processing Messages in /api/queue/[queueName]

NextDeploy's generated Worker queue(batch, env, ctx) handler dispatches each batch internally to /api/queue/<queueName> (external fetch() requests to /api/queue/* and /api/cron are automatically rejected with 403 Forbidden):

// app/api/queue/[name]/route.ts
import { NextRequest, NextResponse } from 'next/server';

export async function POST(req: NextRequest) {
  const { messages } = await req.json();
  const ack: string[] = [];
  const retry: string[] = [];

  for (const msg of messages) {
    try {
      await sendEmail(msg.body.recipient);
      ack.push(msg.id);
    } catch {
      retry.push(msg.id);
    }
  }

  return NextResponse.json({ ack, retry });
}
  • Returning { ack: [...], retry: [...] } acknowledges and retries individual messages cleanly.
  • Returning any 2xx response without { ack, retry } calls batch.ackAll().
  • Returning any non-2xx response calls batch.retryAll() and throws so Cloudflare's max_retries, retry_delay, and dead_letter_queue policies trigger reliably.

Related