Queue Groups
Outcome: you can run two copies of a worker so that each message is handled by exactly one of them, and you have a twenty-message receipt proving nothing was lost or doubled.
The problem with willing helpers
Chapter 4's subscribers each get their own copy of every message. That is right for noticeboards and wrong for work: if "process this order" is received by two copies of the order program, the order is processed twice.
The fix is one word. Subscribers that join the same
queue group stop being audiences and become a team:
for each message, the server picks one member and
delivers only to that member. The CLI joins with
--queue:
nats sub jobs --queue workers
Start that twice, in two windows, and the two processes are one logical worker with two pairs of hands.
The demonstration, including the lopsided one
The lab published six jobs to two queue members, with an ordinary subscriber watching for contrast (transcript t05). The results:
- the ordinary subscriber received 6 of 6 — as chapter 4 promises, subscribing does not consume;
- the queue members received 0 and 6.
Six is a small sample, and the pick is random per message, so a shutout is possible — about one time in thirty-two, and the lab hit it first try. The transcript keeps that run rather than quietly re-rolling, because the lesson matters: queue delivery is fair over time, not per handful. The rerun with twenty jobs came out 9 and 11 — twenty messages, twenty deliveries, each job in exactly one window, none in both, none missing. That — total preserved, uniqueness preserved — is the guarantee. The split being even is not.
What queue groups are, and are not
- A group is named by its members, not created by an
administrator. The first
--queue workerssubscription brings the group into being; the last member leaving dissolves it. Two groups on the same subject each get their own one copy — groups multiply audiences, members share. - There is no memory. A job published while no member is connected is gone, exactly as in chapter 4. Queue groups share live work; they do not store it. If the work must wait for a worker to return, that is JetStream's work-queue pattern (next chapter), where the stream holds the message until a consumer acknowledges it.
- There is no retry. If the chosen member crashes mid-job, core NATS does not hand the job to the other member — it never knew the job mattered. Acknowledged, redelivered work is likewise a JetStream idea. Knowing which of your problems need which half of NATS is most of designing with it.
- Request/reply fits naturally. A question published to a queue group's subject is seen by one member, whose answer is the one the asker gets — chapter 6's fleet of answerers.
A naming habit
Name the group for the job, not the machine (workers,
transcribers), and keep the subject for the work
(jobs). Then adding a third pair of hands is starting
the same command again — no config, no announcement,
no restart of the others.
Check: run two members, publish twenty numbered messages, and add the two received counts. Nineteen or twenty-one means something else is on the subject; twenty between them, each number once, is the queue group doing its whole job.