Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Fast messaging with Nats and Go

9e13b6114dfd38952b3699dd779c6958?s=47 Federico Paolinelli
October 23, 2018
820

Fast messaging with Nats and Go

Quick intro about Nats messaging system

9e13b6114dfd38952b3699dd779c6958?s=128

Federico Paolinelli

October 23, 2018
Tweet

Transcript

  1. FAST MESSAGING WITH GO AND NATS

  2. About me: Federico Paolinelli Lead developer @ List @fedepaol github.com/fedepaol

    fedepaol@gmail.com 2
  3. WHY MESSAGING 3

  4. Good points of messaging ▷ Decoupling ▷ Scaling ▷ Event

    sourcing ▷ Reactive paradigms 4
  5. 50 SHADES OF MESSAGING 5

  6. 6

  7. What to look for ▷ Messaging patterns ▷ Durability of

    the messages ▷ Ordering guarantees ▷ Routing capabilities ▷ Dev / Ops experience ▷ Performance ▷ Auth / authz 7
  8. What makes a good communication system? 8

  9. Enterprise service Buses 9

  10. “ We've seen many products and approaches that stress putting

    significant smarts into the communication mechanism itself [..] The microservice community favours an alternative approach: smart endpoints and dumb pipes 10 Martin Fowler
  11. NATS: Dumb but not dumber 11

  12. The protocol PUB Publish a message to a subject, with

    optional reply subject SUB Subscribe to a subject (or subject wildcard) MSG Delivers a message payload to a subscriber UNSUB Unsubscribe (or auto-unsubscribe) from subject 12
  13. Subscription Wildcards ▷ foo.bar ▷ foo.bar.baz ▷ foo.* ▷ foo.>

    ▷ foo.*.baz 13
  14. The server 14

  15. The server ▷ Single go executable ▷ Full mesh of

    servers ▷ Gossip based autodiscovery 15 gnatsd -p 4222 -cluster nats://localhost:4248 gnatsd -p 5222 -routes nats://localhost:4248
  16. 16 Nats Server 1 Nats Server 2 Nats Server 3

    Client 1 PUB HI.ALL HELLO Client 2 MSG HI.ALL HELLO Full Mesh of servers
  17. Guarantees ▷ at most once ▷ messages ordered per publisher

    17
  18. 18 Always up Dialtone

  19. What can I do with it? 19

  20. Publish Subscribe! 20 Nats Server 2 Client 1 PUB HI.ALL

    HELLO Client 2 MSG HI.ALL HELLO
  21. Queueing 21 Nats Server 2 Client 1 PUB HI.ALL HELLO

    Client 2 MSG HI.ALL HELLO Client 2 PUB HI.ALL HELLO1 MSG HI.ALL HELLO1
  22. Request - Reply 22 Nats Server 2 Client 1 PUB

    HI.ALL REPLY_HERE HELLO ? Client 2 MSG HI.ALL HELLO? Client 2 MSG HI.ALL HELLO? MSG REPLY_HERE GOLAB! MSG REPLY_HERE GOLAB!
  23. Using nats with go 23

  24. The Go client ▷ Knows about all the nodes of

    the mesh ▷ Reconnects gracefully ▷ Buffers the messages to be sent (up to 8MB) ▷ Buffers the messages to be consumed (65k / 65Kb) 24
  25. The Go client: pub/sub 25 nc, _ := nats.Connect(nats.DefaultURL) nc.Publish("foo",

    []byte("Hello World")) nc.Subscribe("foo", func(m *nats.Msg) { // handle the message }) nc.Flush() // chan subscribe ch := make(chan *nats.Msg, 64) sub, err := nc.ChanSubscribe("foo", ch) msg := <- ch
  26. The Go client: request 26 // Requests var response string

    err := c.Request("help", "help me", &response, 10*time.Millisecond) if err != nil { fmt.Printf("Request failed: %v\n", err) } // Replying c.Subscribe("help", func(subj, reply string, msg string) { c.Publish(reply, "I can help!") }) // Queue group reply nc.QueueSubscribe("help", "workers", func(msg *Msg) { c.Publish(msg.Reply, "I can help!") })
  27. The Go client ▷ Connection lifecycle callbacks ▷ Sync /

    async api ▷ Type safe encoders ▷ Context based requests 27
  28. “ Simplicity is complicated but the clarity is worth the

    fight. 28 Rob Pike
  29. An example 29 Nats Server Source PUB SENSORS.TEMP 0, 1,

    2
  30. An example 30 Nats Server Source Client

  31. An example 31 Nats Server Source Client REQ SENSORS.CURRENT SUB

    SENSORS.TEMP
  32. An example 32 Nats Server Source Client REQ SENSORS.CURRENT PUB

    SENSORS.CURRENT 2
  33. An example 33 Nats Server Source Client PUB SENSORS.TEMP 3,

    4, 5...
  34. Is it fast? 34

  35. Nats benchmarking tool 35 go run ./nats-bench.go -ms 16 -np

    1 -ns 5 -n 1000000 foo Starting benchmark [msgs=1000000, msgsize=16, pubs=1, subs=5] NATS Pub/Sub stats: 8,729,005 msgs/sec ~ 133.19 MB/sec Pub stats: 1,457,683 msgs/sec ~ 22.24 MB/sec Sub stats: 7,276,675 msgs/sec ~ 111.03 MB/sec [1] 1,456,749 msgs/sec ~ 22.23 MB/sec (1000000 msgs) [2] 1,455,785 msgs/sec ~ 22.21 MB/sec (1000000 msgs) [3] 1,455,848 msgs/sec ~ 22.21 MB/sec (1000000 msgs) [4] 1,455,864 msgs/sec ~ 22.21 MB/sec (1000000 msgs) [5] 1,455,335 msgs/sec ~ 22.21 MB/sec (1000000 msgs) min 1,455,335 | avg 1,455,916 | max 1,456,749 | stddev 459 msgs
  36. Bravenewgeek benchmarks 36 Source : https://bravenewgeek.com/benchmarking-message-queue-latency/

  37. Other features ▷ TLS support ▷ Monitoring endpoints ▷ Authorization

    scheme ▷ Configuration reload ▷ Authentication ▷ K8s operator ▷ Prometheus exporter 37
  38. Nats streaming (codename stan) 38

  39. 39

  40. Features ▷ At least once guarantee ▷ Messages encoded with

    protobuf ▷ Durable subscriptions ▷ Queue groups ▷ Message Replay (date/time, offset, seqnum) 40
  41. Features ▷ SQL, File or memory storage ▷ Partitioning ▷

    Clustering ▷ Active / Passive ▷ No wildcarding 41
  42. Something to keep an eye on 42

  43. LiftBridge ▷ A nats streaming alternative ▷ github.com/liftbridge-io/liftbridge ▷ Promises

    some features missing from nats streaming ▷ Can work together with Nats 43
  44. Just give it a try! 44

  45. Thanks! Any questions? This slides: goo.gl/fJY6rw @fedepaol fedepaol@gmail.com 45