| 1 | # Queues |
| 2 | |
| 3 | [Queues](https://developers.cloudflare.com/queues/) is a message broker. A |
| 4 | producer Worker sends a message, and a consumer Worker receives the message |
| 5 | later in a batch. An application uses a queue to move work out of a request, |
| 6 | such as an email, a webhook call, a thumbnail, or a search index update. Each |
| 7 | queue is one celld cell, so a message lives in that cell's SQLite database |
| 8 | until a consumer acknowledges it. Read the |
| 9 | [Cloudflare Queues documentation](https://developers.cloudflare.com/queues/configuration/javascript-apis/) |
| 10 | for the standard API behavior. |
| 11 | |
| 12 | ## Example |
| 13 | |
| 14 | The [Queues example](../../examples/queues) sends one job from a request and |
| 15 | processes the job in a queue handler. A job for a path that ends in `/fail` |
| 16 | retries twice, and the example then reads it from a dead-letter queue. |
| 17 | |
| 18 | <!-- celld-example: queues --> |
| 19 | |
| 20 | ## Sending a message |
| 21 | |
| 22 | A project declares a producer with a `queues.producers` entry that gives a |
| 23 | `binding` name and a `queue` name. `env.JOBS.send(body)` sends one message, and |
| 24 | `env.JOBS.sendBatch(messages)` sends up to 100 messages in one call. celld |
| 25 | resolves the queue name to one cell, therefore every producer in the fleet that |
| 26 | names `example-jobs` writes to the same queue. |
| 27 | |
| 28 | A message carries a body and a content type. The `contentType` option selects |
| 29 | `"v8"`, `"json"`, `"text"`, or `"bytes"`, and the `queues_json_messages` |
| 30 | compatibility flag chooses the default, as on Cloudflare. celld enforces the |
| 31 | [Cloudflare limits](https://developers.cloudflare.com/queues/platform/limits/): |
| 32 | a message of at most 128,000 bytes, a batch of at most 100 messages, and at most |
| 33 | 256,000 bytes in one `sendBatch()` call. A `delaySeconds` option holds a message |
| 34 | invisible for up to 86,400 seconds, and a producer entry can set |
| 35 | `delivery_delay` as the default for every message of that binding. |
| 36 | |
| 37 | `send()` resolves after the queue cell commits the message, so a producer learns |
| 38 | that the message is durable. celld joins the concurrent sends of one queue into |
| 39 | one transaction, because a separate commit for each call makes durability the |
| 40 | cost of the queue. The cell accepts at most 256 producer calls at a time, and it |
| 41 | refuses a further call with a cell overload error that the producer can retry. |
| 42 | One queue has one writer, so an application that needs more write capacity |
| 43 | spreads its messages across more queues. |
| 44 | |
| 45 | ## The consumer and the batch |
| 46 | |
| 47 | A project declares a consumer with a `queues.consumers` entry that names the |
| 48 | `queue`. One script consumes a queue, and a deployment in which two scripts |
| 49 | consume one queue fails. The consumer entry sets `max_batch_size` (10 by |
| 50 | default, 100 at most), `max_batch_timeout` in seconds (5 by default, 60 at |
| 51 | most), `max_retries` (3 by default), `max_concurrency` (250 at most), |
| 52 | `retry_delay`, and `dead_letter_queue`. celld validates each bound at deploy |
| 53 | time, so a bad value fails the deployment instead of the first delivery. Read |
| 54 | [Batching, retries, and delays](https://developers.cloudflare.com/queues/configuration/batching-retries/) |
| 55 | for the upstream meaning of each key. |
| 56 | |
| 57 | The queue cell forms the batch. It closes a batch when `max_batch_size` messages |
| 58 | are ready, or when `max_batch_timeout` passes after the oldest ready message, |
| 59 | whichever arrives first. A batch can therefore hold fewer messages than |
| 60 | `max_batch_size`. The cell leases the batch and calls the |
| 61 | `queue(batch, env, ctx)` handler that the consumer script exports. That handler |
| 62 | runs in a stateless isolate and not inside the queue cell, so a slow handler |
| 63 | does not block a producer. `max_concurrency` bounds how many batches of one |
| 64 | queue can run at the same time. |
| 65 | |
| 66 | `batch.queue` names the queue, and `batch.messages` holds the messages. Each |
| 67 | message has an `id`, a `timestamp`, the decoded `body`, and an `attempts` count |
| 68 | that is 1 on the first delivery. celld does not guarantee a delivery order, and |
| 69 | [Cloudflare gives the same rule](https://developers.cloudflare.com/queues/reference/how-queues-works/). |
| 70 | A retry, a per-message delay, and a concurrent batch each move a message away |
| 71 | from its send order, so an application that needs an order must carry that order |
| 72 | in the message body. |
| 73 | |
| 74 | ## Acknowledgement and retry |
| 75 | |
| 76 | celld leases a batch instead of deleting it, so a message survives a consumer |
| 77 | that fails. Each message settles once. `message.ack()` marks one message as |
| 78 | done, and `message.retry()` returns one message to the queue. `batch.ackAll()` |
| 79 | and `batch.retryAll()` apply the same two outcomes to the whole batch. The first |
| 80 | call for a message wins, therefore a later call on that message does nothing. |
| 81 | |
| 82 | A handler that returns without a call acknowledges every message of its batch. A |
| 83 | handler that throws returns every message to the queue, except a message that |
| 84 | the handler acknowledged before the throw. A handler that never settles its |
| 85 | batch holds the lease until the lease expires, and celld then counts the batch |
| 86 | as one failed delivery. |
| 87 | |
| 88 | Every failed delivery raises `attempts` by one and makes the message visible |
| 89 | again. A `delaySeconds` option on `retry()` or `retryAll()` sets the delay, and |
| 90 | the consumer's `retry_delay` is the default. celld adds no exponential backoff, |
| 91 | so an application that wants one computes the delay from `message.attempts`. |
| 92 | |
| 93 | A message leaves the retry loop when `attempts` passes `max_retries`. celld |
| 94 | moves the message to the queue that `dead_letter_queue` names, which is an |
| 95 | ordinary queue and can have its own consumer. celld deletes a message that |
| 96 | reaches the bound when the consumer configures no dead-letter queue. Read |
| 97 | [Dead letter queues](https://developers.cloudflare.com/queues/configuration/dead-letter-queues/) |
| 98 | for the upstream behavior. |
| 99 | |
| 100 |  |
| 101 | |
| 102 | celld delivers a message at least once, as Cloudflare does. A consumer can crash |
| 103 | after its side effect and before its acknowledgement, and the message then |
| 104 | returns to the queue and runs again. A consumer must therefore tolerate a |
| 105 | duplicate. `message.id` stays the same across a redelivery, so an application |
| 106 | can use it as an idempotency key. |
| 107 | |
| 108 | celld keeps a message for four days after the send. A sweeper removes an older |
| 109 | message, even when no consumer read it, so a queue with no working consumer |
| 110 | loses its backlog instead of growing without a bound. |
| 111 | |
| 112 | ## Differences from Cloudflare |
| 113 | |
| 114 | - A queue has one writer. Use more queues to increase write capacity. |
| 115 | - A queue owner accepts at most 256 concurrent producer calls. It refuses an |
| 116 | additional call, which the producer can retry. |
| 117 | - celld retains a message for four days. This period is not configurable. |
| 118 | - Pull consumers, the Queues HTTP API, dashboard controls, manual consumer |
| 119 | attachment, R2 event notifications, and Queue event subscriptions are |
| 120 | unavailable. |
| 121 | |
| 122 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists the |
| 123 | runtime APIs and the unsupported services. |