Synadia/NATS Team Presentations for NATS Connect Live on April 16, 2020. To see the recorded event, go to our NATS YouTube Channel https://youtube.com/c/nats_messaging
27. NATS Connect Live April 16, 2020
Writing a NATS
Client in a
Language Geared
for Performance
and Correctness
Rust and NATS
By: Tyler Neely
28. NATS Connect Live April 16, 2020 2
Who am I
● Building databases and distributed systems in Rust
since 2014
○ Performance Critical ⋂ Correctness Critical
systems
○ Created the http://sled.rs embedded lock-free
storage engine
● I became an engineer at Synadia because:
○ computation is becoming more location-agnostic
○ NATS is extremely well positioned to deal with
this world
● I’m a core maintainer of the Rust client
○ github.com/nats.io/nats.rs
29. NATS Connect Live April 16, 2020
Roadmap for
This Talk
● Intro to Rust
● Our Rust Client
● The Future
3
30. NATS Connect Live April 16, 2020
Rust History
● Began as a personal project of an
engineer who worked at Mozilla
Research in 2006
● Mozilla decided to invest in the
project in 2009, with the stretch
goal of potentially replacing
security-critical C++ code in Firefox
● The Mozilla Servo experimental
browser engine project began in
2012 to experiment with taking
advantage of Rust’s features
● In 2014, Rust 1.0 was released
● Voted Most Loved Language in
Stack Overflow’s annual developer
survey every year since 2016
4
31. NATS Connect Live April 16, 2020
Rust Interest
5
subredditstats.com/r/rust
32. NATS Connect Live April 16, 2020
Rust’s Advantages
● Excellent ergonomics for “programming in the large”
● Has a great built-in package manager, cargo
○ Automatic documentation generation, docs.rs/natsio
● Prevents memory corruption and data race bugs at compile-time
○ no GC or runtime race detector required
■ is quite nice to use for embedded programming
○ “Ownership” and “borrowing” guarantee:
■ no dangling references
■ at-most-once destruction
○ Microsoft: 70% of CVEs are related to memory corruption in C and C++
● Performs on-par with or better than C/C++
○ No need to make defensive copies
○ Can approach Fortran performance due to alias avoidance
● Can incrementally replace parts of a C/C++ codebase
○ We call this “oxidation”
○ Plays great with other languages via FFI
6
33. NATS Connect Live April 16, 2020
Who uses Rust?
● Google’s potential Android replacement, Fuchsia
● Amazon’s Firecracker VM which powers AWS Lambda
● Microsoft Azure IoT
● Facebook’s HHVM
● Twitter’s caching and build systems
● VxWorks RTOS (powers Mars Rover) now supports Rust
● Dropbox has rewritten its correctness-critical replication logic in Rust
7
36. NATS Connect Live April 16, 2020
Major Feature: Ownership
● By having items consumed by default when passed to other scopes, we have the
ability to treat functions as proofs that certain actions can only happen once
○ Useful for building strongly-typed state machines
○ Useful for building configuration objects that prevent misuse
○ Useful for encoding complex embedded hardware behavior using the type
system
● None of this adds any runtime cost. It is enforced at compile-time.
● We call this a “zero cost abstraction”
10
37. NATS Connect Live April 16, 2020
Major Feature: Ownership
● By having items consumed by default when passed to other scopes, we have the
ability to treat functions as proofs that certain actions can only happen once
○ Useful for building strongly-typed state machines
○ Useful for building configuration objects that prevent misuse
○ Useful for encoding complex embedded hardware behavior using the type
system
● None of this adds any runtime cost. It is enforced at compile-time.
● We call this a “zero [runtime] cost abstraction”
11
38. NATS Connect Live April 16, 2020
Major Feature: Borrowing
● If you don’t want to give a variable to a function that uses it, you may use Rust’s
borrowing system to pass a reference, instead.
● The borrow checker guarantees that the reference never outlives the object it
points to.
○ C/C++ programmers often discover dangling pointer bugs in their existing
codebases when porting them to Rust. This kind of bug is often taken
advantage of in exploits that attack systems.
● It is like a compile-time reader-writer lock.
○ several immutable references XOR a single mutable reference
● This provides a benefit that is quite similar to functional purity, while letting users
express themselves in a familiar imperative style
12
43. NATS Connect Live April 16, 2020
Our Rust NATS Client
We use typestate programming to make configuration less error-prone
17
44. NATS Connect Live April 16, 2020
Our Rust NATS Client
We use borrowing to pin a subscription’s iterator to the lifetime of the
subscription itself, which guarantees that our connection won’t close while we’re
still iterating over it
18
45. NATS Connect Live April 16, 2020
Our Rust NATS Client
● Rust has a tagged union structure called an “enum”
● We use this to handle internal state transitions in clean ways that reduce the
chance of certain classes of bugs during reconnection
19
48. NATS Connect Live April 16, 2020
TLS and nkeys coming very soon :)
22
49. NATS Connect Live April 16, 2020 23
Testing
● Where do bugs come from in distributed systems?
○ Simple Testing Can Prevent Most Critical Failures: An Analysis
of Production Failures in Distributed Data-intensive Systems
○ “almost all (92%) of the catastrophic system failures are the
result of incorrect handling of non-fatal errors explicitly signaled
in software.”
○ “in 58% of the catastrophic failures, the underlying faults could
easily have been detected through simple testing of error
handling code.”
● We built a “buggy server” that injects many disconnections per
second
51. NATS Connect Live April 16, 2020
Profile Everything!
github.com/flamegraph-rs/flamegraph
25
52. NATS Connect Live April 16, 2020 26
The Future of Rust & NATS
● Because Rust was built to avoid the need for GC, it’s able to be deployed in
extremely flexible ways.
● One of Rust’s most exciting compilation targets is WebAssembly - a highly
portable code format which plays nicely with sandboxed execution environments
○ This may be quite useful for allowing users to supply their own logic to
filter or transform messages
● Rust is a great language for building storage systems in
● Stay tuned for Derek’s Future Work talk ;)
53. NATS Connect Live April 16, 2020
Thank you!
Check it out and let us know what you think!
https://github.com/nats-io/nats.rs
https://crates.io/crates/natsio
https://docs.rs/natsio
27
54. NATS Connect Live April 16, 2020
NATS JetStream
The future of NATS Streaming
55. NATS Connect Live April 16, 2020 2
Speaker
R.I. Pienaar / @ripienaar
Creator of choria.io, Maintainer for NATS-Streaming-Server, Surveyor, JetStream, and
others
56. NATS Connect Live April 16, 2020 3
What is JetStream?
JetStream is the Next Generation of NATS Streaming technologies building on what we
learned from NATS Streaming Server and taking it into the NATS 2.0 future.
✓ Delivered as part of standard NATS Server in same binary
✓ At-Least-Once delivery model enabled on specific Subjects
✓ Interaction using existing core NATS Client
✓ NATS 2.0 Multi Tenancy and Routing Topologies
✓ Push and Pull message consumption
✓ Work Queue semantics
✓ JSON Administration API
✓ Administration via CLI or Terraform
✓ Currently in Technical Preview @ https://github.com/nats-io/jetstream
58. NATS Connect Live April 16, 2020 5
New NATS CLI and related tools
A new unified CLI tool will soon replace tools like nats-pub and nats-sub
✓ Combines all previous tools like pub, sub, req, reply, bench
✓ Full JetStream lifecycle management
✓ Adds a number of tools to discover and query superclusters
✓ Viewer for various NATS events like connection advisories and JetStream events
✓ Built using a new Golang library allowing full programmatic management of JetStream
✓ Currently in Technical Preview @ https://github.com/nats-io/jetstream
59. NATS Connect Live April 16, 2020 6
Management
Cloud Native and Traditional management approaches
✓ Full JetStream lifecycle management using nats CLI
✓ Unattended CLI API for use in CI/CD systems
✓ Configuration file based management using the nats CLI
✓ Terraform provider to manage any JetStream instances @
https://github.com/nats-io/terraform-provider-jetstream/
✓ Built using a new Golang library allowing full programmatic management of JetStream @
https://github.com/nats-io/jsm.go
✓ Administrative events and advisories about internal events
60. NATS Connect Live April 16, 2020 7
JetStream Roadmap
We have big plans for JetStream this year:
✓ Single Server Preview in NATS Server Release 2.2.0
✓ Clustering and GA Q3 2020
✓ Deeper insight into the running system using metrics and advisories
✓ Helper libraries for the main languages
✓ Documentation improvements and migration to https://docs.nats.io
✓ Observability in NATS Surveyor
✓ Integration with 3rd party ecosystem like OpenFaaS, Cloud Events, Fluentd, Telegraf and other Streaming
systems
✓ Custom logic hosted in the server using Web Assembly
61. NATS Connect Live April 16, 2020 8
What about Streaming Server?
NATS Streaming Server is our first generation Streaming solution used very widely in
production environments.
✓ A production level supported platform with 50 million Docker pulls
✓ Continues to be supported for Production use until June 2022
✓ Bug fixes and Security fixes until June 2022
✓ Migration guides or assistance via consultation services
✓ Data migration tool potentially provided
62. NATS Connect Live April 16, 2020
NATS JetStream
Q & A
https://github.com/nats-io/jetstream
#jetstream on Slack
9