What is the Web Server for Rocket app

Hell Rust lovers,

I am very new to Rust but amazed with it's popularity and cult following so decided to create my new app in Rust , I am using Rocket framework because my new app will be based on APIs.
I am wondering which web server is best for Rocket app, on the internet i am not finding any answer, like Nginx is one of the best server for node js apps what is the best server for Rocket apps, or Rocket itself a server, if it is , then can it handle large number of requests and responses.

please help me in this guys.
thanks in advance.

Rocket is a web server framework.

1 Like

thanks for the reply @moy2010
can i develop api based app in Rocket or do i need to use other framework for my requirement
I need apis which will connect to mysql and then fetch data and then transfer to mobile app in json format.

If you are going to use Rocket you don't need a web server. Rocket is a frame work for easily creating web servers in Rust. I have been using it for a couple of years now with great success. Creating those API's you want is easy in Rocket.

By way of a kickstart in the Rocket guide you will find an example of a simple web server that looks like this:

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;

#[get("/")]
fn index() -> &'static str {
    "Hello, world!"
}

fn main() {
    rocket::ignite().mount("/", routes![index]).launch();
}

See here: Getting Started - Rocket Programming Guide. Build and run that and you have web server serving up "Hello, world" on the "/" path. Easy.

Having created may web servers in node.js I have to say that statement confuses me. node.s makes an excellent web server by itself. Using the node Express.s module makes it even easier. https://expressjs.com/. Again no nginx or Apache etc is required to create a web server in Node.js. Indeed that is one of the selling points of node.js.

You might have to detail what you mean by "large number"?

A search for "rust web framework benchmarks" will reveal a lot of comparisons. I suspect it all rather depends on what your web server is expected to do.

I would not call it "cult following". No more or less than users or evangelists of other languages. Rust is a very good language that provides features that no other language does. At least no commonly used ones or any I have heard of. Features that are great if you are concerned with code quality. As all software writers should be.

Having said all that, it is common for people to run web servers, created in Rust, Node or whatever else to use them in conjunction with nginx or other front end. Perhaps to take care of security or load balancing or just as a way to provide a single point of entry for multiple back-end services that run on other web servers.

Yes That's exactly what Rocket is for.

You can do it all in Rocket. (Unless you need web sockets I guess). I suggest you spend an hour reading the Rust docs Programming Guide - Rocket Web Framework to get a feel for all this.

No problem. Pretty much what I do with Rocket except we use Postgres.

Hi @ZiCog thanks for your reply, i got it, express is server itself similarly Rocket is also web server in itself.
Nginx is reverse proxy and load balancing is easier for those severs.
so my main reason of choosing Rust is it's performance as it is a compiled language, i am expecting it to be fast and to server more requests as compared to node js so that i can achive some cost effectiveness and appreciation from users because of its performance.
I will go through links you gave me looks like i am on right path, will understand load balancing in rocket and other important things.

now i understood your point @moy2010

I haven’t used rocket but looks like there docs explain what it can do pretty well and their repo has quite a lot of examples including connecting to databases. The database examples are SQLite but other than the rusqlite file each one can connect to more than SQLite (MySQL).

In many cases for api servers, including with node, nginx is just used as a reverse proxy to help with https/routing to your service. So whether you use nginx or not is up to how you deploy.

Hi @ijsnow , thanks for your suggestion, will check Rust's repo to see examples regarding mysql connection with Rust.

I'm sure you will find it faster. But it does very much depend on what your server is going to be doing.

For example here is a simple benchmark that shows Rust/Rocket to be massively faster than Node/Express: Node Express vs. Rust Rocket. A speed comparison | by Daniel Segovia | Better Programming. But note that the benchmark is mostly computation heavy, calculating fibonacci numbers as it does.

You will not see such huge gains if your server is simply making requests to a database and returning responses where a lot of the work is done in your database. But still it should be good.

Your users will appreciate the reliability, high availability, of a server in Rust. The languages type checking and data lifetime checking greatly reduce the number of silly mistakes that slip through and cause one to get support calls in the middle of the night when the thing goes down or produces nonsense results in some odd circumstances that are heard to debug.

Great. Good luck with that. Be prepared to find it a bit harder to create what you want in Rust/Rocket than node/express. Remember the effort you put in will be rewarded with better performance and reliability.

@ZiCog completely understood now, as you said will get at least little something more than node js, though will not get huge good impact of performance now still will be happy and thanks for wishing me luck, appreciate your efforts to guide me , complete satisfied with your guidance, as you said rust server is highly available and in rust i will be able to avoid silly mistakes and other gains you said are enough for me , very happy and satisfied thanks again.

I suspect that if you have a large number of users and hence a large number of connections in flight at the same time you should see significant performance increase.

From version 5 Rocket is handling requests asynchronously rather like a web server in node.js does. However Rocket can make use of all the cores on your machine where as Node.s is single threaded.

Please do come back and let us know how it goes and what performance you see in your real use case.

Sure @ZiCog will share my experience after deployment, as you said it can make use of all core of computer then it may be more fast.
One more thing i feel that Rocket will not have a interpretation layer of node js so it will save that interpretation time hense performance may be good ( this is what i feel but as you guys have more experience you suggest wherever v8 stores compiled code as buffer for frequent code then my feeling is wrong).
please share your suggestion thanks @ZiCog

Yes, node.js interprets the Javascript source code and will do some kind of "Just In Time" compilation of it to actual machine code as it runs. That makes performance far better than a non-JIT interpreter in many cases. But still, I would expect Rust to do much better. At least that is my experience of comparing node.js with C++ for compute intensive tasks.

ok great, i am excited to see how Rust do it, this is my first compiled language which i will be using for web development thanks again @ZiCog will be back soon

In my opinion, it's still good hygiene to run nginx in front of your Rust server.
If you didn't have already have experience setting nginx up, I would recommend serving static files from Rocket to keep it simple. (you don't need nginx to run node.js apps either, it's just good practice)

Could you expand on "Good hygiene"?

I use nginx in front of web services in Rust, node.js and dear I mention even Python. For two reasons,

  1. Something has to forward requests to multiple appropriate services.

  2. It takes care of HTTPS certs for everyone.

Oddly I we do things the other way around. Nginx serves up static files. Rust and node.s (mostly) do only active stuff.

Sorry, I mean clean code.
You're separating the responsibilities of "responding to requests for static unchanging files" and "responding to dynamic HTTP requests".
nginx is very mature and IMO it's more maintainable if you offload responsibility to an application that can "just handle it" (e.g. nginx makes HTTPS and static file serving very easy) rather than writing code to do it (even if it's a library like Rocket).

Yeah sorry, this is what I meant to say is best practice.

There are also many other benefits that come from using nginx like you mentioned. There's not really another easy way to have two dynamic webservers (e.g. node.js and Python) listen on the same port.

Ah, we are on the same page then. Good to know I'm not totally off the rails. (Well, I have never used Rails :slight_smile: )

1 Like