Web API Benchmarking: Python (FastAPI) vs Rust (Axum) with Database (Dockerized)
When it comes to building web APIs, developers have a plethora of tools and languages to choose from. In this article, we will compare the performance of two popular web frameworks – Python’s FastAPI and Rust’s Axum, with a database connection running in a Docker container.
Python (FastAPI)
FastAPI is a modern web framework for building APIs with Python. It is known for its speed and efficiency, thanks to its use of asynchronous programming. FastAPI is built on top of Pydantic and Starlette, providing a robust and scalable solution for building APIs.
Setting Up FastAPI
To run FastAPI with a database in a Docker container, you will need to install FastAPI and Docker on your system. Follow the steps below to set up FastAPI:
- Install FastAPI using pip:
pip install fastapi
- Create a FastAPI app and define your API endpoints
- Set up a database connection using an ORM like SQLAlchemy
- Create a Docker container for your database using Docker Compose
Rust (Axum)
Axum is a web framework for Rust that focuses on performance and safety. It is built on top of the Tokio runtime, providing fast and reliable asynchronous handling of web requests. Axum is designed to take advantage of Rust’s ownership system to ensure memory safety and prevent common runtime errors.
Setting Up Axum
To run Axum with a database in a Docker container, you will need to install Rust and Docker on your system. Follow the steps below to set up Axum:
- Install Axum using Cargo:
cargo install axum
- Create an Axum app and define your API endpoints
- Set up a database connection using a Rust ORM like Diesel
- Create a Docker container for your database using Docker Compose
Benchmarking
Once you have set up FastAPI and Axum with a database running in a Docker container, you can now benchmark the performance of both frameworks. Use tools like Apache Bench or wrk to send a large number of requests to your API endpoints and measure the response time and throughput.
Compare the results from both FastAPI and Axum to see which framework performs better under heavy load and how they handle database queries. Consider factors like response time, throughput, and error rate when evaluating the performance of each framework.
Conclusion
Web API benchmarking is an essential step in evaluating the performance of web frameworks and ensuring that your application can handle the expected load. By comparing the performance of Python’s FastAPI and Rust’s Axum with a database connection running in a Docker container, you can make an informed decision about which framework is best suited for your project.
haha suck python
I tried increasing Postgres' max_connections and FastAPI's results improved a lot.
database:
command: postgres -c 'max_connections=10000'
When you're using `oha`, you have `–latency-correction`, but that flag is ignored unless you set `-q {queries per second}`. Consequently, I suspect this benchmark suffers from the coordinated omission problem. What do you think?
I already expected the outcome of this even before watching the video, since compiled language will always be better than interpreted language, and Rust is one of the fastest compiled lang, while Python is probably the worst and slowest interpreted lang,
But it still shocking to see this bloodbath 🤣🤣 I'm thinking PHP 8.2 even without swoole could still massacre python,
Now Im curious to see a benchmark between PHP (with swoole) vs NodeJS
Please try 1 more change – altogether disable tracing on both the Rust and Python sides and re-run the tests. Logging is a potential bottleneck, especially on the Python side. Also from the logs, I see that the Postgres client is hitting limits not the fast API itself, maybe tuning that bit can potentially reduce the difference of results
Is there any plan to compare Quarkus Native and Rust? (Create and Read)
The memory usage disparity between Axum and fastapi is crazy. 27MB to 1.2GB. I chose rust for efficiency.
I thought that the rust version would have at least as many, or even more, failed [500] requests as fastapi but it has none! Does anyone know how these failed request happens for fastapi? Timeouts or problem handling 16 workers?
Really nice benchmark otherwise as always 🙂
But python is logging a lot of data. Isn't that slow down the python project. Or are you logging the same with rust but just not showing it?
Btw, I think python is a woman language 😁
Wow, this benchmark is fantastic it is the first time I have seen this comparison using a database Rust is really a game changer for the future of development. Thanks for taking the time to do it.
TL;DW: Rust ran circles around Python, as expected. (20x more requests handled in the 30 second test run)