Skip to content

Local

Setup .env

This example uses direnv to load local .env files as shell variables.

 cp .env.template .env
❯ direnv allow
direnv: export +NATS_CREDS_PATH +NATS_STREAM_NAME +NATS_URL

Initialize operator, account, and users with nsc

Initialize a local nsc config, including an operator, account, and users.

 make init-nsc
❯ tree -L2 .nsc 
.nsc
├── creds
│   └── local
├── keys
│   ├── A
│   ├── O
│   └── U
├── local   ├── accounts
│   └── local.jwt
├── nsc.conf
└── nsc.json

Start local NATS server

Start a local NATS server using the nsc configuration (./.nsc/nsc.conf).

[!NOTE] NKey values will be different in your output.

 make local
nats-server -c nats.conf
[18828] 2025/08/14 10:53:52.434907 [INF] Starting nats-server
[18828] 2025/08/14 10:53:52.434991 [INF]   Version:  2.11.7
[18828] 2025/08/14 10:53:52.434993 [INF]   Git:      [df44964]
[18828] 2025/08/14 10:53:52.435006 [INF]   Cluster:  leaf
[18828] 2025/08/14 10:53:52.435007 [INF]   Name:     leaf
[18828] 2025/08/14 10:53:52.435008 [INF]   Node:     ZLMV550B
[18828] 2025/08/14 10:53:52.435009 [INF]   ID:       NDCVTRDUCIKXHIDXV5E2B2BTSZ5L7WOZFKSH35UMX5DSUF57RNUPYHDL
[18828] 2025/08/14 10:53:52.435026 [INF] Trusted Operators
[18828] 2025/08/14 10:53:52.435028 [INF]   System  : ""
[18828] 2025/08/14 10:53:52.435029 [INF]   Operator: "memory"
[18828] 2025/08/14 10:53:52.435031 [INF]   Issued  : 2025-08-14 10:51:51 -0600 MDT
[18828] 2025/08/14 10:53:52.435038 [INF]   Expires : Never
[18828] 2025/08/14 10:53:52.436161 [INF] Starting JetStream
[18828] 2025/08/14 10:53:52.436167 [WRN] Temporary storage directory used, data could be lost on system reboot
[18828] 2025/08/14 10:53:52.436715 [INF]     _ ___ _____ ___ _____ ___ ___   _   __  __
[18828] 2025/08/14 10:53:52.436717 [INF]  _ | | __|_   _/ __|_   _| _ \ __| /_\ |  \/  |
[18828] 2025/08/14 10:53:52.436718 [INF] | || | _|  | | \__ \ | | |   / _| / _ \| |\/| |
[18828] 2025/08/14 10:53:52.436719 [INF]  \__/|___| |_| |___/ |_| |_|_\___/_/ \_\_|  |_|
[18828] 2025/08/14 10:53:52.436720 [INF]
[18828] 2025/08/14 10:53:52.436721 [INF]          https://docs.nats.io/jetstream
[18828] 2025/08/14 10:53:52.436722 [INF]
[18828] 2025/08/14 10:53:52.436723 [INF] ---------------- JETSTREAM ----------------
[18828] 2025/08/14 10:53:52.436758 [INF]   Max Memory:      1.00 GB
[18828] 2025/08/14 10:53:52.436760 [INF]   Max Storage:     1.00 GB
[18828] 2025/08/14 10:53:52.436761 [INF]   Store Directory: "/var/folders/q3/sxmzn9xs6r90xcfzr3g5ch2m0000gn/T/jetstream"
[18828] 2025/08/14 10:53:52.436762 [INF]   API Level:       1
[18828] 2025/08/14 10:53:52.436764 [INF] -------------------------------------------
[18828] 2025/08/14 10:53:52.437383 [INF] Listening for client connections on 0.0.0.0:4222
[18828] 2025/08/14 10:53:52.437509 [INF] Server is ready

Create local stream and consumers using Terraform.

Provision a single stream and two consumers. These will be used by the Go micro services. The stream uses an "interest" retention policy, acting as a durable queue.

[!IMPORTANT]
You cannot source/mirror from streamsw the interest or work queue retention policies.

 cd ./tf/local
❯ terraform apply -auto-approve  # or `terraform plan -out plan.out` and `terraform apply plan.out`
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # jetstream_consumer.ORDERS will be created
  + resource "jetstream_consumer" "ORDERS" {
      + ...
    }

  # jetstream_consumer.SHIPMENTS will be created
  + resource "jetstream_consumer" "SHIPMENTS" {
      + ...
    }

  # jetstream_stream.QUEUE will be created
  + resource "jetstream_stream" "QUEUE" {
      + ...
    }

Plan: 3 to add, 0 to change, 0 to destroy.
jetstream_stream.QUEUE: Creating...
jetstream_stream.QUEUE: Creation complete after 1s [id=JETSTREAM_STREAM_QUEUE]
jetstream_consumer.SHIPMENTS: Creating...
jetstream_consumer.ORDERS: Creating...
jetstream_consumer.ORDERS: Creation complete after 0s [id=JETSTREAM_STREAM_QUEUE_CONSUMER_ORDERS]
jetstream_consumer.SHIPMENTS: Creation complete after 0s [id=JETSTREAM_STREAM_QUEUE_CONSUMER_SHIPMENTS]

