Writing a proxy in Rust and why it is the language of the future
It’s been a year since I wrote a little proxy with Rust, it is one of my first projects with this language and I learned a lot writing it. To sum up a bit, the main goal of this proxy is to be really simple to use and easy to extend with middlewares. It targets HTTP APIs and can be used in front of a lot of services. This proxy has been used in multiple projects, with microservices architecture and so on…
Why this proxy ?
As said earlier, we are working on a micro-services architecture. The first thing I needed was an authentication mechanism, we chose to use JWTs and added a service to generate and sign those. As I wanted to avoid asking this service if the provided token had the right signature for each request, I had to find another solution, what about a proxy ? It would also allow us to route our requests to the right services, log the requests, add CORS and so on…
The real deal here is that I can forget it once set up, it is not coupled with other services and is really light.
It is based on hyper, a solid and async HTTP library. I know my implementation may not be the most efficient, but it works, it is light, easy to use and extend, and my goal was not to write the perfect piece of software but something fitting my evolving needs. It has not the pretension of being a real fatass proxy like sozu.
How to use it:
I left some middlewares included in rs-simple-proxy like Logger or Router. Here, Auth middleware is a custom one.
What a rs-simple-proxy middleware looks like ?
Didn’t ask but got a good performance
The thing is, I never focused on the performance writing it, at first it was a test project to understand and see how we could leverage the awesomeness of Rust in our stack. It wasn’t meant to handle thousands of requests per sec. Guess what ? Side effect of Rust, it does.
When we benchmarked the part where rs-simple-proxy was used in production, we were instantly surprised by the results, when you come from the Ruby world, you know how to handle performance issues, cause you know… you’ll have some, you have to do some caching stuff, add workers so you can spin up more instances and such things.
This little project convinced me to go all-in Rust. I would not write a web service in Ruby anymore.
Rust is the future
Rust allowed me to build anything with approximately 0 worries.
- It is not painful to write since there is no boilerplate depending on what you build, for example, rocket allows you to write web apps/apis in an awesome and fast way. see https://rocket.rs/.
- The code is robust, even when you are not so experienced with the language, you may have a bad time at first, but you‘ll end up writing robust code.
- The development tools available are already powerful: VSCode integration (with rust-analyzer or RLS), Cargo, Rustfmt, Clippy and so on…
- The community is incredible and welcoming, it is a FOSS-oriented community which does not disappoint when it comes to going forward.
And now what can’t I do with Rust ? Tell me. Since this proxy, almost all of my projects have been written in Rust, and although it is not really straight forward to write front apps with Rust, it is possible and will become more and more straightforward with the growing ecosystem of WASM (WebAssembly).
A big thanks to the Rust community.