# REST API: Node vs Rust

![](https://cdn.hashnode.com/uploads/posts/6aefcf97a529a56101dcc7c8/b6e6b80b-8f6d-4334-ba9c-4e5484adf048.jpg)

With all the hype surrounding [Rust](https://www.rust-lang.org/en-US/), I’ve decided to take a closer look. So I spent about an hour or so looking over the [Rust Docs](https://doc.rust-lang.org/book/syntax-and-semantics.html) 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](http://seanwragg.com/2017/02/06/rest-api-node-vs-rust/%28https://github.com/restify/node-restify%29) framework. To do this, I’ve decided to use [Rocket](https://github.com/SergioBenitez/Rocket) as my Rust web framework. I did notice [Iron](http://ironframework.io/) and several other Rust web frameworks but, I really liked the simplicity of the Rocket interface. I think [nickel](http://nickel.rs/) 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 [Environment](https://rocket.rs/guide/configuration/#environment) section of the docs (thanks [@SergioB](https://github.com/SergioBenitez)!) 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`](http://doc.crates.io/guide.html) feels a lot like a merge between npm and any standard task runner (similar to Grunt or Gulp). Packages are called [crates](https://crates.io/). 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](https://api.rocket.rs/rocket/) 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](https://rocket.rs/guide/requests/#forwarding) 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`

![](https://cdn.hashnode.com/uploads/posts/6aefcf97a529a56101dcc7c8/343c83cf-24cd-418b-9584-f038a9378ec8.png)

Running Rust (Rocket), idle @ < 1MB

![](https://cdn.hashnode.com/uploads/posts/6aefcf97a529a56101dcc7c8/4cb92f2e-f1d8-4642-851b-66880881148b.png)

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](http://httpd.apache.org/docs/current/programs/ab.html) but, found it to produce inconsistent and (oddly) less performant results. I’ve instead switched to [wrk](https://github.com/wg/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!

[Embedded content](https://datawrapper.dwcdn.net/ZrTz7/1/)

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

- Results for [Rust (via Rocket)](https://gist.github.com/sean3z/aa88bce0816878afb651277547b51f58)
- Results for [Node.js (via Restify)](https://gist.github.com/sean3z/9255a421df3389c728d837bd108484a9)

#### Original AB Benchmark Raw Results

- Results for [Rust (via Rocket)](https://gist.github.com/sean3z/e06cfd0ebff0231771e8173f44eb5177) (~9,300 RPS)
- Results for [Node.js (via Restify)](https://gist.github.com/sean3z/b6426ed9dae521c4b56948ce2c6062b1) (~2,800 RPS)

*Originally published at* [*seanwragg.com*](http://seanwragg.com/2017/02/06/rest-api-node-vs-rust/) *on February 7, 2017.*
