Log inSign up

Deploy NATS messaging with JetStream persistence

Download the nats-server release binary to /usr/local/bin, write /etc/nats/nats-server.conf with a client port 4222, an account holding hashed passwords and a jetstream block, and run it under systemd as a dedicated user. Clients connect over TLS; cluster routes on 6222 are open only to the other nodes.

Topic: Queues and messaging · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13 · Updated 2026-12-03

What do you get from NATS?

NATS is a single Go binary that offers publish and subscribe, request and reply, and, with JetStream enabled, persistent streams and key-value buckets. Core NATS delivers to whoever is listening right now; JetStream stores messages on disk so consumers can replay them later or acknowledge them one by one.

The server is light: a test node runs comfortably in 256 MB, and memory needs grow with the JetStream data you choose to keep in RAM. Fast local NVMe helps file-based streams. Prepare a sudo user and a DNS name such as nats.example.net for the certificate (see DNS records).

How do you install the server and the CLI?

Fetch the Linux release archives from the project GitHub pages, check them and copy the binaries into place. Set the two variables to the newest version numbers shown on the releases pages of nats-io/nats-server and nats-io/natscli.

sudo apt update && sudo apt install -y curl unzip
NV=2.12.1
CV=0.3.0
curl -fLO "https://github.com/nats-io/nats-server/releases/download/v$NV/nats-server-v$NV-linux-amd64.tar.gz"
curl -fLO "https://github.com/nats-io/nats-server/releases/download/v$NV/SHA256SUMS"
grep "nats-server-v$NV-linux-amd64.tar.gz" SHA256SUMS | sha256sum -c -
tar -xzf nats-server-v$NV-linux-amd64.tar.gz
sudo install -m 755 nats-server-v$NV-linux-amd64/nats-server /usr/local/bin/
curl -fLO "https://github.com/nats-io/natscli/releases/download/v$CV/nats-$CV-linux-amd64.zip"
unzip -o nats-$CV-linux-amd64.zip
sudo install -m 755 nats-$CV-linux-amd64/nats /usr/local/bin/
nats-server --version
nats --version

If a checksum file name differs for the version you picked, open the release page and compare the published hash by hand. The Go guide is only needed if you want to build from source instead.

How do you write a configuration with accounts and JetStream?

Create a system user and data directory, generate bcrypt hashes for the passwords with nats server passwd and put everything in one file. Accounts isolate subjects from each other, the way vhosts do in other brokers.

sudo useradd --system --home /var/lib/nats --shell /usr/sbin/nologin nats
sudo install -d -o nats -g nats -m 750 /var/lib/nats /etc/nats
nats server passwd   # run twice: once for the app, once for the admin user

Paste the hashes into the config:

server_name: n1
listen: 127.0.0.1:4222
http: 127.0.0.1:8222

jetstream {
  store_dir: "/var/lib/nats"
  max_memory_store: 256MB
  max_file_store: 20GB
}

accounts {
  APP: {
    jetstream: enabled
    users: [
      { user: orders, password: "$2a$11$REPLACE-WITH-HASH-1" }
    ]
  }
  SYS: {
    users: [
      { user: admin, password: "$2a$11$REPLACE-WITH-HASH-2" }
    ]
  }
}
system_account: SYS

Save it as /etc/nats/nats-server.conf, set sudo chown nats:nats and mode 640 on the file, and check the syntax with nats-server -c /etc/nats/nats-server.conf -t.

How do you run NATS under systemd?

Write a small unit that starts the server as the nats user and reloads the config on a HUP signal.

sudo tee /etc/systemd/system/nats.service >/dev/null <<'EOF'
[Unit]
Description=NATS server
After=network-online.target
Wants=network-online.target

[Service]
User=nats
Group=nats
ExecStart=/usr/local/bin/nats-server -c /etc/nats/nats-server.conf
ExecReload=/bin/kill -s HUP $MAINPID
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now nats
curl -s http://127.0.0.1:8222/healthz
sudo ss -ltnp | grep nats-server

The health endpoint returns {"status":"ok"} once JetStream has opened its store.

How do you check publish, subscribe and a stream?

Save a CLI context so you do not repeat the credentials, then try core messaging and a JetStream stream.

