Skip to content

Commit 669031f

Browse files
weltekialexellis
authored andcommitted
Update Jetstream queue-worker configuration
Signed-off-by: Han Verstraete (OpenFaaS Ltd) <han@openfaas.com>
1 parent 9fb0c40 commit 669031f

File tree

1 file changed

+22
-134
lines changed

1 file changed

+22
-134
lines changed

docs/openfaas-pro/jetstream.md

Lines changed: 22 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,28 @@ On the blog we show reference examples built upon these architectural patterns:
2929
* NATS - an open source messaging system hosted by the [CNCF](https://www.cncf.io/)
3030
* NATS JetStream - a messaging system built on top of NATS for durable queues and message streams
3131

32-
1. A JetStream Server is the NATS server, running in *jetstream* mode
33-
2. A Stream is a message store it is used in OpenFaaS to queue async invocation messages.
34-
3. A Consumer is a stateful view of a stream when clients consume messages from a stream the consumer keeps track of which messages were delivered and acknowledged.
35-
5. A Subscriber is what the queue-worker creates to start pulling messages from the stream.
32+
1. A JetStream Server is the original NATS Core project, running in "jetstream mode"
33+
2. A Stream is a message store it is used in OpenFaaS to queue async invocation messages.
34+
3. A Consumer is a stateful view of a stream when clients consume messages from a stream the consumer keeps track of which messages were delivered and acknowledged.
3635

3736
Learn more about [NATS JetStream](https://docs.nats.io/nats-concepts/jetstream)
3837

3938
## Installation
4039

41-
For staging and development environments OpenFaaS can be deployed with an embedded version of the NATS server which uses an in-memory store.
40+
**Embedded NATS server**
4241

43-
To enable JetSteam for OpenFaaS set `jetstream` as the queue mode in the values.yaml file of the [OpenFaaS Helm chart](https://github.com/openfaas/faas-netes/blob/master/chart/openfaas/README.md)
42+
For staging and development environments OpenFaaS can be deployed with an embedded version of the NATS server which uses an in-memory store. This is the default when you install OpenFaaS using the [OpenFaaS Helm chart](https://github.com/openfaas/faas-netes/blob/master/chart/openfaas/README.md).
4443

45-
```yaml
46-
queueMode: jetstream
47-
nats:
48-
streamReplication: 1
49-
```
50-
51-
If the NATS Pod restarts, you will lose all messages that it contains. In your development or staging environment. This could happen if you update the chart and the version of the NATS server has changed, or if a node is removed from the cluster.
44+
If the NATS Pod restarts, you will lose all messages that it contains. This could happen if you update the chart and the version of the NATS server has changed, or if a node is removed from the cluster.
5245

46+
** External NATS server**
5347
For production environments you should install NATS separately using its Helm chart.
5448

55-
NATS can be configured with a quorum of at least 3 replicas so it can recover data if one of the replicas should crash. You can also enable a persistent volume in the NATS chart for additional durability.
49+
NATS can be configured with a quorum of at least 3 replicas so it can recover data if one of the replicas should crash. You can also enable a persistent volume in the NATS chart for additional durability.
5650

5751
If you are running with 3 replicas of the NATS server, then update the OpenFaaS chart to reflect that in the `nats.streamReplication` parameter. With this in place, the stream for queued messages will be replicated across the 3 NATS servers.
5852

5953
```yaml
60-
queueMode: jetstream
6154
nats:
6255
streamReplication: 3
6356
external:
@@ -68,6 +61,8 @@ nats:
6861
6962
By default the NATS helm chart will be installed into the nats namespace with the name of `nats`, but you can customise this if you wish by setting the `nats.external.host` parameter.
7063

64+
Instructions for a recommended NATS production deployment are available for customers though the [customer community repo](https://github.com/openfaas/customers/blob/master/jetstream.md)
65+
7166
## Features
7267

7368
### Queue-based scaling for functions
@@ -87,7 +82,7 @@ The `mode` parameter can be set to `static` or `function`.
8782

8883
If set to `static`, the queue-worker will scale its NATS Consumers based upon the number of replicas of the queue-worker. This is the default mode, and ideal for development, or constrained environments.
8984

90-
If set to `function`, the queue-worker will scale its NATS Consumers based upon the number of functions that are active in the queue. This is ideal for production environments where you want to scale your functions based upon the queue depth. It also gives messages queued at different times a fairer chance of being processed earlier.
85+
If set to `function`, the queue-worker will scale its NATS Consumers based upon the number of functions that are active in the queue. This is ideal for production environments where you want to [scale your functions based upon the queue depth](/reference/autoscaling/). It also gives messages queued at different times a fairer chance of being processed earlier.
9186

9287
The `inactiveThreshold` parameter can be used to set the threshold for when a function is considered inactive. If a function is inactive for longer than the threshold, the queue-worker will delete the NATS Consumer for that function.
9388

@@ -108,11 +103,9 @@ OpenFaaS ships with a “mixed queue”, where all invocations run in the same q
108103

109104
See: [multiple queues](/reference/async/#multiple-queues)
110105

111-
> If the capacity of your queue does not fit within the default limits described [here](#configure-jetstream) you will need to follow these steps to [create a Stream and Consumer manually](#configure-streams-and-consumers-manually) for each queue.
112-
113106
### Retries
114107

115-
Users can specify a list of HTTP codes that should be retried a number of times using an exponential back-off algorithm to mitigate the impact associated with retrying messages.
108+
Users can specify a list of HTTP codes that should be retried a number of times using an exponential back-off algorithm to mitigate the impact associated with retrying messages.
116109

117110
See: [retries](/openfaas-pro/retries)
118111

@@ -131,118 +124,13 @@ jetstreamQueueWorker:
131124
![Structured logs formatted for the console](https://www.openfaas.com/images/2022-07-jetstream-for-openfaas/structured-logs.png)
132125
> Structured logs formatted for the console
133126

134-
## Configure JetStream
135-
136-
Every OpenFaaS async queue requires a Stream and Consumer to be created on the JetStream server. By default the queue-worker manages these for you and ensures they are created on startup if they do not exist.
137-
138-
Stream and Consumers can be managed manually although it is not recommended.
139-
140-
### Configure Streams and Consumers manually
141-
142-
Streams and Consumers can be defined manually, typically using the [NATS CLI tool](https://docs.nats.io/running-a-nats-service/configuration/resource_management/configuration_mgmt/nats-admin-cli).
143-
144-
>A [Kubernetes controller](https://docs.nats.io/running-a-nats-service/configuration/resource_management/configuration_mgmt/kubernetes_controller) is available for managing Streams and Consumers declaratively. This can be used if you are using a CD system like ArgoCD or Flux.
127+
## Clear or reset a queue
145128

146-
You can use [arkade](https://github.com/alexellis/arkade) to install the NATS CLI.
147-
```
148-
arkade get nats
149-
```
150-
151-
Port forward the nats server to your localhost so you can use the cli to interact with it.
152-
```bash
153-
kubectl port-forward -n openfaas svc/nats 4222:4222
154-
```
155-
#### Create a Stream
156-
157-
The Stream will need to be created first. In this example we will create the Stream for the shared queue, `faas-request`. We recommend giving the stream the same name as the queue.
129+
From time to time, you may wish to reset or purge an async message queue. You may have generated a large number of unnecessary messages that you want to remove from the queue.
158130

159-
```bash
160-
export QUEUE_NAME=faas-request
131+
[Cancel async invocations](/reference/async/#cancel-async-invocations)
161132

162-
nats stream create $QUEUE_NAME \
163-
--subjects=$QUEUE_NAME \
164-
--replicas=1 \
165-
--retention=work \
166-
--discard=old \
167-
--max-msgs-per-subject=-1
168-
```
169-
170-
Messages intended for a queue are published to a NATS subject, `faas-request` by default and the queue name for a [dedicated queue](/reference/async/#multiple-queues). A Stream should only bind the subject for the queue that it will be associated with. This can be configured with the `--subjects` flag.
171-
172-
The `--replicas` flag is used to configure the stream replication factor. This should be at least 3 for production environments.
173-
174-
The command above includes the required settings for using the Stream with a queue-worker. The queue-worker requires a retention policy of type `work (WorkQueuePolicy)` . You will get prompted interactively for the remaining stream information. Use the defaults or configure your own storage and limits for the stream.
175-
176-
```
177-
? Storage file
178-
? Stream Messages Limit -1
179-
? Total Stream Size -1
180-
? Message TTL -1
181-
? Max Message Size -1
182-
? Duplicate tracking time window 2m0s
183-
? Allow message Roll-ups No
184-
? Allow message deletion Yes
185-
? Allow purging subjects or the entire stream Yes
186-
```
187-
188-
#### Create a Consumer
189-
190-
Once the Stream has been created the Consumer can be added. We recommend naming giving the consumer the same name as the queue with the suffix `-workers` added to it. The consumer for the shared `faas-request` queue would be named `faas-request-workers`.
191-
192-
```bash
193-
export QUEUE_NAME=faas-request
194-
195-
nats consumer \
196-
create $QUEUE_NAME $QUEUE_NAME-workers \
197-
--pull \
198-
--deliver=all \
199-
--ack=explicit \
200-
--replay=instant \
201-
--max-deliver=-1 \
202-
--max-pending=-1 \
203-
--no-headers-only \
204-
--backoff=none \
205-
--wait=3m \
206-
--max-waiting=512 \
207-
--defaults
208-
```
209-
210-
This command creates a pull consumer that makes available all messages for every subject on the stream. We require that each message is acknowledged explicitly.
211-
212-
Important configuration flags:
213-
- The queue-worker will control how many times a message can be redelivered,`--max-deliver` has to be set to `-1` to allow unlimited deliveries.
214-
215-
- The queue-worker automatically extends the ack window for functions that require more time to complete. In order to prevent us from having to extend the ack window to often we recommend configuring a default acknowledgement waiting time of 3 minutes. This can be configured with the `--wait` flag.
216-
217-
- The `--max-pending` flag limits the number of messages that can have a pending status. Messages that are queued for retries are also considered pending. It is set to `-1` to allow an unlimited number of pending messages by default. The consumer is paused and no new messages are delivered when this limit is reached. If you select a customer value it should be at least `max_inflight * queue-worker-replicas + buffer`. The size of the buffer depends on the number of retries your queue needs to be able to handle.
218-
219-
This value can always be update later:
220-
221-
```bash
222-
nats consumer edit --max-pending 6000
223-
```
224-
225-
- The `--max-waiting` flag limits the number of subscribers a queue-worker can create. We recommend using the default value of 512 but the value should be at least equal to the number of queue worker replicas.
226-
227-
#### Configure the queue-worker
228-
As a final step the queue-worker needs to be configured to use the externally created Stream and Consumer.
229-
230-
For the shared OpenFaaS queue edit the values.yaml file of the OpenFaaS chart. The name of the Consumer used by the queue-worker is set with `jetstreamQueueWorker.durableName`. The name of the Stream needs to be set with `nats.channel`.
231-
232-
```yaml
233-
jetstreamQueueWorker:
234-
durableName: faas-workers
235-
236-
nats:
237-
streamReplication: 1
238-
channel: faas-request-workers
239-
```
240-
241-
A dedicated queue using the [queue-worker Helm chart](https://github.com/openfaas/faas-netes/tree/master/chart/queue-worker) can be configured by setting the `nats.stream.name` and `nats.consumer.durableName` parameters.
242-
243-
## Reset the stream for async messages
244-
245-
From time to time, you may wish to reset or purge the stream for async messages. Either due to a configuration change in the stream that can not be applied automatically, or because you have generated a large number of unnecessary messages you want to remove from the queue.
133+
Each queue-worker deployment creates a dedicated NATS Stream to store async invocation messages. On startup the queue-worker will create this stream if it does not exist. To reset Keep in mind that all queued invocations will be lost.
246134

247135
The stream is automatically created by the queue-worker of it does not exist. Simply deleting the stream and restarting the queue-worker will reset the stream and consumer.
248136

@@ -261,16 +149,16 @@ The stream is automatically created by the queue-worker of it does not exist. Si
261149
```
262150

263151
3. Delete the queue-worker stream.
264-
152+
265153
Keep in mind that deleting the stream removes any queued async invocations.
266154

267155
```bash
268-
# This example deletes the default queue-worker stream.
269-
# Replace the stream name for dedicated queues
270-
# deployed with the queue-worker Helm chart.
271-
export STREAM_NAME=faas-request
156+
# This example deletes strean for the OpenFaaS default queue named faas-request.
157+
# The stream name is always the queue name prefixed with OF_.
158+
# Replace the queue name with the name of the queue stream you want to delete.
159+
export QUEUE_NAME=faas-request
272160
273-
nats stream delete $STREAM_NAME
161+
nats stream delete OF_$QUEUE_NAME
274162
```
275163

276164
4. Restart the queue-worker deployment.

0 commit comments

Comments
 (0)