21 ms·
Trying Rust for web services
- dkhenry 11y agoI 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.
- zatkin 11y agoNickle.rs is at http://nickel.rs/ http://nickel.rs/
- shepmaster 11y agoIt's also spelled "Nickel.rs", as shown in the link ^_^
- zatkin 11y agoOops, my mistake.
- vkjv 11y agoWhat are the compile times like? Is a simple watch script with a re-compile sufficient?
- steveklabnik 11y agoNot the parent, but they've gotten faster every release, so if you've tried Rust in the past, your previous experience would be wrong. We're in the middle of a big refactoring of the compiler that will eventually enable even more gains. I'm confident rustc will only get faster in the future.
- kibwen 11y agoYou can't go evangelizing about compile times without even mentioning incremental recompilation. :P https://github.com/rust-lang/rfcs/pull/1298 https://github.com/rust-lang/rfcs/pull/1298
- untothebreach 11y agoUnless you are on windows, you can try cargo-watch[1] to re-compile and run your code when the source changes. 1: https://github.com/passcod/cargo-watch https://github.com/passcod/cargo-watch
- dkhenry 11y agoI will have to give that a try. Its amazing how much hot reloading speeds up development.
- mixmastamyk 11y agosudo apt-get install inotify-tools and a short shell script.
- Keats 11y agoAh that's interesting! Can you talk a bit more about the kind of project you're doing ?
- dkhenry 11y agoMy current project is an interface to get detailed application performance statistics off a clustered system. So it connects to a local database and has a configurable list of system checks that it can run. Then it has a defined API that is consumed by the Polymer front end. Rust is nice in that I can do all sorts of great clustering and network programming, and then have a thread that exposes the WebUI and all of that is wrapped up in a single binary. That I can just push to systems and execute.
- Keats 11y agoIt would definitely be interesting if you have time to push a sample project somewhere to see how it looks when it's someone that knows what they are doing.
- Touche 11y agoWhat 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 11y ago"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.
- jurre 11y ago> PHP pages that take two or three full seconds to render I don't use php personally, but most of my rails apps render full pages in under 100ms. I'd imagine php performance to be at least on-par with a full rails app.
- jerf 11y agoI'm thinking Wordpress, or bespoke status pages for internal apps. Of course you can get a PHP page out in under a millisecond if you try hard enough, but it's often not used in that sort of place. (And it often is, but that still doesn't mean it often isn't.) Rails, for that matter, is known for being easy to produce multi-second pages too. Slow languages are slow in practice, too.
- the_mitsuhiko 11y agoI have some ideas for making hot reload work for web dev i just did not have the time yet :(
- markus2012 11y agoI have also switched to Rust for backend work. I'm using Iron instead of Nickel. Not sure who will become more popular over time... Because of the language (no null, bounds checking, option/result types, etc.) and borrow checker I find I rarely have to 'hot reload' or even test my code. I realize that sounds surprising. I'm not doing any HTML templating in Rust - that's better done in JavaScript on the client - so I don't have the edit/compile/test cycle for every tweak to the UI.
- dangoor 11y agoIf 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 11y agoFrom 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.
- purescript 11y agoThere are some PureScript projects that I know of running on Node, although it is admittedly less common. I'm currently working on a framework to build REST services in PureScript, so hopefully the situation will improve shortly.
- deleted 11y ago[deleted]
- smt88 11y agoI 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.
- matthewmacleod 11y agoI 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 11y ago> 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 11y agoI 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.
- steveklabnik 11y agoI'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-documentation/2791 https://users.rust-lang.org/t/lets-talk-about-ecosystem-docu... 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.
- Keats 11y agoWhat's the deal with Serde / rustc_serialize ? I've used serde mainly because I've seen mentioned a few times and saw a thread somewhere saying it was the successor of rustc_serialize.
- steveklabnik 11y agorustc_serialize was built for the compiler itself, and not really well-designed to be an external library. But it was Good Enough for a long time. Serde is the next-gen, built from the ground up to be awesome thing.
- saosebastiao 11y agoIsn'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 11y agoThe 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 11y ago(NB. mio has windows support in master, but it seems that it hasn't been published to crates.io yet.)
- acconsta 11y agoA Node-style event loop leads to callback hell. It would be awesome to have something like Boost Asio.
- spion 11y agoNot anymore - with V4 generator based "coroutines" are available (enabled by default).
- cdnsteve 11y agoI 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.
- steveklabnik 11y agoPostgres and Redis are probably the best two supported datastores. Oh, and Mongo paid some interns this summer to write a new driver as well.
- kibwen 11y agoSteven 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-driver https://www.mongodb.com/blog/post/the-new-mongodb-rust-drive... , 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 11y agoArmin isn't the author of requests; that's Kenneth Reitz.
- jnbiche 11y agoMinor correction: Kenneth Reitz is the author of Requests.
- avinassh 11y ago> but I assume they're great because Armin (the author of Flask and Requests in Python) minor correction, Requests is written by Kenneth Reitz
- munro 11y agoAre 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 11y agoAny visual tool on top of GDB should work. Microsoft's recent open source efforts mean that Visual Studio debugging is coming along.
- kinghajj 11y agoI've used Eclipse with the RustDT plugin to debug a project successfully. There's probably ways to make it work in Sublime/Vim/et. al, I just haven't put in the effort to figure it all out yet.
- welder 11y ago> 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=peak&test=json&l=hds https://www.techempower.com/benchmarks/#section=data-r10&hw=...