nats context save local --server nats://127.0.0.1:4222 --user orders --password 'orders-secret' --select
nats sub 'orders.>' &
nats pub orders.created '{"id":"A17"}'
kill %1
nats stream add ORDERS --subjects 'orders.>' --storage file --replicas 1 --defaults
nats pub orders.created '{"id":"A18"}'
nats stream info ORDERS
nats account info

The stream info should report one stored message. nats account info confirms the account has JetStream limits assigned.

How do you open the client port with TLS?

Add a tls block, change listen to all addresses and open 4222 in the firewall. With the block present, the server refuses plain-text clients on that port.

sudo install -d -o nats -g nats -m 750 /etc/nats/tls
sudo cp /etc/letsencrypt/live/nats.example.net/{fullchain,privkey}.pem /etc/nats/tls/
sudo chown nats:nats /etc/nats/tls/*.pem
# in /etc/nats/nats-server.conf
listen: 0.0.0.0:4222
tls {
  cert_file: "/etc/nats/tls/fullchain.pem"
  key_file: "/etc/nats/tls/privkey.pem"
  timeout: 2
}
# then
sudo systemctl reload nats
sudo ufw allow 22/tcp
sudo ufw allow 4222/tcp
sudo ufw enable
nats --server tls://nats.example.net:4222 --user orders --password 'orders-secret' rtt

The Certbot guide covers issuing the certificate; add a deploy hook that copies the files and runs systemctl reload nats.

How do you cluster three NATS servers over public IPs?

Give each server a unique server_name, add a cluster block with route credentials and TLS, and list the other two nodes as routes. JetStream then replicates streams created with --replicas 3 across the cluster. Since the nodes reach each other over public addresses, open port 6222 only to the two peers.

cluster {
  name: east
  listen: 0.0.0.0:6222
  authorization { user: route, password: "Long-Route-Secret" }
  tls {
    cert_file: "/etc/nats/tls/fullchain.pem"
    key_file: "/etc/nats/tls/privkey.pem"
  }
  routes: [
    "nats-route://route:Long-Route-Secret@n2.example.net:6222"
    "nats-route://route:Long-Route-Secret@n3.example.net:6222"
  ]
}

# firewall on n1, repeat on each node for its peers
sudo ufw allow proto tcp from 198.51.100.25 to any port 6222
sudo ufw allow proto tcp from 198.51.100.26 to any port 6222

Use host names that match each node certificate so route TLS verifies. After restarting all three, nats server list run as the system account user shows every node, and nats server report jetstream shows the meta leader.

How do you upgrade and troubleshoot NATS?

Upgrades are a binary swap: install the new nats-server over the old one and restart nodes one at a time, waiting until nats server report jetstream lists the restarted node as current before moving on. Read the release notes for config keys that changed between minor versions.

For trouble, run sudo journalctl -u nats -e. An authorization violation means the user, password or account is wrong; insufficient resources when adding a stream means the stream needs more than the max_file_store or account limit allows. The monitoring port exposes /varz, /connz and /jsz as JSON, which Prometheus can scrape through the NATS exporter.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-1core NATS and small streams1 vCPU, 1 GB RAM, 25 GB NVMe$3.60/mo$43.20 for 12 months
A2-2JetStream cluster member2 vCPU, 2 GB RAM, 60 GB NVMe$10.80/mo$64.80 for 6 months

Questions about NATS server and JetStream

Do I need JetStream?

Only when messages must survive a moment with no subscribers, or when consumers need acknowledgements and replay. Plain request/reply and fan-out work without it.

Can NATS replace a Kafka or RabbitMQ setup?

For many event and work-queue cases, yes, through JetStream streams and consumers. Compare the client libraries you depend on before switching; see the Kafka and RabbitMQ guides.

Where are JetStream files stored?

Under store_dir, here /var/lib/nats/jetstream. Back it up together with the config.

Why does the server refuse plain connections after adding TLS?

A tls block on the client listener makes TLS mandatory. Use tls:// URLs in clients and the CLI.

Can I run a single node with replicas set to 3?

No. A stream needs as many JetStream servers as replicas, so keep --replicas 1 on a single node.