NATS is a simple, high performance open source messaging system for cloud native applications. It provides a basic publish-subscribe messaging protocol along with durable message queues through NATS Streaming. NATS is lightweight, fast, and scalable. It handles over 11 million messages per second and provides predictable performance and resilience through features like cluster support and protection against slow consumers.
3. NATS is a simple, high performance open source
messaging system for cloud native applications.
3 . 1
4. Facts about NATS
Written by Derek Collison in 2010, originally for CloudFoundry
Initial implementation in Ruby, was then rewritten in Go in 2012
Project used in production environments for ~7 years
NATS Server v1.0.0 milestone reached in July 2017
Github Organization: https://github.com/nats-io
4 . 1
12. Simplicity in NATS
Plain text protocol with few commands β Simple network clients & API
7MB binary with no dependencies β Small Docker image
Fire & Forget Pub Sub on top of TCP/IP
| PUB | SUB | UNSUB | CONNECT | INFO | MSG | -ERR | +OK | PING | PONG |
telnet demo.nats.io 4222
Connected to demo.nats.io.
INFO {"server_id":"Fzwx2ndlHFg3lvVwwdBRSe","tls_required":false,...,"max_payload":1048576}
SUB hello 1
+OK
PUB hello 5
world
+OK
MSG hello 1 5
world
12 . 1
18. Basic API: C# Client
using NATS.Client;
ConnectionFactory cf = new ConnectionFactory();
IConnection c = cf.CreateConnection();
ISyncSubscription sSync = c.SubscribeSync("hello");
// Wait up to 1000 ms for next message
Msg m = sSync.NextMessage(1000);
c.Publish("hello", Encoding.UTF8.GetBytes("world"));
18 . 1
19. Basic API: Java Client
Connection nc = Nats.connect();
nc.subscribe("foo", m -> {
System.out.printf("Received a message: %sn", new String(m.getData()));
});
nc.publish("foo", "Hello World".getBytes());
19 . 1
26. NATS & Delivery Guarantees
The NATS Server is re & forget, providing at-most once delivery.
This means that if the consumer is not connected it will not receive the message!
26 . 1
27. Is NATS a Message Broker/Queue?
From the book (Kleppmann, O'Reilly)
"a message broker (also known as a message queue), is essentially a kind of database
that is optimized for handling message streams."
βγ
Designing Data-Intensive Applications
27 . 1
28. Is NATS a Message Broker/Queue?
From the book (Kleppmann, O'Reilly)
"By centralizing the data in the broker, these systems can more easily tolerate clients
that come and go (connect, disconnect, and crash)
"the question of durability is moved to the broker instead
βγ
Designing Data-Intensive Applications
28 . 1
29. Is NATS a Message Broker/Queue?
From the book (Kleppmann, O'Reilly)
"Faced with slow consumers, they generally allow unbounded queueing (as opposed to
dropping messages or backpressure)
βγ
Designing Data-Intensive Applications
29 . 1
30. The NATS Server does none of that!
It's the total opposite.
30 . 1
33. NATS Streaming
Supports at-least-once delivery model and durability of messages
Rate limiting & message redelivery features
First release in 2016
33 . 1
34. NATS Streaming: How it works
Designed as a request/response protocol which uses NATS as its transport.
34 . 1
35. NATS Streaming: How it works
Designed as a request/response protocol which uses NATS as its transport.
35 . 1
36. NATS Streaming: How it works
Designed as a request/response protocol which uses NATS as its transport.
36 . 1
37. NATS Streaming: How it works
Designed as a request/response protocol which uses NATS as its transport.
37 . 1
38. NATS Streaming Clustering
Clustering support in the works!
To be fair, you have to have a very high IQ to understand Rick & Morty clustering
https://github.com/nats-io/nats-streaming-server/issues/316
38 . 1
39. NATS Streaming: Go Client
Publish and persist a message
Start with last received message
Receive all messages
sc, _ := stan.Connect(clusterID, clientID)
sc.Publish("foo", []byte("Hello World"))
sub, err := sc.Subscribe("foo", func(m *stan.Msg) {
fmt.Printf("Received a message: %sn", string(m.Data))
})
sub, err := sc.Subscribe("foo", func(m *stan.Msg) {
fmt.Printf("Received a message: %sn", string(m.Data))
}, stan.DeliverAllAvailable())
39 . 1
40. NATS Streaming: Ruby Client
require 'stan/client'
sc = STAN::Client.new
# Customize connection to NATS
opts = { servers: ["nats://127.0.0.1:4222"] }
sc.connect("test-cluster", "client-123", nats: opts)
# Simple async subscriber
sub = sc.subscribe("foo", start_at: :first) do |msg|
puts "Received a message (seq=#{msg.seq}): #{msg.data}"
end
# Synchronous Publisher, does not return until an ack
# has been received from NATS Streaming.
sc.publish("foo", "hello world")
40 . 1
41. NATS Streaming: Python Client
nc = NATS()
await nc.connect(io_loop=loop)
sc = STAN()
await sc.connect("test-cluster", "client-123", nats=nc)
await sc.publish("hi", b'hello')
async def cb(msg):
print("Received a message (seq={}): {}".format(msg.seq, msg.data))
# Subscribe to get all messages since beginning.
sub = await sc.subscribe("hi", start_at='first', cb=cb)
await sub.unsubscribe()
41 . 1
42. NATS Streaming: Node.js Client
var stan = require('node-nats-streaming').connect('test-cluster', 'test');
stan.on('connect', function () {
stan.publish('hello', 'world', function(err, guid){ /*...*/ });
var opts = stan.subscriptionOptions().setStartWithLastReceived();
var subscription = stan.subscribe('hello', opts);
subscription.on('message', function (msg) {
console.log('Received [' + msg.getSequence() + '] ' + msg.getData());
});
});
42 . 1
43. NATS Streaming: Java Client
StreamingConnectionFactory cf = new StreamingConnectionFactory("test-cluster", "bar");
StreamingConnection sc = cf.createConnection();
sc.publish("foo", "Hello World".getBytes());
Subscription sub = sc.subscribe("foo", new MessageHandler() {
public void onMessage(Message m) {
System.out.printf("Received a message: %sn", new String(m.getData()));
doneSignal.countDown();
}
}, new SubscriptionOptions.Builder().deliverAllAvailable().build());
43 . 1
44. NATS Streaming: C# Client
var cf = new StanConnectionFactory();
using (var c = cf.CreateConnection("test-cluster", "appname"))
{
using (c.Subscribe("foo", (obj, args) =>
{
Console.WriteLine(
System.Text.Encoding.UTF8.GetString(args.Message.Data));
}))
{
c.Publish("foo", System.Text.Encoding.UTF8.GetBytes("hello"));
}
}
44 . 1
46. Remember: There are 2 servers
NATS Server / gnatsd
NATS Streaming Server / nats-streaming-server
NATS Streaming itself uses the NATS Server: Two birds, one stone
https://github.com/nats-io/gnatsd
https://github.com/nats-io/nats-streaming-server
46 . 1