Skip to content

The Moving Parts

Outcome: you can name every part of a NATS setup — the server, the clients, the connection, the subject, and JetStream's three stored things — and say which part is responsible when something surprises you.

The server: nats-server

One program, one job: hold connections, match subjects, move bytes. A single server with no configuration at all is a complete, working NATS — most of this guide's lab ran on exactly that, started with a port number and a name. The server is a single static binary with no database and no runtime to install beside it; chapter 3 shows it is about 19 MB.

A server can be given a config file (chapter 10) naming its ports, its JetStream store, and who may connect. Servers can also be joined into clusters, where they forward messages to each other and JetStream keeps copies on several machines; and leaf nodes extend one system into another network, such as a shop floor or a remote site. The lab had one machine, so clusters and leaf nodes are described here from the documentation and not run — everything else in this chapter was exercised for real.

The clients and the connection

Any program that speaks the NATS protocol is a client. Clients exist for Go, Python, JavaScript, Rust, Java, and dozens more; this guide uses the official command-line client, nats, which is a client like any other — when it subscribes, the server cannot tell it apart from a production program.

A client holds one open connection to a server and multiplexes everything over it: subscriptions, publishes, replies. Connections are cheap and long-lived. The monitoring pages in chapter 10 list who is connected right now; the lab's transcript t12 catches the CLI itself in that list, name and all.

Subjects: the only addressing there is

There are no queues to create, no topics to provision, no address book. A subject is just a string of words joined by dots — shop.order.1, sensors.kitchen.temp — chosen by the publisher at the moment of publishing. Two wildcards let subscribers cover whole trees:

  • * stands for exactly one word: shop.* hears shop.order but not shop.order.1.
  • > stands for one or more words, and must come last: shop.> hears everything under shop..

Chapter 5 demonstrates both against the same three messages, so the difference is observed, not asserted. Subjects are the whole design surface of core NATS, which is why one chapter of this guide is spent on nothing else.

Messages

A message is a subject, an optional set of headers, and a body of bytes — any bytes; NATS does not inspect them. The default size ceiling is 1 MB per message (the server's startup banner and its /varz page both state it; the lab read it off in transcript t01). That ceiling is a hint about intent: NATS is for many small messages, not for hauling files — though the object store in chapter 9 shows the sanctioned way files get through anyway, in chunks.

JetStream's three stored things

When JetStream is switched on (one flag, chapter 3), the server gains a store directory and three concepts:

  • A stream is a named, configured net over a set of subjects. It decides how long messages are kept, how many, and what happens when the limit is reached. The messages themselves sit in files under the store directory — transcript t08 lists them.
  • A consumer is a reading position on a stream, kept by the server: which message comes next, which are waiting for acknowledgement, how many times each has been offered. Consumers survive both client and server restarts, which is the point.
  • The key-value and object stores are streams with a fixed, convenient shape, managed through their own commands — a stream underneath, as transcript t08's file listing shows (KV_SETTINGS sits beside SHOP in the same store).

Who holds what — the table to reason with

Question Answer
Who decides a message's subject? The publisher, at publish time
Who keeps core messages? Nobody — delivery is live or not at all
Who keeps JetStream messages? The server, in the stream's files
Who remembers what a reader has read? The consumer, on the server
Who decides who may connect? The server's config file (chapter 10)
Who enforces a user's subject fence? The server, on every publish and subscribe

When something surprises you later — a message missing, a stranger refused, a file reappearing after a restart — run down this table and the surprise usually names its own chapter.

Check: the server in the lab answered http://127.0.0.1:18085/healthz with {"status":"ok"} and its /varz page reported version 2.15.2, port 18084, JetStream enabled and a 1 MB maximum payload (t01). If you can say where each of those four facts lives — the config, the server, the store — the moving parts are in place.