Create a NATS JetStream durable consumer and reliably process messages with acknowledgment

domain: nats · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Ensure JetStream is enabled on the NATS server (--jetstream flag or jetstream: enabled in config); create a stream that captures your subject(s) by calling js.AddStream with Name, Subjects (e.g., ['orders.>']), Storage (file or memory), and retention policy
  2. Create a durable consumer on the stream using js.AddConsumer or the consumer API with Durable set to a stable name; set DeliverPolicy (all, last, new, or by start time/sequence), AckPolicy to explicit, and MaxDeliver to limit redelivery attempts
  3. Subscribe using the durable consumer name via js.Subscribe or js.PullSubscribe; for pull consumers call Fetch(n) in a loop to receive batches of messages — pull consumers give the application full control over consumption rate
  4. Acknowledge each message explicitly (msg.Ack()) after successful processing; use msg.Nak() to request immediate redelivery, or msg.NakWithDelay(duration) to back off before redelivery; use msg.Term() to permanently discard a message without redelivery
  5. Set the MaxDeliver limit on the consumer and configure a dead-letter stream: route terminated or MaxDeliver-exceeded messages to a separate stream using an advisory subject filter or a consumer with a separate subject transform
  6. Monitor consumer lag (NumPending) and redelivery rate via the NATS monitoring endpoint or nats consumer info <stream> <consumer> CLI command; high NumPending with low throughput indicates a stalled consumer

Known gotchas

Related routes

Create NATS JetStream streams and push/pull consumers
docs.nats.io · 5 steps · unrated
tune nats jetstream consumer flow control with ackwait, maxackpending, and maxdeliver to prevent redelivery storms
docs.nats.io · 5 steps · unrated
Configure a NATS JetStream stream's Republish setting to mirror new messages onto a core NATS subject without creating a consumer
nats · 6 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans