Skip to content

Request and Reply

Outcome: you can ask a question on a subject and get exactly one answer back — and you know what the asker sees in each of the three ways it can go wrong.

Asking instead of announcing

Publishing is an announcement; nobody owes you an answer. But plenty of exchanges are questions: what time is it, what is the price, is this order valid? NATS builds the asking pattern from the same two pieces. A request is a publish that carries a return address — a private, one-off subscription (an inbox) created for this single question. A responder publishes its answer to that inbox. The server does nothing special; the pattern is a convention the clients honour, which is why any client library can answer any other's questions.

The CLI plays both parts. A responder with a fixed answer:

nats reply help.static "static answer"

and the asking:

$ nats request help.static "question"
Sending request on "help.static"
Received with rtt 562.985µs
static answer

(transcript t04). The asker prints the round-trip time — half a millisecond on one machine — and the answer.

A responder that computes its answer

nats reply can run a command for each request and use its output as the answer:

nats reply help.echo --command "echo hello-from-command"
$ nats request help.echo "x"
hello-from-command

The command receives the request body on its input and its output becomes the reply body, which makes small shell-powered services a one-liner. One trap, found the hard way and kept in transcript t04: the command string is also the CLI's template surface, and a command containing % — such as a date format, date -u +%H:%M:%S — came back with an empty reply, twice. Keep reply commands free of % and {{ }} unless you mean the CLI's own subject templates; chapter 14 lists the symptom so you can recognise it.

The three ways it goes wrong

  1. Nobody is listening. The request above to a subject with no responder answered itself at once:

$ nats request nosuchsubject "" --timeout 2s Sending request on "nosuchsubject" No responders are available

Because the server knows every subscription, it can tell the asker immediately that no responder exists — no waiting out the timeout. In a program this arrives as a distinct no responders error, and it is one of NATS's kindest diagnostics: it separates "service is down" from "service is slow" without guessing.

  1. A responder exists but is slow or stuck. Then the asker's timeout is the only referee. --timeout (2 seconds in the example; the CLI's default is shorter than you would like for real work) decides how long a question stays open. Choose it from the answer's honest cost, not from hope.

  2. Several responders answer. A request goes to one inbox, and the asker takes the first answer; later ones are discarded. If you publish a request to a subject served by a queue group (next chapter), the group itself guarantees only one member even sees the question — the standard way to run a fleet of answerers without duplicate replies.

When to ask, when to announce

Announce facts (orders.new) for anyone who cares, now or — with JetStream — later. Ask questions (help.time) when exactly one answer decides what you do next, and accept that asking couples you to the answerer's health in a way announcing never does. Most real systems mix the two, subject tree by subject tree.

Check: run the static responder, ask twice, then kill the responder and ask again — the third question should end with No responders are available, not a hang.