Mawoka's Blog - Actix/Rust for FastAPI/Python Devs

Actix/Rust for FastAPI/Python Devs

05 June 2022

Introduction

First, I have used FastAPI/Python for ~2 years now and I am quite happy. Then, I heard many great things about Rust so I tried it. I built PersonalBlogEngine with Rust. All in all, it is pretty simple. What I needed was a database, a search-thing, a frontend and a web-server. Here, I'll list my standard Python- and Rust,libraries I would use for this job:

Python

  • FastAPI (Web-Framework)
  • Ormar (ORM based on Pydantic)
  • Search ?

Rust

  • Actix (Web-Framework)
  • Diesel.rs (ORM)
  • Milli (The same library Meiliseach uses)

With all the tools listed, let's compare the developer-experience, efficency and more.

What the Rust-Stack does better than the Python-Stack

There are many things the Rust-stuff does better than the Python-stuff, for example resource usage (eg. FastAPI uses ~150mb in idle, Actix ~20mb, so Actix uses 7 times less ram than FastAPI in idle) and stability and reliability. What do I mean by stability and reliability? Since Rust uses a compiler, nearly every bug that is in the code and doesn't come from logic-errors will be noticed, otherwise the compiler will complain. In Python, in comparison, many bugs will just sneak into your final product and the errors will just pop up if the software is deployed in production. The compiler gives you also one binary, not like Python where you need many script-files with your code, dependencies and the Python-interpreter itself. In Rust, I can also embed my frontend into the code easily. Lets quickly compare the steps to set a Rust/Python program up:

Rust

  • Download the file (wget https://sth.test/<file>)
  • Make it executable (chmod +x <file>)
  • Run it (./<file>)

Python

  • Install Python (sudo apt install python3)
  • (Probably) install something like Poetry or Pipenv (pip3 install poetry/pipenv)
  • Download the source-code (git clone https://github.com/mawoka-myblock/<project>)
  • Install the dependencies (pipenv install)
  • Run the application

As you can see, the end-user would definetely prefer the first method (the rusty-one)


All in all, the end-user would get an application, which is easier to install, faster and more efficient.

What the Python-Stack does better than the Rust-Stack

There is one huge aspect: speed of development, because many things are easier and simpler than in Rust. Of course, with the time you'll get to know Rust quite well, but at the beginning, you're pretty slow. Then, there is the ORM-situation. Ormar is just great. It makes declaring your models easy and quick. In Rust, you have to write your own migrations in SQL, maintain objects which have the same properties as your SQL-schema has and more. This adds an extra layer of complexity. Then, in Rust, you also have to care about memory-management. Not like C(++) but you still have to do it (kinda), but the compiler helps you (most) of the time. There's also auto-reloading in both stacks, but for Rust a reload takes between 2 and 8 seconds, with Python at most 1 second. This can slow you down dramatically. FastAPI's main-feature is also the automatic documentation via Swagger/OpenAPI, which Actix sadly can't provide.

Conclusion

Should you start with Rust and Actix if you already use FastAPI and you're happy?

I'd say yes, just try it. More efficient software is also better for the climate and the consumer.

Sould you start with FastAPI and Python if you're a Rustacean?

No, unless development-speed is more important to you than stability/efficency is.

Should you start with Rust or Python if you're new to web-APIs?

Start with FastAPI. The docs of FastAPI are also great, in particular, so you can learn what all even belongs to web-development.


I'd definetely use more Rust, but Rust won't replace FastAPI/Python completely.

1 1