Skip to content

Queues

Queues is a message broker. A producer sends a message, and a consumer receives it later in a batch. workerd keeps the messages in SQLite-backed Durable Objects under .wrangler/state/workerd/queues/. Read the Cloudflare Queues documentation for the API.

Example

The Queues example sends a job for each request and processes it in a queue handler. A job for a path that ends in /fail retries twice, and then the example reads it from a dead-letter queue.

index.jsjs
export default {
  async fetch(request, env) {
    const job = {
      path: new URL(request.url).pathname,
      createdAt: new Date().toISOString(),
    };
    await env.JOBS.send(job);
    return Response.json({ queued: job });
  },

  async queue(batch) {
    for (const message of batch.messages) {
      if (batch.queue === "example-jobs-dead-letter") {
        console.log("dead job", message.body);
        message.ack();
        continue;
      }

      if (message.body.path.endsWith("/fail")) {
        console.log("retrying job", message.body, message.attempts);
        message.retry({ delaySeconds: 1 });
        continue;
      }

      console.log("processing job", message.body);
      message.ack();
    }
  },
};
wrangler.jsoncjsonc
{
  "name": "queues",
  "main": "index.js",
  "compatibility_date": "2026-01-01",
  "no_bundle": true,
  "queues": {
    "producers": [{ "binding": "JOBS", "queue": "example-jobs" }],
    "consumers": [
      {
        "queue": "example-jobs",
        "max_batch_size": 10,
        "max_batch_timeout": 5,
        "max_retries": 2,
        "dead_letter_queue": "example-jobs-dead-letter"
      },
      { "queue": "example-jobs-dead-letter" }
    ]
  }
}

Queues is experimental, so it needs --experimental:

npx workerd serve --experimental ./queues/wrangler.jsonc
curl http://localhost:8787/hello
curl http://localhost:8787/job/fail

workerd logs processing job for /hello. For /job/fail, it logs retrying job three times and then dead job, in about 15 seconds.

Differences from Cloudflare

  • The consumer of a queue is the Worker of the same wrangler.jsonc as the producer. A Worker in another configuration cannot reach the queue.
  • A producer entry takes binding, queue, and remote. workerd ignores remote and refuses delivery_delay.
  • workerd clamps a consumer setting that is out of range, and does not refuse it: max_batch_size to 1 through 100, max_batch_timeout to 60 seconds, and max_concurrency to 1024.
  • workerd keeps a message for four days, and the period is fixed.
  • workerd has no pull consumers, no Queues HTTP API, and no event notifications or subscriptions.
  • Queues does not work in a cluster yet. If a configuration with queues joins a cluster with --cluster-addr, workerd does not start.

See also Cloudflare compatibility.

Type to search…

↑↓ navigate↵ selectEsc close