Apply complete! Resources: 3 added, 0 changed, 0 destroyed.

View the created stream and consumers.

 nats --creds ../../.nsc/creds/memory/A/admin.creds stream report
Obtaining Stream stats

╭─────────────────────────────────────────────────────────────────────────────────────────╮
│                                      Stream Report                                      │
├────────┬─────────┬───────────┬───────────┬──────────┬───────┬──────┬─────────┬──────────┤
│ Stream  Storage  Placement  Consumers  Messages  Bytes  Lost  Deleted  Replicas │
├────────┼─────────┼───────────┼───────────┼──────────┼───────┼──────┼─────────┼──────────┤
│ QUEUE   File                2          0         0 B    0     0                 │
╰────────┴─────────┴───────────┴───────────┴──────────┴───────┴──────┴─────────┴──────────╯

❯ nats --creds ../../.nsc/creds/memory/A/admin.creds consumer report QUEUE
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│                                Consumer report for QUEUE with 2 consumers                                │
├───────────┬──────┬────────────┬──────────┬─────────────┬─────────────┬─────────────┬───────────┬─────────┤
│ Consumer   Mode  Ack Policy  Ack Wait  Ack Pending  Redelivered  Unprocessed  Ack Floor  Cluster │
├───────────┼──────┼────────────┼──────────┼─────────────┼─────────────┼─────────────┼───────────┼─────────┤
│ ORDERS     Pull  Explicit    30.00s    0            0            0            0                  │
│ SHIPMENTS  Pull  Explicit    30.00s    0            0            0            0                  │
╰───────────┴──────┴────────────┴──────────┴─────────────┴─────────────┴─────────────┴───────────┴─────────╯

Start publisher

Start the publisher Go micro service, which publishes new orders to the stream.

 cd demo
❯ make publisher
Publishing on nats://localhost:4222
Publishing order 1

In another tab, verify the stream has messages and the ORDERS consumer has unprocessed messages.

# from the root of the repo nats --creds .nsc/creds/memory/A/admin.creds stream report
Obtaining Stream stats

╭─────────────────────────────────────────────────────────────────────────────────────────╮
│                                      Stream Report                                      │
├────────┬─────────┬───────────┬───────────┬──────────┬───────┬──────┬─────────┬──────────┤
│ Stream  Storage  Placement  Consumers  Messages  Bytes  Lost  Deleted  Replicas │
├────────┼─────────┼───────────┼───────────┼──────────┼───────┼──────┼─────────┼──────────┤
│ QUEUE   File                2          3         132 B  0     0                 │
╰────────┴─────────┴───────────┴───────────┴──────────┴───────┴──────┴─────────┴──────────╯

❯ nats --creds .nsc/creds/memory/A/admin.creds consumer report QUEUE
╭────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│                               Consumer report for QUEUE with 2 consumers                               │
├───────────┬──────┬────────────┬──────────┬─────────────┬─────────────┬─────────────┬───────────┬───────┤
│ Consumer   Mode  Ack Policy  Ack Wait  Ack Pending  Redelivered  Unprocessed  Ack Floor  Clust │
├───────────┼──────┼────────────┼──────────┼─────────────┼─────────────┼─────────────┼───────────┼───────┤
│ ORDERS     Pull  Explicit    30.00s    0            0            3 / 100%     0                │
│ SHIPMENTS  Pull  Explicit    30.00s    0            0            0            0                │
╰───────────┴──────┴────────────┴──────────┴─────────────┴─────────────┴─────────────┴───────────┴───────╯

Start services

Start the orders and shipments services, which will consume messages from their respective consumers.

The orders service will trigger shipments when it finished processing the order.

 cd demo
❯ make orders
Consuming orders on nats://localhost:4222
Received order: 1
Received order: 2

# in another tab cd demo
❯ make shipments
Consuming shipments on nats://localhost:4222
Received shipment: 1
Received shipment: 2

Confirm that messages in the consumers are being processed.

 nats --creds .nsc/creds/memory/A/admin.creds consumer report QUEUE
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│                                Consumer report for QUEUE with 2 consumers                                │
├───────────┬──────┬────────────┬──────────┬─────────────┬─────────────┬─────────────┬───────────┬─────────┤
│ Consumer   Mode  Ack Policy  Ack Wait  Ack Pending  Redelivered  Unprocessed  Ack Floor  Cluster │
├───────────┼──────┼────────────┼──────────┼─────────────┼─────────────┼─────────────┼───────────┼─────────┤
│ ORDERS     Pull  Explicit    30.00s    95           0            26 / 18%     35                 │
│ SHIPMENTS  Pull  Explicit    30.00s    10           0            12 / 8%      147                │
╰───────────┴──────┴────────────┴──────────┴─────────────┴─────────────┴─────────────┴───────────┴─────────╯