NATS
NATS from the service's side: core publish–subscribe with its at-most-once delivery, subjects and wildcards, queue groups, request–reply, and JetStream streams, pull consumers, acks with redelivery and publish deduplication.
8 questions
JuniorTheoryVery commonWhat delivery guarantee does core NATS give, and what happens to a message no one is subscribed to?
What delivery guarantee does core NATS give, and what happens to a message no one is subscribed to?
At-most-once: the server passes each message to the subscribers connected at that moment and stores nothing. A message on a subject no one listens to is dropped, and the publisher is not told. A subscriber that was offline never gets it later.
Common mistakes
- ✗Expecting
Publishto fail when no one is subscribed: core NATS drops the message silently, and the publisher learns nothing - ✗Expecting a subscriber that reconnects to get what it missed: core NATS keeps no copy of a message once it has forwarded it
Follow-up questions
- →Why does the NATS server disconnect a slow consumer instead of buffering messages for it without limit?
- →What would you add to a service that must not lose a message published while it restarts?
JuniorTheoryVery commonWhat does a JetStream stream add to core NATS?
What does a JetStream stream add to core NATS?
A stream stores the messages published to its subjects, on disk or in memory, numbered in sequence. Limits on count, size and age decide what stays. Consumers read it from any position, so one that was offline still finds what the stream holds. The publisher gets an ack for each message.
Common mistakes
- ✗Expecting a stream to delete a message once a consumer has read it: by default it keeps messages until a limit on count, size or age removes them
- ✗Expecting a stream to make delivery exactly-once: a stored message can be delivered again, so consumers still handle duplicates
Follow-up questions
- →How do the retention policies
limits,interestandworkqueuechange when a stream deletes a message? - →Why does a JetStream publish wait for an ack while a core NATS publish does not?
JuniorTheoryVery commonHow does a queue group share messages, and what does a plain subscriber on the same subject get?
How does a queue group share messages, and what does a plain subscriber on the same subject get?
The server gives each message to one member of the group, picked at random, so the members split the load. A plain subscriber on the same subject still gets its own copy of every message, and so does every other queue group.
Common mistakes
- ✗Expecting a queue group to keep other subscribers from getting the message: plain subscriptions on the subject still get every message
- ✗Expecting the server to hold messages while every member is busy: core NATS hands each message over at once and keeps no queue
Follow-up questions
- →How do you add workers to a NATS service without changing the publisher?
- →What happens to the messages in flight when a member of a queue group disconnects?
JuniorTheoryVery commonHow do the * and > wildcards match NATS subjects?
How do the * and > wildcards match NATS subjects?
A subject is tokens separated by dots. * matches exactly one token in its place, and > matches one or more tokens and may stand only last. So orders.> matches orders.eu.new but not orders, and a wildcard counts only as a whole token.
Common mistakes
- ✗Reading
>as zero or more tokens: it needs at least one, soorders.>does not matchorders - ✗Writing a wildcard inside a token, as in
orders*: wildcards work only as whole tokens, so such a token is a literal
Follow-up questions
- →What happens when a publisher puts a wildcard in the subject it publishes to?
- →How would you name subjects so that one service can subscribe to all orders of one region?
MiddleTheoryCommonWhat do AckWait and MaxDeliver control, and what do Nak, Term and InProgress do?
What do AckWait and MaxDeliver control, and what do Nak, Term and InProgress do?
AckWait (30 s by default) is how long the server waits for an ack before redelivering; MaxDeliver caps the deliveries and is unlimited by default. Nak asks for redelivery now or after a delay, Term stops redelivery for good, and InProgress restarts the AckWait timer.
Common mistakes
- ✗Leaving
MaxDeliverat its default for a message that always fails: there is no limit, so it comes back after everyAckWaitforever - ✗Running a job longer than
AckWaitwithoutInProgress: the server redelivers the message, possibly to another instance, while the first one is still working
Follow-up questions
- →How does
BackOffchange the gaps between redeliveries of one message? - →How can a service find out that a message has used up its
MaxDeliver?
MiddleTheoryCommonHow does the Nats-Msg-Id header deduplicate publishes, and what are its limits?
How does the Nats-Msg-Id header deduplicate publishes, and what are its limits?
The stream remembers each Nats-Msg-Id for its duplicate window, 2 minutes by default, from the first publish. A repeat within it is not stored, and the ack returns the original sequence with duplicate: true. After the window the id is new again, and consumer redeliveries are not covered.
Common mistakes
- ✗Expecting each repeat to extend the window: the id is kept from its first publish, so a retry after the window is stored again
- ✗Dropping consumer idempotency because publishes are deduplicated: a redelivery after a lost ack still brings the same message twice
Follow-up questions
- →Why can the duplicate window not be longer than the stream's
MaxAge? - →What does a long duplicate window cost a stream that takes many messages per second?
MiddleTheoryCommonWhy use a durable pull consumer in JetStream, and how does it differ from an ephemeral one?
Why use a durable pull consumer in JetStream, and how does it differ from an ephemeral one?
A pull consumer sends only what the client asks for with Fetch or Messages, so each instance works at its own pace and several instances share one consumer. A durable one outlives the clients that read from it and keeps its position; an ephemeral one goes away once nobody uses it.
Common mistakes
- ✗Using an ephemeral consumer for a service that restarts: once it goes away, the new consumer starts from a position it chose, not where the old one stopped
- ✗Giving each instance its own durable consumer to share work: each consumer reads the whole stream, so every instance gets every message
Follow-up questions
- →How does
MaxAckPendinglimit the work one pull consumer hands out? - →When would you still choose a push consumer over a pull consumer?
MiddleTheoryCommonHow does NATS request–reply work, and how does a requester tell no responders from a time-out?
How does NATS request–reply work, and how does a requester tell no responders from a time-out?
The requester publishes the request with a unique _INBOX. reply subject that it listens on; the first reply wins. With no subscriber on the subject, the server sends back a 503 status at once (ErrNoResponders); ErrTimeout means no reply came in time.
Common mistakes
- ✗Waiting out the whole time-out when no one serves the subject: the server answers with a 503 status at once, and
nats.goreturnsErrNoResponders - ✗Reading a time-out as proof that no one got the request: a responder may have received it and still be working on it
Follow-up questions
- →Why is it safe to run several responders for one subject in a queue group?
- →Why must a requester be careful about retrying a request after a time-out?