Diffinery

homeflare/celldpublic

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