Diffinery

homeflare/celldpublic

celld/docs/services/queues.md
74 lines · 3.6 KBRaw History
1# Queues
2
3[Queues](https://developers.cloudflare.com/queues/) is a message broker. A
4producer Worker sends a message, and a consumer Worker receives it later in a
5batch. Each queue is one celld cell. Read the
6[Cloudflare Queues documentation](https://developers.cloudflare.com/queues/configuration/javascript-apis/)
7for the API.
8
9## Example
10
11The [Queues example](../../examples/queues) sends one job from a request and
12processes the job in a queue handler. A job for a path that ends in `/fail`
13retries 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
33A `queues.producers` entry gives a `binding` and a `queue` name. Every producer
34in the fleet that names one queue writes to the same queue. celld enforces the
35[Cloudflare limits](https://developers.cloudflare.com/queues/platform/limits/):
36a message of at most 128,000 bytes, at most 100 messages and 256,000 bytes in
37one `sendBatch()` call, and a `delaySeconds` of at most 86,400. A producer entry
38can set `delivery_delay` as the default delay.
39
40`send()` resolves after the queue cell commits the message. The cell accepts at
41most 256 producer calls at a time, and it refuses a further call with a cell
42overload error that the producer can retry.
43
44## Consumers
45
46A `queues.consumers` entry names the `queue`. Only one script can consume a
47queue, so a deployment in which two scripts consume one queue fails. The entry
48sets `max_batch_size` (10 by default, 100 at most), `max_batch_timeout` in
49seconds (5 by default, 60 at most), `max_retries` (3 by default),
50`max_concurrency` (250 at most), `retry_delay`, and `dead_letter_queue`. celld
51validates each bound at deploy time. Read
52[Batching, retries, and delays](https://developers.cloudflare.com/queues/configuration/batching-retries/)
53for the meaning of each key.
54
55celld does not guarantee a delivery order, and it delivers a message at least
56once. `message.id` stays the same across a redelivery, so an application can use
57it as an idempotency key. A handler that never settles its batch holds the lease
58until the lease expires, and celld counts that as one failed delivery. celld
59adds 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
73The [Cloudflare compatibility](../cloudflare-compat.md#services) page lists the
74runtime APIs and the unsupported services.