
Trying Rust for web services - Keats
https://blog.wearewizards.io/trying-rust-for-web-services
======
steveklabnik
I've historically been very skeptical of Rust in the web application tier, but
I've been playing around with it lately. It's been nicer than I've expected.

One thing this blog post points out is Rust ecosystem docs. I've been trying
to find a good way to evaluate this: [https://users.rust-lang.org/t/lets-talk-
about-ecosystem-docu...](https://users.rust-lang.org/t/lets-talk-about-
ecosystem-documentation/2791)

Other thoughts:

Crates.io, our package server, uses Rust + Ember. One thing that is really
interesting about Rust in this space is memory usage: last I heard, crates.io
uses a flat ~30 MB of RAM, resident. I'm used to Rails where that is... very
different.

On that note, I think "Rust serving JSON + Javascript for the frontend" is a
really interesting stack. I've been trying to find the time (and hopefully
roping another person or two in) to build a
[http://jsonapi.org/](http://jsonapi.org/) library for this purpose. I have a
project where I've been using it, but I went down the "why use a library, I
can generate the JSON by hand" and it's.... ugly. Serde is so much nicer.

As always with a young ecosystem, we need more libraries. For everything. It's
gotten a lot better, and much of the basics are there, but sometimes, you're
still going to have to write things yourself, which slows things down.

mio is gaining preliminary Windows support, and once it does, hyper will roll
it in, which should significantly improve performance in many web contexts, as
well.

I largely come from Ruby, where Rails was such a leap forward that people
learned Ruby just to use it. I expect that if you know Rust, writing
application-tier Rust will be fine, but I'm not sure it will ever see the same
kind of pattern.

~~~
deskamess
Is it possible to do https serving in any of the web frameworks? Last I
checked (months ago) it did not seem possible. I guess a crypto and tls
library would be essential to this. I think https is pretty important feature
to have given http2 and whats coming down the pipe.

~~~
steveklabnik
As far as I know, hyper has HTTPS support, yes. We use an openssl wrapper in
Cargo all the time.

~~~
deskamess
Can you point me to an example? I tried looking at the cargo github repo and
searched for tls but had no luck. Also searched for 443 and found it in
source.rs but it was in a url canonicalization function when calling github.

I am looking for example code where I can pass in the server certificate prior
to listening. Something similar in functionality to Go's
http.ListenAndServeTLS(":443", "cert.pem", "key.pem", nil)

Edit: typo

~~~
steveklabnik
Cargo is a client, not a server, so that's probably where your problem finding
things lies. I don't have an example handy, to be honest. Historically
speaking, when deploying Rails, I would always use Nginx in front of my
application server to terminate SSL anyway...

Also, the Iron docs that another commenter pointed you at looks vaguely like
that call. I'm not familiar with Go's API.

------
dkhenry
I have fully switched over to Rust to do my web backend's. I use Nickel.rs and
it has worked great. The code is very succinct and fast. The only downside I
have found to this is that there is no "Hot Reload" which I had with
Flask(Python) and Play(Scala).

As the ecosystem grows I only expect it to get better.

~~~
Touche
What was behind your decision to pick Rust? My understanding is that Rust is
meant as replacement for C++ in places where C++ performance is needed
(browsers, systems programming (maybe)), etc. A web server does not fall into
that category, so why did you pick it?

~~~
jerf
"A web server does not fall into that category, so why did you pick it?"

That's too categorical a statement. It depends on a lot of things. Sure,
there's a place in the world for PHP pages that take two or three full seconds
to render, but it doesn't matter because they're doing something useful and
don't get hit a thousand times per second. There's also a place for things
that render in under a millisecond because they're getting hit at an
incredible rate, and if they also have some sort of interdependencies then
Rust can make a lot of sense.

Plus as I've said before, the whole "you're stuck in IO anyhow so who cares
how fast the render code runs" dogma has sometimes gone too far. (I know you
didn't say anything about that, but I'm guessing based on experience it's what
lies behind your point.) My experience in switching from slow languages to
fast languages, even doing the same IO, is that you _do_ tend to see real
performance gains unless your page was absolutely trivial. Slow languages
really are slow in practice.

~~~
Touche
> That's too categorical a statement. It depends on a lot of things.

I didn't mean it to be. Of course there are extreme cases where GC pauses are
unacceptable on a web server, but those are relatively rare. Even Google
mostly uses Java (although they are heavy C++ users, is it for their web
servers?).

There are always extreme cases, which is why I asked the question.

> My experience in switching from slow languages to fast languages, even doing
> the same IO, is that you do tend to see real performance gains unless your
> page was absolutely trivial. Slow languages really are slow in practice.

Slow vs. fast is not black and white. Is Go slow? Is Lua slow? I Java (on a
warmed up JVM) slow?

We're talking about GC vs. non-GC languages here. Do you really see a
difference in performance gains?

~~~
acconsta
>Even Google mostly uses Java

I'm not so sure about that. The core services (e.g. Search, Maps, Earth, Big
table, Map reduce) seem to be C++. Ads is possibly an exception.

~~~
Touche
We're talking specifically about the web application servers here. I wouldn't
be surprised that something like Search is C++, it's one of the most visited
pages in the world. But I haven't heard specifically that it is.

I don't think it's any secret that Java is huge Java users. If not web
servers, then for what?

~~~
acconsta
50% C++, 25% Java

