You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -29,35 +29,28 @@ On the blog we show reference examples built upon these architectural patterns:
29
29
* NATS - an open source messaging system hosted by the [CNCF](https://www.cncf.io/)
30
30
* NATS JetStream - a messaging system built on top of NATS for durable queues and message streams
31
31
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.
36
35
37
36
Learn more about [NATS JetStream](https://docs.nats.io/nats-concepts/jetstream)
38
37
39
38
## Installation
40
39
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**
42
41
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).
44
43
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.
52
45
46
+
** External NATS server**
53
47
For production environments you should install NATS separately using its Helm chart.
54
48
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.
56
50
57
51
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.
58
52
59
53
```yaml
60
-
queueMode: jetstream
61
54
nats:
62
55
streamReplication: 3
63
56
external:
@@ -68,6 +61,8 @@ nats:
68
61
69
62
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.
70
63
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
+
71
66
## Features
72
67
73
68
### Queue-based scaling for functions
@@ -87,7 +82,7 @@ The `mode` parameter can be set to `static` or `function`.
87
82
88
83
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.
89
84
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.
91
86
92
87
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.
93
88
@@ -108,11 +103,9 @@ OpenFaaS ships with a “mixed queue”, where all invocations run in the same q
> 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
-
113
106
### Retries
114
107
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.
116
109
117
110
See: [retries](/openfaas-pro/retries)
118
111
@@ -131,118 +124,13 @@ jetstreamQueueWorker:
131
124

132
125
> Structured logs formatted for the console
133
126
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
145
128
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.
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.
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 forasync messages. Either due to a configuration changein 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.
246
134
247
135
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.
248
136
@@ -261,16 +149,16 @@ The stream is automatically created by the queue-worker of it does not exist. Si
261
149
```
262
150
263
151
3. Delete the queue-worker stream.
264
-
152
+
265
153
Keep in mind that deleting the stream removes any queued async invocations.
266
154
267
155
```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
-
exportSTREAM_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.
0 commit comments