| 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 it later in a |
| 5 | batch. Each queue is one celld cell. Read the |
| 6 | [Cloudflare Queues documentation](https://developers.cloudflare.com/queues/configuration/javascript-apis/) |
| 7 | for the API. |
| 8 | |
| 9 | ## Example |
| 10 | |
| 11 | The [Queues example](../../examples/queues) sends one job from a request and |
| 12 | processes the job in a queue handler. A job for a path that ends in `/fail` |
| 13 | retries twice, and the example then reads it from a dead-letter queue. |
| 14 | |
| 15 | <!-- celld-example: queues --> |
| 16 | |
| 17 | ## API |
| 18 | |
| 19 | - `env.JOBS.send(body, { contentType, delaySeconds })` sends one message. |
| 20 | `contentType` is `"v8"`, `"json"`, `"text"`, or `"bytes"`. |
| 21 | - `env.JOBS.sendBatch(messages)` sends up to 100 messages in one call. |
| 22 | - The consumer script exports `queue(batch, env, ctx)`. `batch.queue` names the |
| 23 | queue, and each entry of `batch.messages` has an `id`, a `timestamp`, a |
| 24 | `body`, and an `attempts` count. |
| 25 | - `message.ack()` and `message.retry({ delaySeconds })` settle one message. |
| 26 | `batch.ackAll()` and `batch.retryAll()` settle the whole batch. The first |
| 27 | call for a message wins. |
| 28 | - A handler that returns acknowledges every unsettled message. A handler that |
| 29 | throws retries every message that it did not acknowledge. |
| 30 | |
| 31 | ## Producers |
| 32 | |
| 33 | A `queues.producers` entry gives a `binding` and a `queue` name. Every producer |
| 34 | in the fleet that names one queue writes to the same queue. celld enforces the |
| 35 | [Cloudflare limits](https://developers.cloudflare.com/queues/platform/limits/): |
| 36 | a message of at most 128,000 bytes, at most 100 messages and 256,000 bytes in |
| 37 | one `sendBatch()` call, and a `delaySeconds` of at most 86,400. A producer entry |
| 38 | can set `delivery_delay` as the default delay. |
| 39 | |
| 40 | `send()` resolves after the queue cell commits the message. The cell accepts at |
| 41 | most 256 producer calls at a time, and it refuses a further call with a cell |
| 42 | overload error that the producer can retry. |
| 43 | |
| 44 | ## Consumers |
| 45 | |
| 46 | A `queues.consumers` entry names the `queue`. Only one script can consume a |
| 47 | queue, so a deployment in which two scripts consume one queue fails. The entry |
| 48 | sets `max_batch_size` (10 by default, 100 at most), `max_batch_timeout` in |
| 49 | seconds (5 by default, 60 at most), `max_retries` (3 by default), |
| 50 | `max_concurrency` (250 at most), `retry_delay`, and `dead_letter_queue`. celld |
| 51 | validates each bound at deploy time. Read |
| 52 | [Batching, retries, and delays](https://developers.cloudflare.com/queues/configuration/batching-retries/) |
| 53 | for the meaning of each key. |
| 54 | |
| 55 | celld does not guarantee a delivery order, and it delivers a message at least |
| 56 | once. `message.id` stays the same across a redelivery, so an application can use |
| 57 | it as an idempotency key. A handler that never settles its batch holds the lease |
| 58 | until the lease expires, and celld counts that as one failed delivery. celld |
| 59 | adds no exponential backoff to a retry. Compute the delay from |
| 60 | `message.attempts`. A message that passes `max_retries` moves to the |
| 61 | `dead_letter_queue`, or celld deletes it when the consumer names none. |
| 62 | |
| 63 | ## Differences from Cloudflare |
| 64 | |
| 65 | - A queue has one writer. Use more queues to increase write capacity. |
| 66 | - A queue owner accepts at most 256 concurrent producer calls. It refuses an |
| 67 | additional call, which the producer can retry. |
| 68 | - celld retains a message for four days. This period is not configurable. |
| 69 | - Pull consumers, the Queues HTTP API, dashboard controls, manual consumer |
| 70 | attachment, R2 event notifications, and Queue event subscriptions are |
| 71 | unavailable. |
| 72 | |
| 73 | The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists the |
| 74 | runtime APIs and the unsupported services. |