[http://qr.ae/RP7wXh](http://qr.ae/RP7wXh)

At Google's scale, the performance difference between Java and C++ can means
millions of dollars in electricity alone.

~~~
Touche
That's the amount of code in their repo which covers a lot more than web
application servers.

This is an unnecessary tangent though, as I've said throughout this
discussions, there's extreme cases and no doubt Google is likely one of them.
But we're talking about common use case, to which I still haven't seen any
evidences that GC pauses are a deliberating factor.

~~~
portmanteaufu
You may already be familiar with this and are specifically concerned with GC.
However, for what it's worth, it's not just the absence of garbage collection
that makes languages like Rust and C faster. A huge piece of their performance
is the level of control that they offer over types' layout in memory and where
things get stored. Rust's preference for stack allocations and the absence of
a mandatory per-Object size overhead (a la Java) really allow it to shine.

------
saosebastiao
Isn't async io pretty much mandatory to compete with the top
platforms/frameworks now? I'm certain rust can compete with C++ on raw compute
speed, but io is still going to bog it down. Are there any rust frameworks
that are using mio yet?

~~~
steveklabnik
The biggest HTTP library, hyper, has a branch, but will not merge it without
mio gaining Windows support. Said support is actively being worked on.

~~~
dbaupp
(NB. mio has windows support in master, but it seems that it hasn't been
published to crates.io yet.)

------
welder
> the server is currently quite slow, only handling around 6.5k req/s for the
> GET handler, removing the database part makes it shoot up to 70k req/s so
> something must be going wrong somewhere around postgres

Dynamic typed language frameworks are also around 70k requests per second[1],
something must be going wrong outside postgres too.

[1]
[https://www.techempower.com/benchmarks/#section=data-r10&hw=...](https://www.techempower.com/benchmarks/#section=data-r10&hw=peak&test=json&l=hds)

------
cdnsteve
I love this type of exploration and discovery. Sometimes it's the best way to
find a diamond in the rough. Who knows? Maybe Rust is the best language for
web services, it just needs a little more exploring. Any feedback in terms of
RDMS that you can use today or NoSQL? How about reddis? Milliseconds and
memory matter when you scale.

~~~
kibwen
Steven Fackler has a fabulous Postgres API at
[https://crates.io/crates/postgres](https://crates.io/crates/postgres) ,
MongoDB has an official (though experimental) driver at
[https://www.mongodb.com/blog/post/the-new-mongodb-rust-
drive...](https://www.mongodb.com/blog/post/the-new-mongodb-rust-driver) , and
Armin Ronacher has Redis bindings at
[https://crates.io/crates/redis](https://crates.io/crates/redis) (which I
can't comment on the quality of, but I assume they're great because Armin (the
author of Flask in Python) is a boss at API design).

EDIT: Agh! Armin isn't the author of Requests. :) I'm mad at myself because I
even went to the Requests home page to attempt to verify that before posting
this comment, and mistook his name under "Testimonials" as an indication of
authorship. :P Sorry Kenneth!

~~~
teisman
Armin isn't the author of requests; that's Kenneth Reitz.

------
matthewmacleod
I expect that Rust will actually be really good for the sort of things that Go
is currently being used for on the web – i.e. services that don't have user
interfaces.

The performance is better and I reckon it's much nicer to write.
Unfortunately, the ecosystem isn't there yet.

~~~
gtaylor
> The performance is better and I reckon it's much nicer to write.
> Unfortunately, the ecosystem isn't there yet.

In my preliminary tinkerings, I'm a bit faster writing Go than Rust. I'm not
super comfortable with either language. Rust's compiler is a lot more picky,
and there's a learning curve to understand how ownership works. Go is a
comparatively simple language (which can be a positive or a negative depending
on your ideals and usage cases).

I think Go will probably always be "faster" to write, but Rust will keep you
from making certain mistakes that you _may_ have sunk debugging time into
later. Hard to say where the time break-even is. I am keeping a close eye on
both camps so that I'm equipped to make the call as soon as some of our
components at work need something faster than Python.

~~~
deckiedan
I think one advantage for me that rust has in this area is that you can very
easily write core/computational stuff in rust and use it from your current
python codebase, and then if/when you need to switch the entire codebase away
from python, you've already got the libs done.

------
dangoor
If the main goal is static typing, it seems like using a static compile-to-JS
language may be better because you can use the whole Node ecosystem. A few
choices for static typing (the first two are JS supersets, the second two are
just different languages):

* Flow from Facebook

* TypeScript

* Elm

* PureScript

~~~
Keats
From my experience (used Typescript for a few projects for frontend and tried
Flow but ran into bugs/missing things) both TS and Flow type system are easy
to fool and in the end you are still writing javascript.

I've never heard of Elm or PureScript being used for backends, users would
prefer Haskell I presume. With elm it would require using ports for almost
everything, I haven't used PureScript so I can't comment on that.

And as far as ecosystems go, I would prefer using the Python one rather than
the Node one.

~~~
smt88
I just decided to use TypeScript for a new REST API I'm writing. I'd have
liked to use Rust instead, but I felt it was still too early (and can't afford
the productivity hits of using a new-ish ecosystem).

So far, TypeScript's type system isn't perfect, but it is strict enough to
help me catch errors before runtime. Also, there's the (huge) added benefit of
making code more readable and self-documenting.

I think I've only had one instance where I violated a type at runtime, but
keeping the type system in mind as you're writing your code, you know exactly
where those things can or can't happen.

I am really enjoying using Visual Studio's excellent IntelliSense and
debugging features, which are on-par with any other web language I've used.
Generally speaking, the typing makes hints and autocompletion _far_ better.

Overall, I'd give the whole experience a 7 out of 10. I don't really know of a
more productive way to write asynchronous code at the moment.

------
munro
Are there any good GUI tools for debugging Rust? Python + PyCharm is amazing,
except now django.setup() takes so long to run in the debugger, I've switched
to print statements.

~~~
steveklabnik
Any visual tool on top of GDB should work. Microsoft's recent open source
efforts mean that Visual Studio debugging is coming along.

