Skip to content

NATS

NATS provides lightweight, high-performance messaging with simple operations and cloud-native design.


StrengthBenefit
LightweightSingle binary, minimal resources
FastSub-millisecond latency
SimpleMinimal configuration
Cloud nativeBuilt for Kubernetes
JetStreamPersistent streaming option

ConcernConsideration
Less matureSmaller ecosystem than Kafka/RabbitMQ
DurabilityRequires JetStream for persistence

[bus]
backend = "nats"
[bus.nats]
url = "nats://localhost:4222"
# For JetStream (persistent)
jetstream = true
stream_name = "angzarr-events"
Terminal window
export NATS_URL="nats://localhost:4222"
export BUS_BACKEND="nats"

NATS uses dot-separated subjects for hierarchical routing:

angzarr.events.player.PlayerRegistered
angzarr.events.player.FundsDeposited
angzarr.events.hand.CardsDealt

Wildcards enable flexible subscriptions:

angzarr.events.player.* # All player events
angzarr.events.*.> # All events
angzarr.events.hand.> # All hand events and sub-subjects

  • Pub/sub without persistence
  • At-most-once delivery
  • Lowest latency
  • Use for ephemeral events
  • Stream persistence
  • At-least-once delivery
  • Consumer replay
  • Use for event sourcing
# Enable JetStream for event sourcing
[bus.nats]
jetstream = true

Terminal window
# Create stream
nats stream add angzarr-events \
--subjects "angzarr.events.>" \
--retention limits \
--max-msgs -1 \
--max-bytes -1 \
--max-age 7d \
--storage file \
--replicas 3
# Create consumer
nats consumer add angzarr-events player-projector \
--filter "angzarr.events.player.>" \
--ack explicit \
--deliver all \
--max-deliver 5 \
--replay instant

Terminal window
# Core NATS
docker run -p 4222:4222 nats:latest
# NATS with JetStream
docker run -p 4222:4222 nats:latest -js

values.yaml
bus:
backend: nats
nats:
enabled: true
url: nats://nats.messaging.svc.cluster.local:4222
jetstream: true
streamName: angzarr-events

NATS provides a monitoring endpoint:

http://localhost:8222/varz # Server stats
http://localhost:8222/connz # Connections
http://localhost:8222/subsz # Subscriptions
http://localhost:8222/jsz # JetStream stats

  • Edge computing — Minimal resource footprint
  • Kubernetes native — Designed for containers
  • Simple operations — Single binary, little config
  • High performance — Sub-millisecond latency

  • AMQP — More features, larger ecosystem
  • Kafka — Higher throughput alternative