Skip to content

NATS JetStream — persistent streams and consumer types in practice

Enabling JetStream on a NATS server, creating a stream, designing pull vs push consumers, and the retention, replica, and storage choices an MSP makes in production.

JetStream is the persistent, at-least-once layer baked into the NATS binary. Turn it on, define a stream that captures a set of subjects, and consumers can replay history, scale horizontally with work-sharing, and survive broker restarts. This article is the JetStream design we run for clients who picked NATS for its simplicity but need delivery guarantees beyond fire-and-forget — what a stream actually stores, the difference between pull and push consumers, and the retention/replica decisions you make at design time, not after the first outage.

How to verify

Check JetStream is compiled in (it is, since NATS 2.2) and whether it is enabled on the running server:

nats-server --version
curl -s http://127.0.0.1:8222/jsz | head -c 400
nats account info --server nats://app:PASS@127.0.0.1:4222

If the /jsz endpoint returns JetStream not enabled and nats account info shows JetStream: Not enabled for account, the server config needs the JetStream block before any of the rest applies.

What’s happening

A JetStream stream is a server-side log that captures every message published to one or more subjects. Storage is either file-based (durable, recommended) or memory-based (faster, gone on restart). A stream has a retention policy — limits (drop oldest when full), interest (drop when no consumer needs them), or workqueue (drop after acknowledged consumption) — plus quotas on bytes, messages, and age.

A consumer is a stateful cursor across the stream. Two interaction models:

  • Pull consumer — the application calls Fetch(N) to request a batch. The server tracks ack state per delivered message; redeliveries are configurable. This is the default for horizontal scaling: 10 worker processes share the same durable pull consumer and split the work without coordination.
  • Push consumer — the server delivers messages to a subject the application subscribes to. Lower latency, less control over flow. Useful for low-volume topic listeners.

Replication is per-stream: replicas: 1 for non-critical, replicas: 3 for at-least-once with one-node tolerance. JetStream uses Raft inside; a quorum of replicas must acknowledge a publish before it is considered persisted. This is the same model as RabbitMQ quorum queues, just inside NATS.

The mental model: a stream is the log, a consumer is the cursor, replicas are the durability dial. Get those three right and everything else is tuning.

The procedure

  1. Enable JetStream in the server config. Edit /etc/nats/nats-server.conf and add a jetstream block:

    jetstream {
      store_dir: "/var/lib/nats/jetstream"
      max_memory_store: 2GB
      max_file_store: 50GB
    }

    The store_dir must be owned by the nats user; the disk should be a dedicated volume, not the root partition. max_file_store caps total JetStream disk for this server — a safety net against a runaway stream.

  2. Restart and verify:

    sudo systemctl restart nats
    curl -s http://127.0.0.1:8222/jsz | head -c 400
    # should now show "config" with limits and "memory":0 "storage":0
  3. Create a stream. The nats CLI is the operator tool:

    nats --server nats://app:PASS@127.0.0.1:4222 stream add ORDERS \
      --subjects "orders.>" \
      --storage file \
      --replicas 1 \
      --retention limits \
      --max-age 7d \
      --max-bytes 10GB \
      --max-msgs -1 \
      --max-msg-size 1MB \
      --discard old \
      --dupe-window 2m

    --dupe-window is the window in which Nats-Msg-Id headers are deduplicated — set it to the longest plausible client retry window plus a margin.

  4. Add a pull consumer for the worker pool:

    nats consumer add ORDERS process-orders \
      --pull \
      --ack explicit \
      --max-deliver 5 \
      --wait 30s \
      --filter "orders.created.>" \
      --replay instant

    --max-deliver 5 is the redelivery cap; after 5 failed attempts the message goes to the configured DLQ (see dead-letter queues). --ack explicit requires the worker to call Ack(); without it, the message is redelivered after --wait.

  5. Test publish and consume:

    # publish with a message ID for dedup
    nats --server nats://app:PASS@127.0.0.1:4222 pub orders.created.eu \
      --reply '' --header "Nats-Msg-Id:order-12345" "payload here"
    
    # worker pulls one
    nats consumer next ORDERS process-orders --ack

    The second command prints the payload and acks; subsequent calls block waiting for the next message. Run two consumer next --ack loops in parallel and watch them split the work.

  6. Wire monitoring. The /jsz endpoint returns per-stream and per-consumer stats; the NATS Prometheus exporter scrapes it and exposes jetstream_stream_messages, jetstream_stream_bytes, jetstream_consumer_num_pending, and jetstream_consumer_num_redelivered. Alert on:

    • jetstream_consumer_num_pending growing without bound (consumers falling behind)
    • jetstream_consumer_num_redelivered non-zero for sustained periods (a poison message or a sick worker)
    • jetstream_stream_bytes approaching max-bytes (about to hit retention)
  7. For 3-replica streams, scale to a cluster. A single-node JetStream stream is a single point of failure. The cluster install is its own piece of work — three NATS servers joined by routes, each with JetStream enabled, then create the stream with --replicas 3. The cluster uses Raft across the three nodes; a publish is acked once a quorum (2 of 3) has persisted it.

Operational notes

  • File storage on JetStream uses block files in store_dir; back them up like any other state — restic or borg of /var/lib/nats/jetstream while the server is running is generally safe, but for point-in-time consistency, snapshot the underlying filesystem.
  • A stream’s --max-bytes and --max-msgs are checked at publish time; hitting either triggers --discard old (default) or rejects the publish (--discard new). For audit-style streams where you must not lose data, use --discard new and alert before you get close.
  • Consumer state is per-consumer per-stream; deleting a consumer loses its cursor position. To “rewind” a consumer to a specific sequence, delete and recreate with --deliver set to the sequence number.
  • The Nats-Msg-Id header for dedup is per-stream, not per-subject; reusing the same ID across two streams creates no conflict, but reusing within a stream within the --dupe-window drops the second message silently.
  • Cluster JetStream needs all three nodes’ clocks synced via chrony or NTP; Raft elections fail in confusing ways when clocks drift more than a few seconds.

Stack Harbor stands up JetStream-backed NATS for clients building event-driven services with delivery guarantees — streams sized to the actual traffic, replicas chosen for the loss tolerance the business actually has, consumer concurrency tuned to the worker fleet, and DLQ wiring that catches poison messages before they jam the pipeline. This is the work covered by our Managed Operations practice.