Internet Engineering
Fall 2022

Introduction to


Parham Alvani



  • NATS is a high-performance messaging system created by Derek Collison in 2010
  • It was originally built to serve as the message bus for Cloud Foundry
  • Handling internal communication among components of the system

It Addresses

  • Service Discovery
  • Low latency communication
  • Load balancing
  • Notification and events handling


  1. Client publishes message on foo subject
  2. Only clients subscribed to foo receive the message

How Simple!

INFO {"server_id":"NCNS32OEKOMMHTBVUYC7QWASFAP3BPEKGZG3QDIYRCBUL23WE3OVVBBO","server_name":"NCNS32OEKOMMHTBVUYC7QWASFAP3BPEKGZG3QDIYRCBUL23WE3OVVBBO","version":"2.1.7","proto":1,"git_commit":"bf0930e","go":"go1.13.10","host":"","port":4222,"max_payload":1048576,"client_id":1,"client_ip":""}

SUB greetings 1

PUB greetings 12
Hello World!

MSG greetings 1 12
Hello World!


  1. Client connected to NATS publishes a request on a help subject
  2. Worker client subscribed to help, processes requests, then sends response
  3. Audit client is subscribed to all subjects via a wildcard, so also receives message but does not reply
  4. Publisher receieves the response from the worker client


  1. Publisher sends multiple help requests on a help subject
  2. Multiple worker clients subscribed to the help subject from a distributed queue
  3. Each published request is randomly balanced to only one of the worker clients in the distributed queue

NATS As an Always Available Dial Tone

  • The main design constraints that define the style of the NATS project are:
    • Simplicity
    • Performance
    • Reliability
  • It does not offer any Persistence or Buffering
  • It is true Fire and Forget


  • NATS will try to protect itself at all costs to be available for all users
  • NATS client libraries, internally try to have an always established connection to one of the available NATS servers
  • In case a server fails, NATS will reconnect to another available server in the pool
  • NATS supports high-availablity via a clustering mode that is set up as a full-mesh of the servers

Slow Consumer

  • By default, if a client fails to drain the pending data that the server is holding for the client for over two seconds, the server will disconnect the client
  • Read more about it here


  • There is a PING/PONG interval happing that the client has to follow otherwise the server will reset the connection in case there are many PONG replies missing.
  • Server sends PING messages to client in each interval and client has to respond
  • Client also can send PING messages to the server

Subject Names

  • The dot character (.) has special meaning as part of the subject
  • When using it as part of the subject, we can create namespaces that can be later matched via wildcards
foo.* ✅, ✅
foo.*foo.hello ✅, ❌
foo.> ✅, ✅, foo.hello ✅
  • *: partial or token match wildcard
  • >: full wildcard

Queue Subscribtions: Hands-on

    Worker 1 👷

    INFO {"server_id":"NARXYLU3XEZSFYCFRV5WJ63GQS5WDV3WC4WAKCYDYY2IBGIETWJADZVS","server_name":"NARXYLU3XEZSFYCFRV5WJ63GQS5WDV3WC4WAKCYDYY2IBGIETWJADZVS","version":"2.1.8","proto":1,"git_commit":"c0b574f","go":"go1.14.8","host":"","port":4222,"max_payload":1048576,"client_id":4,"client_ip":""}
    SUB requests workers 5
    MSG requests 5 6

    Worker 2 👷

    INFO {"server_id":"NARXYLU3XEZSFYCFRV5WJ63GQS5WDV3WC4WAKCYDYY2IBGIETWJADZVS","server_name":"NARXYLU3XEZSFYCFRV5WJ63GQS5WDV3WC4WAKCYDYY2IBGIETWJADZVS","version":"2.1.8","proto":1,"git_commit":"c0b574f","go":"go1.14.8","host":"","port":4222,"max_payload":1048576,"client_id":5,"client_ip":""}
    SUB requests workers 55
    MSG requests 55 5

    Publisher ✍️

    INFO {"server_id":"NARXYLU3XEZSFYCFRV5WJ63GQS5WDV3WC4WAKCYDYY2IBGIETWJADZVS","server_name":"NARXYLU3XEZSFYCFRV5WJ63GQS5WDV3WC4WAKCYDYY2IBGIETWJADZVS","version":"2.1.8","proto":1,"git_commit":"c0b574f","go":"go1.14.8","host":"","port":4222,"max_payload":1048576,"client_id":6,"client_ip":""}
    PUB requests 5
    PUB requests 6

    Even More Features!

    • Jetstream 🚀
    • Clustering ✨
    • Super-Cluster with Gateways 🗺️

    References 📚

    Fork me on GitHub