Skip to content

Queues

Queues deliver messages from a producer Worker to a consumer Worker. Delivery is at-least-once. Durability comes from scheduler.sqlite on the node running ocd.

For example, you can use Queues for:

  • Decoupling producer and consumer Workers
  • Buffering work for asynchronous processing
  • Retrying failed deliveries
ts
export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    await env.QUEUE.send({ hello: "world" });
    return new Response("queued");
  },
  async queue(batch: MessageBatch<{ hello: string }>, env: Env): Promise<void> {
    for (const message of batch.messages) {
      console.log(message.body);
      message.ack();
    }
  },
} satisfies ExportedHandler<{ QUEUE: Queue }>;

Bind a producer in open-compute.json. Ordinary product bindings are { type, id, permissions? }:

json
{
  "name": "queue-app",
  "main": "src/index.ts",
  "bindings": {
    "QUEUE": { "type": "queue_producer", "id": "<queue-id>" }
  }
}

A consumer is the Worker's queue handler. open-compute.json does not use Wrangler [[queues.consumers]]. Binding grammar: bindings. The CLI is oc / oc run / oc types.

Compatibility

TopicCloudflareopen-compute
JavaScript APIQueues JavaScript APIsSame: send / sendBatch, contentType (json / text / bytes / v8), delaySeconds, metrics, consumer MessageBatch / ack / retry
DurabilityGlobal replicationLocal scheduler.sqlite on the node running ocd
DeliveryAt-least-onceAt-least-once
Global FIFOAvailableNot provided
Unknown native dispatchMay retain the lease; duplicate attempt numbers possible
Pull consumerAvailableNot provided
Bindingwrangler queuesProducer { type, id, permissions? }; consumer is the Worker queue handler

Next