NATS

Messages addressed by name, not by machine.

Baraa Said
Nadeen Jaber

Distributed Systems · Dr. Abdalkarim Awad · Birzeit University. Theory as step-by-step simulations, then a live cluster on this laptop that your phones will join.

Today, as subjects

Subscribe with talk.> to get all of it.

  1. talk.intro.whyWhy a messaging layerwiring services, what NATS is
  2. talk.core.*Core NATSsubjects, wildcards, three ways to talk
  3. talk.consensus.raftFailures and consensusclusters, JetStream, Raft, majorities
  4. talk.edge.leafnodeThe edgekeeps working offline
  5. talk.live.>Live on this laptopwe crash servers; your phones join
  6. talk.wrap.questionsKafka, RabbitMQ, takeawayswhen to pick NATS

Services have to find each other

Small program, whole messaging system

Open source (Apache 2.0), written in Go, a Cloud Native Computing Foundation project.

15.8 MBthe whole serverone file, nothing else to install
26 MBmemory, idle serversmall enough for a Raspberry Pi
40+client languagesGo, Python, Java, JS, C, Rust…

Sizes measured on our demo laptop: nats-server 2.15.3, macOS arm64. The live slide later shows the memory of each running server.

  1. originCreated by Derek Collison as the messaging system of Cloud Foundry, first written in Ruby.
  2. rewriteRewritten in Go for speed. The protocol stays plain text you can type by hand.
  3. 2021JetStream (v2.2) adds storage, replication and Raft to the same binary.
  4. 2025A dispute over the project's licence ends with NATS staying open source in the CNCF.
  5. todayMaintained mainly by Synadia. Used for microservices, IoT and edge systems.

Messages go to subjects, not to machines

Three ways to talk, all on subjects

Core NATS forgets. JetStream remembers.

Clients don't care which server they reach

A stream is a log, copied to three servers

Raft: who leads, and what happens when it dies

No majority, no writes

The edge keeps working when the link doesn't

Live: five acts, real servers

  1. 1
    Publish-subscribe and wildcardsnats sub "campus.>" nats sub "campus.*.temp" nats pub campus.lab1.temp "23.5"
  2. 2
    Queue group: three workers share nine jobsnats sub jobs --queue workers nats pub jobs "job {{Count}}" --count 9
  3. 3
    Request-replynats reply svc.time "it is {{Time}}" nats request svc.time ""
  4. 4
    JetStream: store 3 copies, then crash the leadernats stream add SENSORS --subjects "campus.>" --replicas 3 --defaults ./scripts/sensors.sh ./scripts/kill-leader.sh
  5. 5
    Edge: cut the cable, buffer, sync./scripts/start-edge.sh ./scripts/edge-link.sh down … up

Your phone is now a sensor

Live experiments with your phones

  1. 1
    Everyone: tap “Send reading” a few timesEach phone shows the sequence number the stream gave it.
  2. 2
    We crash the leader while you keep tapping./scripts/kill-leader.shFor a few seconds your phone says “waiting for a leader”, then it is stored.
  3. 3
    Tap “Join the workers”, then we send 30 jobsEach job goes to exactly one phone. Count yours.
  4. 4
    We crash a second server: no majority./scripts/kill-node.sh n1Your phone keeps retrying. Nothing is stored, nothing is lost.
  5. 5
    We restart both servers./scripts/start-node.sh n1 ./scripts/start-node.sh n2The retried readings go through.

How NATS compares

NATSApache KafkaRabbitMQ
ModelSubjects, plus optional stored streamsA partitioned, replicated logExchanges that route into queues
RoutingSubject hierarchy with wildcardsTopic, then partition by keyBindings and routing keys
StorageOptional, per streamAlways: the log is the productOptional, per queue
ReplicationA Raft group per streamLeader with in-sync replicas; Raft (KRaft) for metadataRaft in quorum queues
Runs asOne small binaryJava brokersErlang runtime
Best atMicroservices, IoT and edge, request-replyVery high-volume event pipelines and analyticsTask queues with complex routing rules

When NATS fits, and when it doesn't

A good fit when
  • services need simple, fast communication
  • devices sit at the edge on unreliable links
  • one tool should cover messaging, streams and key-value
  • a small team has to run it
Look elsewhere when
  • you keep years of events and analyse them with a big ecosystem: Kafka
  • you need windows and joins over streams: Flink
  • you depend on AMQP or elaborate routing rules: RabbitMQ

Takeaways

  1. Subjects decouple services: send to a name, not to a machine.
  2. JetStream + Raft: a write counts once a majority has it, and nothing else does.
  3. No majority, no writes: consistency is chosen over availability.
  4. The edge keeps working offline and syncs when the link returns.

Questions?

Any questions?

we have time for a few

Sources · docs.nats.io · raft.github.io (D. Ongaro and J. Ousterhout, “In Search of an Understandable Consensus Algorithm”, 2014) · CNCF and Synadia announcements on NATS licensing, 2025 · our own measurements, nats-server 2.15.3 · GIFs via GIPHY · demo code: ~/Documents/nats-demo