REST API: Node vs Rust

With all the hype surrounding Rust, I’ve decided to take a closer look. So I spent about an hour or so looking over the Rust Docs and admittingly, it looks like a really fun language.

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)

Compiled via cargo build --release using rustup default nightly (as required by Rocket). rocket and rocket_codegen pinned at version 0.2.0.

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 development, staging or 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)

Run via node src/server.js - nothing really special here. restify pinned at version 4.0.3

node v6.9.5
npm 3.10.30

Developer Environment

Coming from a primarily Javascript heavy ecosystem (npm), I felt right at home with Rust. 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.

After painlessly installing the necessary components, I was able to jump right into code without issue. This is one of my favorite things about Javascript — so I’m happy this type of quick engagement has been replicated.

I think this results in a tie in initial setup. Node may have the edge though with the larger community.

Route Interface

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.

Memory Consumption

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 top -pid

Running Rust (Rocket), idle @ < 1MB
Running Node (restify), idle @ 19MB

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

For this, I used originally used ApacheBench but, found it to produce inconsistent and (oddly) less performant results. I’ve instead switched to wrk which seems to perform more reliably.

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 on February 7, 2017.

Show your support

Clapping shows how much you appreciated Sean Wragg’s story.