NATS messaging architecture

Microservices communication diagram from the NATS project blog. The diagram illustrates the messaging pattern, not a benchmark.

When services communicate directly with one another, every new dependency adds another address, retry policy, timeout, and failure mode to understand. A message broker can decouple producers from consumers, but heavyweight brokers are not always necessary for every event-driven system.

NATS Server is a lightweight, open-source messaging system built around subjects and publish-subscribe. Applications connect to a NATS server and send messages to named subjects without needing to know which service will receive them.

It appeared eighth in the Go daily GitHub Trending list on November 23, with 17 stars gained in that snapshot. That is a modest signal, not a viral surge. The more concrete reason to revisit the project is its current 2.15.3 release, published November 11, which adds operational controls for TLS, leaf nodes, gateways, and configuration reload checks.

Core NATS and JetStream are different guarantees

Core NATS is a fast publish-subscribe fabric. A publisher sends a message to a subject such as orders.created. Every connected subscriber with a matching subscription receives a copy. The publisher does not need a list of consumers, and consumers do not need to know the publisher's address.

The important trade-off is delivery. Core NATS does not store messages for disconnected subscribers. If nobody is listening at publish time, the message is not replayed later. That makes it a good fit for transient updates, cache invalidations, telemetry, and request/reply interactions where a missed message can be tolerated.

JetStream adds persistence. Streams store messages, consumers track progress, and applications can replay messages after reconnecting. JetStream provides at-least-once delivery semantics, so consumers should still be idempotent: retries can mean a message is processed more than once.

This distinction should drive the architecture decision. Do not assume that enabling a NATS server automatically turns every Core NATS subject into a durable queue.

Run NATS 2.15.3 locally

The official Docker image publishes a 2.15.3 tag. Start a local server with JetStream enabled:

docker run --rm --name nats \ -p 4222:4222 \ nats:2.15.3 -js 

For a minimal Go client, create a module and add the official client library:

mkdir nats-demo cd nats-demo go mod init example.com/nats-demo go get github.com/nats-io/nats.go@latest 

This small program subscribes to an order subject, waits until the subscription is registered, publishes one message, flushes the connection, and prints the received payload:

package main import ( "fmt" "log" "github.com/nats-io/nats.go" ) func main() { nc, err := nats.Connect(nats.DefaultURL) if err != nil { log.Fatal(err) } defer nc.Close() received := make(chan string, 1) _, err = nc.Subscribe("orders.created", func(msg *nats.Msg) { received <- string(msg.Data) }) if err != nil { log.Fatal(err) } if err := nc.Flush(); err != nil { log.Fatal(err) } if err := nc.Publish("orders.created", []byte("{\"id\":42}")); err != nil { log.Fatal(err) } if err := nc.Flush(); err != nil { log.Fatal(err) } fmt.Println(<-received) } 

The flush calls matter for short-lived programs. Publishing is buffered; flushing waits for the server round trip so the process does not exit before the message has been sent.

This example demonstrates Core NATS only. To try persistence, use the NATS CLI against the same server and create a JetStream stream:

nats stream add ORDERS --subjects "orders.>" --storage file --retention limits --defaults nats pub orders.new "Order #1001" nats consumer add ORDERS order-processor --pull --deliver all --ack explicit --defaults nats consumer next ORDERS order-processor --count 1 --ack 

The CLI commands are from the official JetStream documentation. Install the NATS CLI separately and point it at localhost:4222 before running them.

What changed in 2.15.3?

The 2.15.3 release includes several operational improvements:

  • Account subject mappings, imports, exports, and leaf-node remote permissions gain account-hash mapping functions.
  • TLS configuration can use optional client-certificate verification, allowing a certificate when supplied without requiring one for every connection.
  • Leaf-node and gateway connections gain configurable ping intervals.
  • The command line adds a configuration reload test mode. The test returns exit code 3 when a change requires a restart instead of a reload.
  • The health endpoint can report an unavailable state until configured leaf-node remotes are connected.

These changes are useful for teams running multi-account deployments, mixed certificate-authentication environments, or edge-connected clusters. They are operational controls rather than a new messaging model. The 2.15 upgrade guide is the right place to review compatibility notes before upgrading from 2.14.

When should you choose NATS?

NATS is attractive when you need lightweight service communication, request/reply, queue groups, or persistent streams without adopting a larger platform first. Its single server binary can support clustering, leaf nodes, MQTT, WebSockets, and JetStream through configuration.

Compare it with the alternatives:

  • Kafka is often a better fit when you need a large event-log ecosystem, long retention, partition-oriented processing, and extensive stream-processing integrations.
  • RabbitMQ is a strong choice when routing semantics, work queues, acknowledgements, and established AMQP workflows are central.
  • Redis Streams can be convenient when Redis is already part of the architecture and the stream workload is closely coupled to that data layer.
  • NATS fits teams that value simple subject-based messaging, low operational overhead, and the ability to add persistence when needed.

NATS is not automatically the best choice for every event pipeline. Core NATS messages are ephemeral, JetStream requires stream and consumer design, and at-least-once processing requires idempotency and careful acknowledgement handling. Large payloads should generally be stored elsewhere and referenced by key rather than pushed through the messaging layer.

NATS is an Apache-2.0 project and a CNCF incubating project. The project maintains official clients for Go, JavaScript/TypeScript, Python, Java, Rust, C#/.NET, and C.

The NATS project links to this conference overview of its messaging design and operational model:

A sensible first production step

Start with a single non-critical event path. Measure message size, publish rate, consumer lag, reconnect behavior, and what happens when a consumer is offline. Then decide whether Core NATS is enough or whether the event needs a JetStream stream and durable consumer.

Before upgrading a production cluster, read the 2.15 upgrade notes, test configuration reloads, and verify that health checks cover the leaf-node or gateway connections your topology depends on.

Would you use NATS for service-to-service events in a new application, or do you already have Kafka, RabbitMQ, or Redis Streams handling that responsibility?

Sources and further reading

About the author: GitHub · LinkedIn · YouTube