Skip to main content

Streams

A JetStream stream stores what is published to its subjects under a sequence number, acknowledges each publish, and is read by consumers at their own positions.

A publish stored in stream ORDERS under sequence 6 and acknowledged, read by a durable consumer of two members and by an ephemeral consumerStream ORDERSDurable consumer billingPublisherorders.created,Nats-Msg-IdPubAckstream ORDERS,sequence 6, not a duplicate123456A Nats-Msg-Id seen in the last two minutesis acknowledged again and stored oncereplica 1replica 2Ephemeralconsumerremoved wheninactiveEach consumer keeps its own position in the stream;the members of a durable share one
The diagram as text
  1. Stream ORDERS: 1; 2; 3; 4; 5; 6; A Nats-Msg-Id seen in the last two minutes is acknowledged again and stored once
  2. Durable consumer billing: replica 1; replica 2
  3. Publisher orders.created, Nats-Msg-Id → Stream ORDERS: publish
  4. 6 → PubAck stream ORDERS, sequence 6, not a duplicate: PubAck
  5. Stream ORDERS → Durable consumer billing: reads
  6. Stream ORDERS → Ephemeral consumer removed when inactive: reads
  7. Each consumer keeps its own position in the stream; the members of a durable share one

A stream captures the messages published to the subjects it lists, here orders.*. It numbers them in order and keeps them until its retention policy removes them: by limits of count, size and age, while some consumer still has interest, or until a consumer of a work queue acknowledges them.

The publisher gets an answer for every message: the stream's name, the sequence and whether the message was a duplicate. A publish no stream captures gets an error instead of the silence of Core NATS.

The headers of a message state what the server checks before storing it. Nats-Msg-Id names the message, and a repeat inside the stream's duplicate window (two minutes by default) is acknowledged as a duplicate and stored once. Nats-Expected-Stream, Nats-Expected-Last-Sequence, Nats-Expected-Last-Subject-Sequence and Nats-Expected-Last-Msg-Id make the server refuse the publish when the stream is not in the state the publisher expects.

ruststream-nats creates no stream. A stream is created and configured with the nats CLI or the JetStream API, and a subscription finds it by name when the service starts.

The model is the NATS server's own (streams, headers, deduplication). How a consumer reads a stream is on Consumers.