When we are talking about application development, almost 99% of the time we are talking about an application that is going to live in the public domain, i.e. web application. There are tons of web frameworks circling around written and native to many different programming languages.

In my 3 and a half long career as a Software Engineer, I’ve used a couple of different frameworks. I’ve worked with Django (currently working as well), Flask, FastAPI build in Python, Ruby on Rails build in Ruby, Spring Boot build in Java, as well as a bit with Express JS and vanilla PHP. Out of everything mentioned, I’m most comfortable working with Django.

I’ve completed 4 fairly complex applications, that are used by thousands of people and that are performance-dependent. Even though Python is not known for its performance, and especially paired with Django, which is one of the heaviest frameworks, I’ve managed to develop pretty optimized solutions.

A few months ago, I started working on a really interesting project, highly dependent on performance, having tons of complex Machine Learning algorithms running in the background, used to recognize data and create intelligent optimized output. The backend team on the project is using FastAPI as their web framework.

Before starting this position, I’d never had the chance to use it, nor have read about it. Despite having experience in working with pretty much everything, I was a bit excited to dive into the new framework and see why people were praising it so much.

Why FastAPI is Better Than Anything Else

So what is FastAPI? According to their documentation: “FastAPI is a modern, fast (high-performance), a web framework for building APIs with Python 3.7+ based on standard Python type hints.

The main advantages that you should expect from FastAPI are:

  • Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.
  • Fast to code: Increase the speed to develop features by about 200% to 300%. 
  • Fewer bugs: Reduce about 40% of human (developer) induced errors. 
  • Intuitive: Great editor support. Completion everywhere. Less time debugging.
  • Easy: Designed to be easy to use and learn. Less time reading docs.
  • Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
  • Robust: Get production-ready code. With automatic interactive documentation.
  • Standards-based: Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.

 

The crazy thing is, those expected advantages are really there. For example, the near Node JS and Go performance. You can check the proof for this in the TechEmpower benchmark

You can see that FastAPI, Uvicorn, and Starlette outperformed Go and Node JS in a couple of instances.

Better code productivity. The fact that supports autocomplete for almost anything, reduces the chance for the developer to make a mistake, plus it significantly shortens the time of writing the code. The hint types are also a huge part of this code productivity.

For example, when I am sending data with Django Rest Framework, sometimes it might happen for an integer to be converted to a string, and your code to fail and to get a message, referring to TypeError.

And if you are working on a complex problem an error like this might block you even more. By using hint types in FastAPI, you can customize the expected output and prevent stuff like this from happening. It is doable in Django as well, but in FastAPI is native.

It is so easy to learn and to start using it. For example, it does not force you to follow any methodology (for example Django and Spring Boots are MVC native and follow a strict structure that can be complex at times, and completely inapplicable out of the framework’s ecosystem). 

The integration with OpenAPI (Swagger) also comes with the benefits like out-of-the-box authentication and authorization with HTTPBasic, OAuth2, as well as using Swagger UI to test endpoints.

This cuts the need of using third-party tools like Postman and having the need to authenticate on 1000 places in order to be able to check your endpoint. It also allows more independent work between the front-end and back-end teams, since in Swagger UI everything is really nicely documented, so the user can see what type of data should be sent and what output should be expected. 

Other extremely important features that I want to mention are the Startlette and Pytdantic integrations. 

Since FastAPI is a subclass of Starlette you get all of Starlette’s features:

  • WebSocket support.
  • In-process background tasks.
  • Startup and shutdown events.
  • Test client built on requests.
  • CORS, GZip, Static Files, Streaming responses.
  • Session and Cookie support.
  • 100% test coverage.
  • 100% type annotated codebase.

 

FastAPI is fully compatible with (and based on) Pydantic. This includes external libraries also based on Pydantic, as ORMs, ODMs for databases.

This also means that in many cases you can pass the same object you get from a request directly to the database, as everything is validated automatically.

The same applies the other way around, in many cases, you can just pass the object you get from the database directly to the client.

And probably the best feature of FastAPI is the fact that it is built around the ability to write Asynchronous code. This means that the language has a way to tell the computer program that at some point in the code, it will have to wait for something else to finish somewhere else. Let’s say that something else is called “slow-file”.

So, during that time, the computer can go and do some other work, while “slow-file” finishes.

Then the computer program will come back every time it has a chance because it’s waiting again, or whenever it finished all the work it had at that point. And it will see if any of the tasks it was waiting for have already finished, doing whatever it had to do.

Next, it takes the first task to finish (let’s say, our “slow-file”) and continues whatever it has to do with it.

That “wait for something else” normally refers to I/O operations that are relatively “slow” (compared to the speed of the processor and the RAM memory), like waiting for:

  • the data from the client to be sent through the network
  • the data sent by your program to be received by the client through the network
  • the contents your program gave to the system to be written to disk
  • a remote API operation
  • a database operation to finish

As the execution time is consumed mostly by waiting for I/O operations, they call them “I/O bound” operations.

It’s called “asynchronous” because the computer program doesn’t have to be “synchronized” with the slow task, waiting for the exact moment that the task finishes, while doing nothing, to be able to take the task result and continue the work.

Instead of that, by being an “asynchronous” system, once finished, the task can wait in line a little bit (some microseconds) for the computer program to finish whatever it went to do, and then come back to take the results and continue working with them.

For “synchronous” (contrary to “asynchronous”) they commonly also use the term “sequential”, because the computer program follows all the steps in sequence before switching to a different task, even if those steps involve waiting.

 

Conclusion

I mean it really sounds like the obvious choice. Of course, this article is not the definite direction for you to choose FastAPI, you’ll choose whatever suits your needs and skills the best. But if you are in the process of learning something new, or you are a Python developer, then you might want to take a look.

Tons of successful companies are using FastAPI for their applications or at least some of their services. Here is a link where you check the experiences and opinions on FastAPI from companies like Uber, Netflix, etc.

If you’re interested in learning more about Computer Science, in a well-explained, concise way, you can check out some of our previous articles:

 

  1. How to Gain a Computer Science Education from MIT University for FREE
  2. FREE Courses from Harvard University to Become Data Scientist
  3. Learn How To Do K-Means Clustering On An Image
  4. Top 10 Machine Learning Books That You Should Read Before You Start With It
  5. Top 40 COMPLETELY FREE Coursera Artificial Intelligence and Computer Science Courses

 

Like with every post we do, we encourage you to continue learning, trying, and creating.

Facebook Comments