JetStream — Streams and Consumers
Outcome: you have a stream on disk that catches messages as they pass, a consumer that reads them in order and acknowledges each — and you have watched the one unacknowledged message come back for a second try, which is the whole point of the machinery.
This chapter needs the server from chapter 3 started
with -js and a store directory. Everything here was
run for real; transcripts t06 and t07 are the receipts.
A stream is a net, declared once
A stream says: keep a copy of everything published
on these subjects, under these rules. The lab declared
its net over the shop's subjects with a small JSON file
(nats-lab/shop-stream.json) — a config file rather
than a screen of flags, because the CLI's interactive
prompts stop dead without a terminal (transcript t06's
first, failed attempt; chapter 14 has the symptom):
{
"name": "SHOP",
"description": "Orders and refunds for the lab shop",
"subjects": ["shop.>"],
"retention": "limits",
"discard": "old",
"max_age": 86400000000000,
"storage": "file",
"num_replicas": 1
}
nats stream add SHOP --config shop-stream.json
The rules in plain words: keep messages for subjects
under shop. (subjects); keep them on disk
(storage: file — the alternative, memory, is faster
and forgets at shutdown); when limits are reached,
discard the oldest; nothing here may stay longer
than a day (max_age, in nanoseconds — JetStream's
native unit, and the reason config files beat
mental arithmetic). Retention: limits means "keep
to the limits"; chapter 13 names the other two policies
(interest and work-queue) and what they change.
Publishing does not change at all:
nats pub shop.order.1 "order 2001: two notebooks"
nats pub shop.order.2 "order 2002: one pencil"
nats pub shop.refund.1 "refund for order 1999"
nats stream info SHOP now reports Messages: 3,
195 bytes, sequences 1 to 3. Note the three messages the
lab published to shop.* before the stream existed
(transcript t06, first half): they are not in the count.
A stream catches what passes after it is declared —
JetStream remembers the future, not the past.
A consumer is a reading position that remembers
Reading is done by a consumer: a server-side
bookmark with rules. The lab's (nats-lab/shop-consumer.json)
is a pull consumer — the reader asks for the next
messages when it is ready, rather than having them
pushed at it — with explicit acknowledgement: every
message must be individually confirmed.
nats consumer add SHOP reader --config shop-consumer.json
nats consumer next SHOP reader --count 2
[00:57:14] subj: shop.order.1 / tries: 1 / cons seq: 1 / str seq: 1 / pending: 2
order 2001: two notebooks
Acknowledged message
[00:57:14] subj: shop.order.2 / tries: 1 / cons seq: 2 / str seq: 2 / pending: 1
order 2002: one pencil
Acknowledged message
Read the header line once and it stays readable: the
subject, how many times this message has been offered
(tries), its position in the consumer's count and
the stream's count, and how many remain. consumer next
acknowledges each message it shows you — polite, and
fine for messages that cannot fail to be handled.
The acknowledgement is the contract
The interesting case is the opposite. The lab fetched
the third message with --no-ack — the reader takes the
message and does not confirm it:
nats consumer next SHOP reader --count 1 --no-ack
# [00:57:14] subj: shop.refund.1 / tries: 1 ... refund for order 1999
Then it waited out the consumer's ack wait (10 seconds in the lab's config) and fetched again:
[00:57:26] subj: shop.refund.1 / tries: 2 / cons seq: 4 / str seq: 3 / pending: 0
refund for order 1999
Acknowledged message
Same message, tries: 2. No error, no drama: the server noticed the missing acknowledgement, concluded the work might not be done, and offered the message again. The consumer's max deliver (3 in this config) caps the offers; after that the message stops being redelivered and the consumer reports it as terminated — the design question of what your system does then is yours, and chapter 13 says so plainly.
This trio — stored message, explicit acknowledgement, timed redelivery — is what turns "messages" into reliable work. A crash after receiving is no longer data loss; it is a delayed retry. Note what it costs: somebody must decide ack waits and delivery caps, and a message handled-but-not-acknowledged will be handled twice, so handlers should tolerate a repeat.
Push consumers, briefly
The lab ran a pull consumer because asking-when-ready suits batch work and this guide's terminal rhythm. A push consumer delivers to a subject as messages arrive, for readers that want a live feed with a memory behind it. The acknowledgement contract is the same; only the direction of asking differs. Documented here rather than run — the pull transcripts demonstrate the contract itself, which is the part that matters.
Check: nats stream info SHOP shows your messages;
fetch one without acknowledging, wait past your ack
wait, and fetch again — the same body returns with
tries: 2.