Skip to content

NATS for Mere Mortals

Programs that run in separate processes — or on separate machines — spend a surprising amount of effort just talking to each other. The familiar ways are all compromises: a shared database one program polls, a web request that needs the other side awake right now, a file dropped in a folder and hoped over. NATS is the small, fast messaging system built for exactly this gap. Programs connect to one server, publish short messages under names like shop.order.1, and any program that said it cares about that name receives a copy — without the sender knowing, or needing to know, who is listening.

This guide explains how that works, then does it for real. With nothing but the server and its command-line tool you will publish and subscribe, ask questions and get answers (request/reply), share work between a team of helpers (queue groups), and then meet JetStream, the part of NATS that remembers: messages stored on disk, read back in order, acknowledged one by one, redelivered when a reader forgets to acknowledge — and still there after the server restarts. A key-value store and a small file store built on the same machinery get their turn too, as do the locks on the door: tokens, named users, and permissions that fence a user to one corner of the subject space.

The lab behind this guide ran the real thing: nats-server 2.15.2 and the nats command-line tool 0.5.1, official release binaries, on one small Linux machine, everything on loopback. Against them it published and received, split twenty jobs between two workers with none lost and none doubled, watched an unacknowledged message come back for a second try, restarted the server and found every stored message intact, and was refused at the door with a wrong token — fourteen receipts in all, kept as transcripts in nats-lab/.

What the lab could not do, it did not fake. One machine means no clusters, no leaf nodes, and no second server to fail over to; those are taught from the documentation and labelled as such wherever they appear. criteria.md keeps the full ledger of what was run and what was only read.

One property deserves saying before any procedure: core NATS, the part you meet first, does not keep messages. If nobody is listening when a message is published, it is gone — chapter 4 demonstrates the loss rather than warning about it in the abstract. That is a design choice, not a flaw: it is what makes the core so light, and JetStream exists for the times you need remembering instead.

What this guide covers

  • What NATS is — the mental model: one server, named subjects, messages delivered to whoever asked, and the two halves — the forgetful core and JetStream the rememberer
  • The moving parts — the server, the clients, subjects and connections, and which part holds what
  • Installing NATS — the two official binaries, and the version checks that prove they run
  • Your first messages — publish and subscribe, and the early message nobody heard, shown rather than described
  • Subjects and wildcards — naming with dots, and the two wildcards that let one subscription cover a whole tree of names
  • Request and reply — asking a question on a subject and getting one answer back, plus what happens when nobody answers
  • Queue groups — several workers, one name: each message goes to exactly one of them, and twenty test jobs prove none is lost or doubled
  • JetStream — streams and consumers — messages kept on disk, a consumer that reads in order, acknowledgements, and the redelivery of the message nobody acknowledged
  • The key-value and object stores — JetStream wearing two familiar faces: keys with revisions, and whole files stored in chunks and proven identical on the way back out
  • Running and securing the server — config files, the monitoring pages, a token on the door, named users, and permissions that fence a user to shop.> and nowhere else
  • Recipes — a noticeboard, a work queue with a memory, a question-answering service, and a settings store, all assembled from parts already proven
  • The neighbours — RabbitMQ the post office, MQTT for small devices, Redis doing double duty, and Kafka the archivist: what each does that NATS doesn't, and vice versa
  • Limits — the forgetful core, the 1 MB default message ceiling, one server's worth of JetStream, and the rest of the honest edges
  • Troubleshooting — no servers available, the authorization violation, the reply command that answered empty, and the interactive prompt that stops a script dead
  • Glossary — the terms, collected in one place

What it doesn't cover

Writing NATS clients in a programming language — the command-line tool exercises every idea here, and each language's client library deserves its own treatment once the ideas are in place. Clustering, gateways and leaf nodes — the ways several servers become one system — are described honestly from the documentation in chapters 2 and 13, but the lab had one machine and does not pretend otherwise. The operator/account security machinery used by large hosted NATS services is likewise pointed at, not taught; chapter 10 covers the config-file authentication a self-hosted server actually needs.

How to use this guide

Read chapter 1 for the model and chapter 2 before installing anything — knowing which part holds what turns later confusion into a named problem. Chapters 3 to 7 use only the forgetful core and fit in one sitting; chapters 8 and 9 add JetStream and are where the guide slows down. The lab's transcripts live in nats-lab/transcripts/; its stream, consumer and server configuration files live alongside them in nats-lab/, ready to reuse.


Part of the series

This guide is one of the For Mere Mortals guides: plain-language guides to the tools programmers and makers use.

Related guides:

License

The writing in this guide is licensed under CC BY 4.0. Code examples and lab scripts are licensed under the MIT License.