NATS101

Internet Engineering
Fall 2022
@1995parham

Introduction to

NATS

Parham Alvani

@1995parham

History

  • 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

Overview

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

How Simple!


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

SUB greetings 1
+OK

PUB greetings 12
Hello World!
+OK

MSG greetings 1 12
Hello World!
  

Request/Response

  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
rr

Queue

  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
queues

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

Availability

  • 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
ha

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
sc

Ping/Pong

  • 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
ping-pong

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
SubjectMatches
foo.*.barfoo.hello.bar ✅, foo.hi.bar ✅
foo.*foo.hello ✅, foo.hi.bar ❌
foo.>foo.hello.bar ✅, foo.hi.bar ✅, 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":"0.0.0.0","port":4222,"max_payload":1048576,"client_id":4,"client_ip":"172.22.0.1"}
    SUB requests workers 5
    +OK
    
    MSG requests 5 6
    second
        

    Worker 2 👷

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

    Publisher ✍️

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

    Even More Features!

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

    References 📚

    Fork me on GitHub