Blog post

Hyper vs Rocket - Low Level vs Batteries included

2022-05-09T15:00:00

12 minute read

In this post we're going to be comparing two popular Rust libraries used for building web applications. We'll be writing an example in each and compare their ergonomics and how they perform.

The first library Hyper is a low level HTTP library which contains the primitives for building server applications. The second library Rocket comes with more "batteries included" and provides a more declarative approach to building web applications.

The Demo

We're going to build a simple site to showcase how each libraries implements:

Routing

Routing decides what to respond for a given URL. Some paths are fixed, in our example we will have a fixed route / which returns Hello World. Some paths are dynamic and can have parameters. In the example we will have /hello/*name* which will response Hello *name* which will have name substituted in each response.

Shared state

We want to have a central state for the application.

In this demo we will have central site visitor counter which counts the number of requests. This number can be viewed as JSON on the /counter.json route. In this example we will be storing the counter in application memory. However if you were storing it in a database the shared state would be a database client.

The are lots of other functionality necessary for a site such as handling HTTP methods, receiving data, rendering templates and error handling. But for the scope of this post and example we will only be comparing these two features.

The rules

The rules for this demonstration is to only use the specific library and any of its re-exported dependencies. So no additional libraries (except in the hyper example we need a tokio::main).

Hyper

Hyper's readme describes hyper as a "A fast and correct HTTP implementation for Rust with client and server APIs". For this demo we will be using the server side of the library. It has 9.7k stars on GitHub and 48M crates downloads. It is used as a often a dependency and many other libraries such as reqwest and tonic build on top of it.

In this example we see how far we can get with just using the library. This demo uses Hyper 0.141. Below is the full code for the site:

1use hyper::server::conn::AddrStream;
2use hyper::service::{make_service_fn, service_fn};
3use hyper::{Body, Request, Response, Server};
4use std::convert::Infallible;
5use std::sync::{atomic::AtomicUsize, Arc};
6
7#[derive(Clone)]
8struct AppContext {
9    pub counter: Arc<AtomicUsize>,
10}
11
12async fn handle(context: AppContext, req: Request<Body>) -> Result<Response<Body>, Infallible> {
13    // Increment the visit count
14    let new_count = context
15        .counter
16        .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
17
18    if req.method().as_str() != "GET" {
19        return Ok(Response::builder().status(406).body(Body::empty()).unwrap());
20    }
21
22    let path = req.uri().path();
23    let response = if path == "/" {
24        Response::new(Body::from("Hello World"))
25    } else if path == "/counter.json" {
26        let data = format!("{{\"counter\":{}}}", new_count);
27        Response::builder()
28            .header("Content-Type", "application/json")
29            .body(Body::from(data))
30	        .unwrap()
31    } else if let Some(name) = path.strip_prefix("/hello/") {
32        Response::new(Body::from(format!("Hello, {}!", name)))
33    } else {
34        Response::builder().status(404).body(Body::empty()).unwrap()
35    };
36    Ok(response)
37}
38
39#[tokio::main]
40async fn main() {
41    let context = AppContext {
42        counter: Arc::new(AtomicUsize::new(0)),
43    };
44
45    let make_service = make_service_fn(move |_conn: &AddrStream| {
46        let context = context.clone();
47        let service = service_fn(move |req| handle(context.clone(), req));
48        async move { Ok::<_, Infallible>(service) }
49    });
50
51    let server = Server::bind(&"127.0.0.1:3000".parse().unwrap())
52        .serve(make_service)
53        .await;
54
55    if let Err(e) = server {
56        eprintln!("server error: {}", e);
57    }
58}
59

At the top we define a handle function which processes all the requests.

Routing is done through the chain of ifs and elses in the handle function. First the path of the request (e.g / for the index) is extracted using req.uri().path(). Fixed routes are easy to branch on using string comparison like path == "/". For routes which match multiple paths such as the /hello/<user> route it uses str::strip_prefix which returns a None if the path doesn't start with the prefix or Some if the path starts with the prefix along with a slice that proceeds the prefix.

1"/".strip_prefix("/hello/") == None
2"/test".strip_prefix("/hello/") == None
3"/hello/jack".strip_prefix("/hello/") == Some("jack")
4

The function has a early return for requests with a method other than GET because there are no POST routes or others for this example. If the site accepted different requests types and had to add additional guards then we could additional clauses to the if statement. Although you could see how expand on the if chain would get more complex and verbose.

To return a response, Hyper re-exports Response (from the http crate). It has a nice simple builder pattern for building the responses. The serialization code is hand written using format!. Of course we could import serde but that's against the rules.

The counter is done by creating a struct in the initializing code and cloning it on every request to send to the handler function. Without going into the details it uses Arc<AtomicUsize> instead of a usize as the atomic variant has special properties for when multiple handlers are using and mutating it. The code increments the visitor counter before anything else in the handler function so that a visit is recorded for all requests.

Hyper Verdict

In terms of development (on a low end machine we used for profiling2), a debug build (without any of the build artifacts) takes 79.0s. After the initial compilation, incremental compilation takes only 1.9s. For building a release build with further optimizations (on top of the debug build artifacts) it takes 32.5s.

The initialization code was take from Hyper's server docs and is quite verbose and out of the box for Hyper there are no logs or server information.

In terms of runtime performance over three 30 second connections Hyper responded to on average 74,563 requests per second on the index route on the above code. Which is incredible quick!

Rocket

Rocket is a "web framework for Rust with a focus on ease-of-use, expressibility, and speed". It has 17.4k github stars and 1.7M crates downloads. Rocket internally uses Hyper.

For this demo we are using the 0.5.0-rc2 version of Rocket3 which builds on Rust stable.

1use rocket::{
2    fairing::{Fairing, Info, Kind},
3    get, launch, routes,
4    serde::{json::Json, Serialize},
5    Config, Data, Request, State,
6};
7use std::sync::atomic::AtomicUsize;
8
9#[derive(Serialize, Default)]
10#[serde(crate = "rocket::serde")]
11struct AppContext {
12    pub counter: AtomicUsize,
13}
14
15#[launch]
16fn rocket() -> _ {
17    let config = Config {
18        port: 3000,
19        ..Config::debug_default()
20    };
21
22    rocket::custom(&config)
23        .attach(CounterFairing)
24        .manage(AppContext::default())
25        .mount("/", routes![hello1, hello2, counter])
26}
27
28struct CounterFairing;
29
30#[rocket::async_trait]
31impl Fairing for CounterFairing {
32    fn info(&self) -> Info {
33        Info {
34            name: "Request Counter",
35            kind: Kind::Request,
36        }
37    }
38
39    async fn on_request(&self, request: &mut Request<'_>, _: &mut Data<'_>) {
40        request
41            .rocket()
42            .state::<AppContext>()
43            .unwrap()
44            .counter
45            .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
46    }
47}
48
49#[get("/")]
50fn hello1() -> &'static str {
51    "Hello World"
52}
53
54#[get("/hello/<name>")]
55fn hello2(name: &str) -> String {
56    format!("Hello, {}!", name)
57}
58
59#[get("/counter.json")]
60fn counter(state: &State<AppContext>) -> Json<&AppContext> {
61    Json(state.inner())
62}
63

In Rocket we describe each endpoint using a function. The get macro attribute handles path routing and http method constraint. No need to add early returns for methods and dealing with raw string slices. It takes the declarative approach, #[get("/hello/<name>")] is more descriptive and less verbose than if let Some(name) = path.strip_prefix("/hello/"). The functions are registered using .mount("/", routes![hello1, hello2, counter]).

The application has a state defined here:

1#[derive(Serialize, Default)]
2#[serde(crate = "rocket::serde")]
3struct AppContext {
4    pub counter: AtomicUsize,
5}
6

And it is created and registered using .manage(AppContext::default()). Rocket re-exports the serialization library serde so we can use #[derive(Serialize)] to generate serialization logic for the counter state, so no hand writing the serialization code unlike first method.

In Rocket endpoint functions can just return String or str slices and Rocket handles it automatically. Rocket also comes with a Json return type and reusing the fact that AppContext implements Serialize we can freely build a Json response from it. The Json structure handles setting the Content-Type header automatically for us.

Rocket has a middleware implementation which it calls "fairings". In the example it defines a CounterFairing which on every request modifies the counter state. The initialization code is really slim, it sets up a config and a Rocket structure is created using a builder pattern. Annotating the main function with #[launch] helps Rocket find the entry point and abstracts how the server is span up. Rocket also has really nice built in logs which are great for development.

Rocket Verdict

Since Rocket has more dependencies and requires more macro expansion it takes a bit longer build taking 141.9s (2m 21.9s) on a cold start to compile. A release builds on top of debug artefact takes 147.0s (2m 27.0s) to compile. Incremental builds are still fast taking 3.3s to compile after a small change to the response of a endpoint.

Using the same benchmark as Hyper, on average Rocket returned 43,899 requests per second in a release build with the logging disabled - roughly 60% of Hyper's throughput.

Conclusion

Writing both of theses examples were fun to build and there weren't any frustrations or problems using them. Both are plenty fast for performance to be a concern.

Rockets documentation is very good and explanatory. All of Hyper's api is well documented on its docs.rs page. Both libraries are actively developed with many commits and pull requests made in the last month.

Do you prefer the control and speed of Hyper or prefer the expressiveness of Rocket?

Shuttle: Stateful Serverless for Rust

Deploying and managing your Rust web apps can be an expensive, anxious and time consuming process.

If you want a batteries included and ops-free experience, try out Shuttle.


Footnotes

  1. The dependencies for building the project hyper = { version = "0.14", features = ["server", "tcp", "http1"] } and tokio = { version = "1.18.2", features = ["rt", "macros", "rt-multi-thread"] }

  2. The build and request profile machine is a vm with 2 cores and 7 GB RAM. Take the numbers with a grain of salt

  3. The dependencies for building the project rocket = { version = "0.5.0-rc.2", features = ["json"] }

Share this article

Let's make Rust the next language of cloud-native

We love you Go, but Rust is just better.