NATS

NATS is an open-source message broker created in 2011 and donated to the CNCF in 2018 (currently at the Incubating level). It provides a simple, secure, and high-performance messaging system for cloud-native applications, IoT, and microservices.

NATS is structured around three layers:

  • Communication: core message broker (pub/sub, queue groups, request/reply)
  • Storage: persistent streaming via JetStream (streams, consumers, key-value store, object store)
  • Workload: execution engine via Nex

Installation

Install the NATS CLI:

# macOS
brew install nats-io/nats-tools/nats

# Linux
curl -sf https://binaries.nats.dev/nats-io/natscli/nats@latest | sh

Run a local NATS server with Docker:

docker run -p 4222:4222 nats:latest

Pub/Sub

In publish/subscribe mode, messages are sent to a subject. Every subscriber receives a copy of the message (fan-out).

    graph LR
    P[Publisher] -->|msg1| S((Subject))
    S -->|msg1| A[Subscriber]
    S -->|msg1| B[Subscriber]
    S -->|msg1| C[Subscriber]
  
# Subscribe to a subject
nats sub greetings

# Publish a message
nats pub greetings "Hello, NATS!"

Queue Groups

With a queue group, only one subscriber in the group handles each message — NATS distributes them for load balancing.

    graph LR
    P[Publisher] -->|msgs 1,2,3| Q((Queue))
    Q -->|msg 2| A[Subscriber]
    Q -->|msg 1| B[Subscriber]
    Q -->|msg 3| C[Subscriber]
  
nats sub --queue workers tasks

Request / Reply

The publisher sends a request to a subject and waits. A subscriber handles the message and sends back a reply directly to the publisher.

    graph LR
    P[Publisher] -->|msg1| S((Subject))
    S -->|msg1| A[Subscriber]
    S -->|msg1| B[Subscriber]
    S -->|msg1| C[Subscriber]
    C -->|reply| R((Reply))
    R -->|reply| P
  
# Start a responder
nats reply greet "Hello, {{.Subject}}!"

# Send a request and wait for the reply
nats req greet "world"

JetStream — Storage Layer

JetStream is the persistence layer built into the NATS server. It lets you store and replay messages through streams (which store messages) and consumers (which track a client’s position in a stream).

# Create a stream
nats stream add ORDERS --subjects "orders.>" --storage file --retention limits

# List streams
nats stream ls

# Add a consumer
nats consumer add ORDERS processor --deliver all --ack explicit

# Publish a persistent message
nats pub orders.new "order-123"

# View stream information
nats stream info ORDERS

Key-Value Store

JetStream provides a key-value API on top of streams, useful for configuration, service discovery, or lightweight state management.

# Create a bucket
nats kv add config

# Put a value
nats kv put config db_host "localhost"

# Get a value
nats kv get config db_host

# Watch for changes
nats kv watch config

# Delete a key
nats kv del config db_host

# List all keys
nats kv ls config

Object Store

The object store lets you store and retrieve arbitrary binary data (files, blobs) via NATS, also backed by JetStream.

# Create a bucket
nats object add assets

# Upload a file
nats object put assets ./logo.png

# List objects in a bucket
nats object ls assets

# Download an object
nats object get assets logo.png

# Delete an object
nats object del assets logo.png

Going Further