REST API: Node vs Rust
However, I wanted to see how it’d stack up against my current favorite web layer Node.js using the Restify framework. To do this, I’ve decided to use Rocket as my Rust web framework. I did notice Iron and several other Rust web frameworks but, I really liked the simplicity of the Rocket interface. I think nickel would have been my next choice.
Below we have a snippet for Rocket and another for Restify.
Rust (via Rocket)
cargo build --release using
rustup default nightly (as required by Rocket).
rocket_codegen pinned at version
It’s also worth pointing out that Rocket relies on a
ROCKET_ENV environment variable which can be found under the Configuration section of the docs (thanks @SergioB!) and can be set to either
production. It's important to set this to
production otherwise Rocket will run in development mode by default regardless of using the
--release flag in cargo.
rustc 1.17.0-nightly (ea7a6486a 2017-02-04)
cargo-0.18.0-nightly (d710222 2017-02-04)
Node (via Restify)
node src/server.js - nothing really special here.
restify pinned at version
Cargo feels a lot like a merge between npm and any standard task runner (similar to Grunt or Gulp). Packages are called crates. Cargo creates a
Cargo.toml similar to
package.json except in ini format.
There’s a lot of parallels to draw upon right out of the box. It didn’t feel overly complicated like getting Java (via JAX-RS) up and running the first time.
I think this results in a tie in initial setup. Node may have the edge though with the larger community.
I really enjoy the Rocket interface so far. However, I am biased towards the annotation syntax and wish more Node libraries would adopt it (now that we have decorators). I haven’t gone in depth at dealing with CORS, Cookies, etc. with Rocket yet but, I’m hopeful since the docs seem well written.
Perhaps, one of the best features is the static typing as used for route params. If the type doesn’t match, the request is forwarded to the next matching route or a 404 is served.
Now with Restify, we can supply some regex for dynamic route validation but, while convenient, this truly is an eye-sore and can become difficult to maintain. And obviously, this does not enforce type casting upon
req.params (as they're all treated as strings).
I think Rust (via Rocket) has the upper hand here.
For starters, given that Rust is a systems programming language, this should come as no surprise that it idles at a much lower rate than Node. However, I need to investigate better ways to monitor memory locally and look at this a bit more closely under heavy traffic. For now I’m just using
With Restify at 19MB and Rocket just over 1MB, if I were to guess, I’d assume Rust would win here in the long run.
Requests per Second
wrk -d1m http://localhost:8080/
The results however, were staggering to say the least for both test suites!
Restify was able to reply steadily at nearly 8,000 RPS. Rocket however, was able to churn out a fierce 72,000 RPS!!
I think Rust (and Rocket) wins this category
wrk Benchmark Raw Results
Original AB Benchmark Raw Results
Originally published at seanwragg.com on February 7, 2017.