Recipes
Outcome: four small systems, each assembled only from parts earlier chapters proved, each with the check that shows it working.
Every recipe uses the guide's running example subjects and the lab's server address; substitute your own.
1. The noticeboard — announce to whoever is awake
For: status, readings, "this just happened" — traffic whose value is in the moment.
Any number of announcers publish; any number of watchers subscribe, each receiving every announcement:
# watcher (start as many as you like)
nats sub 'announce.>'
# announcer
nats pub announce.kitchen "the kitchen sensor is online"
nats pub announce.shop "the shop opens at nine"
Nothing is stored; a watcher that starts late has missed the past and owns the future. That is the correct shape for readings — the next one is already coming. Check: start a second watcher mid-stream; both windows print the next announcement. (Chapter 4, t02.)
2. The shared work queue — many hands, no doubles
For: jobs that any of several identical workers may do, where doing one twice is a bug.
# on each worker machine (same command, every copy)
nats sub jobs --queue workers
# the dispatcher
for i in $(seq 1 20); do nats pub jobs "task $i"; done
Count the two windows' receipts: they add to twenty, each task in exactly one. Accept the split being uneven — chapter 7's was 9/11. If a job must survive all the workers being down, this recipe is the wrong one; the next is its durable sibling. Check: the counts sum to the number published. (Chapter 7, t05.)
3. The remembered work queue — JetStream on the job
For: orders and tasks that must not be lost to a restart, a crash, or a quiet afternoon.
Declare the net once (chapter 8's files), then publish
exactly as before — the stream catches shop.> because
it was told to:
nats stream add SHOP --config shop-stream.json
nats pub shop.order.1 "order 2001: two notebooks"
Workers pull and acknowledge:
nats consumer next SHOP reader --count 1
A worker that takes a message and dies before
acknowledging has not lost the order: after the ack
wait, the message is offered again (chapter 8 watched it
happen, tries: 2). The recipe's whole discipline is in
one rule: acknowledge when the work is done, not when
it is received. Check: restart the server, then
nats stream info SHOP — the count is unchanged.
(Chapter 8, t06–t07; restart, t09.)
4. The question desk — one answer, on demand
For: lookups and little services — "what do you know about X?" — without running a web server.
One answerer, computing its reply:
nats reply help.echo --command "echo hello-from-command"
Any number of askers:
nats request help.echo "your question here"
Scale it by running the answerer on several machines
behind a queue group on the same subject (chapter 6
explains why the asker still gets exactly one answer).
Mind the chapter 6 trap: keep % out of the command.
Check: stop the answerer and ask again — the answer is
No responders are available, immediately. (Chapter 6,
t04.)
5. The settings shelf — values everyone can read
For: the handful of settings several programs share, with a history of what they used to be.
nats kv add SETTINGS
nats kv put SETTINGS shop.currency "USD"
nats kv get SETTINGS shop.currency
Programs read at start-up; a kv watch (documented in
chapter 9) tells them when a value changes. The revision
number is your undo button and your audit trail in one.
Check: change a value, restart the server, and
kv history still shows the trail. (Chapter 9, t08.)
Check for the chapter: each recipe names its transcript. If one misbehaves in your hands, the transcript is the known-good run to compare against, line by line.