Account Information

Account Information

JetStream is multi-tenant so you will need to check that your account is enabled for JetStream and is not limited. You can view your limits as follows:

  1. $ nats account info
  2. Connection Information:
  3. Client ID: 8
  4. Client IP: 127.0.0.1
  5. RTT: 178.545µs
  6. Headers Supported: true
  7. Maximum Payload: 1.0 MiB
  8. Connected URL: nats://localhost:4222
  9. Connected Address: 127.0.0.1:4222
  10. Connected Server ID: NCCOHA6ONXJOGAEZP4WPU4UJ3IQP2VVXEPRKTQCGBCW4IL4YYW4V4KKL
  11. JetStream Account Information:
  12. Memory: 0 B of 5.7 GiB
  13. Storage: 0 B of 11 GiB
  14. Streams: 0 of Unlimited
  15. Max Consumers: unlimited

Streams

The first step is to set up storage for our ORDERS related messages, these arrive on a wildcard of subjects all flowing into the same Stream and they are kept for 1 year.

Creating

  1. $ nats str add ORDERS
  2. ? Subjects to consume ORDERS.*
  3. ? Storage backend file
  4. ? Retention Policy Limits
  5. ? Discard Policy Old
  6. ? Message count limit -1
  7. ? Message size limit -1
  8. ? Maximum message age limit 1y
  9. ? Maximum individual message size [? for help] (-1) -1
  10. ? Number of replicas to store 3
  11. Stream ORDERS was created
  12. Information for Stream ORDERS
  13. Configuration:
  14. Subjects: ORDERS.*
  15. Acknowledgements: true
  16. Retention: File - Limits
  17. Replicas: 3
  18. Maximum Messages: -1
  19. Maximum Bytes: -1
  20. Maximum Age: 8760h0m0s
  21. Maximum Message Size: -1
  22. Maximum Consumers: -1
  23. Statistics:
  24. Messages: 0
  25. Bytes: 0 B
  26. FirstSeq: 0
  27. LastSeq: 0
  28. Active Consumers: 0

You can get prompted interactively for missing information as above, or do it all on one command. Pressing ? in the CLI will help you map prompts to CLI options:

  1. $ nats str add ORDERS --subjects "ORDERS.*" --ack --max-msgs=-1 --max-bytes=-1 --max-age=1y --storage file --retention limits --max-msg-size=-1 --discard old --replicas 3

Additionally, one can store the configuration in a JSON file, the format of this is the same as $ nats str info ORDERS -j | jq .config:

  1. $ nats str add ORDERS --config orders.json

Listing

We can confirm our Stream was created:

  1. $ nats str ls
  2. Streams:
  3. ORDERS

Querying

Information about the configuration of the Stream can be seen, and if you did not specify the Stream like below, it will prompt you based on all known ones:

  1. $ nats str info ORDERS
  2. Information for Stream ORDERS
  3. Configuration:
  4. Subjects: ORDERS.*
  5. No Acknowledgements: false
  6. Retention: File - Limits
  7. Replicas: 1
  8. Maximum Messages: -1
  9. Maximum Bytes: -1
  10. Maximum Age: 8760h0m0s
  11. Maximum Consumers: -1
  12. Statistics:
  13. Messages: 0
  14. Bytes: 0 B
  15. FirstSeq: 0
  16. LastSeq: 0
  17. Active Consumers: 0

Most commands that show data as above support -j to show the results as JSON:

  1. $ nats str info ORDERS -j
  2. {
  3. "config": {
  4. "name": "ORDERS",
  5. "subjects": [
  6. "ORDERS.*"
  7. ],
  8. "retention": "limits",
  9. "max_consumers": -1,
  10. "max_msgs": -1,
  11. "max_bytes": -1,
  12. "max_age": 31536000000000000,
  13. "storage": "file",
  14. "num_replicas": 1
  15. },
  16. "stats": {
  17. "messages": 0,
  18. "bytes": 0,
  19. "first_seq": 0,
  20. "last_seq": 0,
  21. "consumer_count": 0
  22. }
  23. }

This is the general pattern for the entire nats utility as it relates to JetStream - prompting for needed information but every action can be run non-interactively making it usable as a cli api. All information output like seen above can be turned into JSON using -j.

In clustered mode additional information will be included:

  1. $ nats str info ORDERS
  2. ...
  3. Cluster Information:
  4. Name: JSC
  5. Leader: S1
  6. Replica: S3, current, seen 0.04s ago
  7. Replica: S2, current, seen 0.04s ago

Here the cluster name is configured as JSC, there is a server S1 that’s the current leader with S3 and S2 are replicas. Both replicas are current and have been seen recently.

Copying

A stream can be copied into another, which also allows the configuration of the new one to be adjusted via CLI flags:

  1. $ nats str cp ORDERS ARCHIVE --subjects "ORDERS_ARCVHIVE.*" --max-age 2y
  2. Stream ORDERS was created
  3. Information for Stream ARCHIVE
  4. Configuration:
  5. Subjects: ORDERS_ARCVHIVE.*
  6. ...
  7. Maximum Age: 17520h0m0s
  8. ...

Editing

A stream configuration can be edited, which allows the configuration to be adjusted via CLI flags. Here I have a incorrectly created ORDERS stream that I fix:

  1. $ nats str info ORDERS -j | jq .config.subjects
  2. [
  3. "ORDERS.new"
  4. ]
  5. $ nats str edit ORDERS --subjects "ORDERS.*"
  6. Stream ORDERS was updated
  7. Information for Stream ORDERS
  8. Configuration:
  9. Subjects: ORDERS.*
  10. ....

Additionally one can store the configuration in a JSON file, the format of this is the same as $ nats str info ORDERS -j | jq .config:

  1. $ nats str edit ORDERS --config orders.json

Publishing Into a Stream

Now let’s add in some messages to our Stream. You can use nats pub to add messages, pass the --wait flag to see the publish ack being returned.

You can publish without waiting for acknowledgement:

  1. $ nats pub ORDERS.scratch hello
  2. Published [sub1] : 'hello'

But if you want to be sure your messages got to JetStream and were persisted you can make a request:

  1. $ nats req ORDERS.scratch hello
  2. 13:45:03 Sending request on [ORDERS.scratch]
  3. 13:45:03 Received on [_INBOX.M8drJkd8O5otORAo0sMNkg.scHnSafY]: '+OK'

Keep checking the status of the Stream while doing this and you’ll see it’s stored messages increase.

  1. $ nats str info ORDERS
  2. Information for Stream ORDERS
  3. ...
  4. Statistics:
  5. Messages: 3
  6. Bytes: 147 B
  7. FirstSeq: 1
  8. LastSeq: 3
  9. Active Consumers: 0

After putting some throw away data into the Stream, we can purge all the data out - while keeping the Stream active:

Deleting All Data

To delete all data in a stream use purge:

  1. $ nats str purge ORDERS -f
  2. ...
  3. Statistics:
  4. Messages: 0
  5. Bytes: 0 B
  6. FirstSeq: 1,000,001
  7. LastSeq: 1,000,000
  8. Active Consumers: 0

Deleting A Message

A single message can be securely removed from the stream:

  1. $ nats str rmm ORDERS 1 -f

Deleting Sets

Finally for demonstration purposes, you can also delete the whole Stream and recreate it so then we’re ready for creating the Consumers:

  1. $ nats str rm ORDERS -f
  2. $ nats str add ORDERS --subjects "ORDERS.*" --ack --max-msgs=-1 --max-bytes=-1 --max-age=1y --storage file --retention limits --max-msg-size=-1