Cover image created by me using Ferris the Crab, the Rust logo, and the FastAPI logo.
What is this Series?
I am going to produce a series of blog posts attempting to create a method for replacing FastAPI applications with Rust for my particular use case. I don't know what this will lead to just yet; it may end up being a tutorial, it may end up being a development log as I create the pieces of the ecosystem that are missing, it may end up being only one more post if I find something that magically solves all my issues.
Check out the requirements section below for exactly what I'm trying to achieve!
Some Background
I have been using FastAPI as my go-to back end framework for a little over a year now and it's almost perfect. In my opinion, FastAPI is the best way to create an API in any language. I regularly look around the ecosystem for
competitors worth trying and haven't found anything compelling enough to try... yet.
For those of you not acquainted, FastAPI is a Python web framework for creating OpenAPI documented REST APIs. You can do a lot more with it too, but that's my primary use case, and the one that matters to this blog series. The key features (to me) are:
- Automatic documentation via OpenAPI, which lets you do things like generate Python code that knows how to talk to your API.
- A documentation UI allowing users to easily understand and interact with the API directly.
- Input validation via Pydantic, the same tool used to generate the schemas in documentation. Basically you just annotate your endpoint with inputs/outputs and they are documented and validated.
- Easy to test using something like pytest due to some fantastic included tools and the flexibility of dependency injection.
- Fantastic documentation of FastAPI itself, some of the best I've ever read, without which it might as well not have all of these amazing features.
- Easy to host on AWS Lambda using Mangum.
FastAPI does much, much more and has one of the most amazing open source communities I've ever encountered. So why am I looking to replace it? FastAPI has one fundamental drawback that it will never overcome: Python.
Don't get me wrong, I love Python, it's my second favorite language, but it has some serious limitations that I would love to move beyond.
Why Rust?
I know, I know, Rust users never shut up about how great the language is, so I'll try to keep this brief and on topic. Let me know in GitHub if you want a blog post going in depth on any of these points:
- Python's static typing story is a sad one. It might get better, but it's not good enough right now, and will never be as good as Rust's.
- Rust is way faster than Python which matters when you're billed by execution time (like on AWS Lambda).
- Rust error handling is undeniably better than exception-based errors (like in Python) when stability matters.
- Algebraic data types in Rust allow you to encode business and security logic in a way that your code will not compile if you get it wrong. This is far better than forcing internal server errors when when someone forgets to secure an endpoint like I do in FastAPI.
Requirements
The end result of my investigation should be a method to produce an API which:
- MUST be written in Rust.
- MUST automatically produce an OpenAPI v3 document from the Rust code and comments.
- The developer may need to know some OpenAPI concepts for properly documenting things, but should not have to write JSON or YAML to achieve it.
- The document MUST be hosted on an endpoint of the API so it's accessible from the internet.
- MUST be easily deployable on AWS Lambda using some infrastructure as code tool (SAM, Serverless, etc.).
- MUST perform at least as fast as an equivalent FastAPI application for common CRUD tasks.
- MUST interact with a relational database (MySQL or Postgres).
- MUST have a simple way to test endpoints, comparable to pytest with FastAPI. If a lot of work has to go into testing, people just won't test things.
- MUST have great documentation.
- SHOULD have automatically hosted documentation which allows direct interaction with the API. If not, this could be achieved by hosting the documentation externally.
Conclusion
So I'm going on an adventure to replace what I believe to be the best API framework that exists with something else. There will be research, documentation, writing of Rust code, and who knows what else! In the end, I'll either come away with the new best way to produce APIs, or even more evidence of how amazing FastAPI is. Sound like fun? Follow along with this series! Have some suggestions of where I should start? Please, please let me know.
Want to be notified when the next part of this series is released? Watch releases in the GitHub repo.
Top comments (4)
What a teaser?!
As a matter of fact, both writing openapi.yaml and hosting ReDoc isn't that hard. The rare thing to find here is, having both validation and documentation in the same place.
True, I have never find anything as convenient as FastAPI, but Node.js's Fastify can achieve same result without code duplication, but do need some plugins for documentation. (But for validation, you also don't need any.)
I look forward to this series
What about node + express ?
Last I checked I wasn’t able to find anything in node that would let you automatically generate and host an OpenAPI document without writing bits of OpenAPI syntax yourself. Most of the extensions/frameworks require you to write the JSON schema by hand.
TypeScript is a bit better than Python in some of the limitations I listed, but does not have all the advantages of Rust. So it would be a tough sell to go from FastAPI & Python to TS as there wouldn’t be as many clear improvements.