Why did we choose FAST API over Flask and Django for our RESTFUL Micro-services

Ahmed Nafies
3 min readFeb 1, 2020

--

Intro

Both Django and Flask are great frameworks, for many years I have been using them interchangeably depending on the service I am trying create.

Django comes in handy when a service depends on a database, needs a simple admin interface and perhaps a requires a nice web GUI. All that comes out of the box with Django thanks to its amazing ORM, admin app and template engine.

When a simple micro-service that exposes a couple API Endpoints is needed, this is where Flask shines. Moreover, Flask can be easily extended, as there are a lot of external packages if we need templates or ORM.

However when it comes to RESTful microservices, both Flask and Django did not live up to expectations when it came to performance and development speed. This is when we found Fast API.

Performance

According to techempower benchmark Fast API beats every other framework when it comes to performance where Flask and Django are no where near the Top 10.

Performance benchmark based of Responses per second at 20 queries per request

So yeah, its fast … REALLY FAST!

Development Speed

Here we are only concerned about a RESTful Micro-service, which would have the following as basic requirements.

  • Exposes REST endpoint/s.
  • OpenAPI(Swagger) documentation.
  • Database connection (Optional).

Django

Django package does include REST API’s for that Django-rest-framework is your best option. It comes with a ton of features, but Open API generation is not among those features :). It comes with its own documentation for REST APIs though. For Open API documentation, drf-yasg or django-rest-swagger packages can be used. Trying to extend the documentation is quite painful with any of these and sometimes we even had to just use .yaml files instead. ORM is included even if you are not going to use a relational database or a database at all.

Flask

No external packages are required to create REST endpoints with Flask however, for Open API documentation external packages like Flask-restplus can be utilised, However it usually generates outdated version of Open API documentation.

Fast API

Creating a REST API endpoint with Fast API and Open API takes less than a 5 minutes.

import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel


class User(BaseModel):
first_name: str
last_name: str = None
age: int


app = FastAPI()


@app.post("/user/", response_model=User)
async def create_user(user: User):
result = await some_library(user)
return result

if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)

The above code block will expose an API end-point to create user with Open API version 3 documentation (by default).

ReDoc documentation as a bonus as well

Changing the customizing documentation and adding validations is quite easy and it is explained in the documentation here.

One Last Thing

Did you notice this code block?

@app.post("/user/", response_model=User)
async def create_user(user: User):
result = await some_library(user)
return result

create_user function will run asynchronously since Fast API actually supports asynchronicity. Now in Django 3.0 this can be achieved as well until you need to use the ORM (sync) you will get SynchronousOnlyOperation exception. But not in Fast API, with the use of databases or ginothis can be achieved.

If you would like to know how to work with databases both sync and async , you can follow this tutorial guide.

All code used for this article can be found here on github.

--

--