FastAPI vs Flask: Choosing the Right Python Framework for Web Services

Samrat Kumar Das
6 min readDec 20, 2023

--

fastapi vs flask

Python stands out as one of the premier programming languages for web development, owing to its ease of learning, extensive libraries, and support for diverse programming paradigms, including object-oriented, functional, and procedural. When it comes to selecting a web framework for building web services, the plethora of options can be overwhelming. Among the top contenders are FastAPI and Flask. In this blog post, we’ll conduct a comprehensive comparison to determine which framework excels in the realm of web services.

Understanding Web Services

Web services are applications that furnish functionality or data over the internet, leveraging standard protocols such as HTTP and JSON. They find utility in diverse domains like data analysis, machine learning, e-commerce, and social media. Web services can be broadly categorized into two types: RESTful and SOAP.

RESTful Web Services: Based on the Representational State Transfer (REST) architectural style, these services adhere to principles and constraints that emphasize scalability, uniformity, and statelessness. Operations on resources, such as users, products, and orders, are executed using HTTP methods (GET, POST, PUT, DELETE), and data exchange is typically in JSON or XML format.

SOAP Web Services: Built on the Simple Object Access Protocol (SOAP), these services facilitate the exchange of structured information between applications using XML. SOAP messages conform to a predefined format consisting of an envelope, a header, and a body. Additionally, a Web Services Description Language (WSDL) file is used to describe the operations and data types of the web service.

Introducing FastAPI

FastAPI emerges as a modern, high-performance web framework tailored for building APIs with Python 3.6 and above. Grounded in the Starlette framework and Pydantic library, FastAPI boasts an array of features:

  • Automatic data validation and documentation through type hints and OpenAPI standards.
  • Dependency injection system for efficient management of dependencies and streamlined testing.
  • Support for asynchronous code using async/await syntax.
  • Robust support for OAuth2 authentication and JWT tokens.
  • In-built compatibility with common tools such as SQLAlchemy, MongoDB, Redis, and more.

Unveiling Flask

Flask, on the other hand, positions itself as a lightweight, micro-framework designed for constructing web applications with Python. Built upon the Werkzeug toolkit and Jinja2 template engine, Flask offers the following features:

  • A minimalist and flexible design that empowers developers to choose their preferred tools and libraries.
  • A routing system that maps URLs to functions (views), facilitating clean URL patterns.
  • A template engine for rendering HTML pages using variables and expressions.
  • Support for sessions, cookies, error handling, logging, and more.
  • Extensibility with a wealth of extensions catering to various needs, including database integration, authentication, caching, and testing.

FastAPI vs Flask: A Comparative Analysis

Performance

Performance stands as a critical factor when evaluating web frameworks. A swift and efficient web service can handle more requests per second, utilize fewer resources, and enhance the overall user experience. Benchmarking tools, such as TechEmpower’s Web Framework Benchmarks, provide valuable insights into the performance of different frameworks across various metrics like response time, throughput, and memory usage.

According to the latest results from TechEmpower’s benchmark (Round 20), FastAPI significantly outperforms Flask in various scenarios:

  • JSON Serialization Test: FastAPI achieves an impressive 1.2 million requests per second (RPS), while Flask lags behind at 87 thousand RPS.
  • Single Query Test: FastAPI excels with 230 thousand RPS, leaving Flask at 13 thousand RPS.
  • Multiple Queries Test: FastAPI demonstrates superior performance with 22 thousand RPS, while Flask trails at 1.6 thousand RPS.

These results underscore FastAPI’s prowess in efficiently handling requests, processing data, and delivering swift responses.

Ease of Use

Ease of use is another pivotal factor influencing the choice of a web framework. A user-friendly and intuitive framework empowers developers to write concise code, minimize errors, and focus on the core business logic. To assess the ease of use of FastAPI and Flask, let’s explore examples of implementing common web service features using each framework.

FastAPI Example:

from fastapi import FastAPI, Depends, HTTPException
from sqlalchemy.orm import Session
from pydantic import BaseModel
from typing import List

# Define the database model
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Item(Base):
__tablename__ = "items"

id = Column(Integer, primary_key=True, index=True)
name = Column(String, nullable=False)
description = Column(String, nullable=True)
price = Column(Integer, nullable=False)

# Define the Pydantic schema
class ItemCreate(BaseModel):
name: str
description: str = None
price: int

class ItemRead(BaseModel):
id: int
name: str
description: str = None
price: int

class Config:
orm_mode = True

# Create the FastAPI app
app = FastAPI()

# Create the database connection
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

engine = create_engine("sqlite:///items.db")
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base.metadata.create_all(bind=engine)

# Define the dependency for the database session
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()

# Define the endpoints for the CRUD operations
@app.post("/items/", response_model=ItemRead)
def create_item(item: ItemCreate, db: Session = Depends(get_db)):
# Create a new item in the database
db_item = Item(name=item.name, description=item.description, price=item.price)
db.add(db_item)
db.commit()
db.refresh(db_item)
return db_item

@app.get("/items/", response_model=List[ItemRead])
def read_items(db: Session = Depends(get_db)):
# Read all items from the database
items = db.query(Item).all()
return items

@app.get("/items/{item_id}", response_model=ItemRead)
def read_item(item_id: int, db: Session = Depends(get_db)):
# Read a single item from the database by id
item = db.query(Item).filter(Item.id == item_id).first()
if not item:
raise HTTPException(status_code=404, detail="Item not found")
return item

@app.put("/items/{item_id}", response_model=ItemRead)
def update_item(item_id: int, item: ItemCreate, db: Session = Depends(get_db)):
# Update an existing item in the database by id
db_item = db.query(Item).filter(Item.id == item_id).first()
if not db_item:
raise HTTPException(status_code=404, detail="Item not found")
db_item.name = item.name
db_item.description = item.description
db_item.price = item.price
db.commit()
db.refresh(db_item)
return db_item

@app.delete("/items/{item_id}", response_model=None)
def delete_item(item_id: int, db: Session = Depends(get_db)):
# Delete an existing item from the database by id
db_item = db.query(Item).filter(Item.id == item_id).first()
if not db_item:
raise HTTPException(status_code=404, detail="Item not found")
db.delete(db_item)
db.commit()

Flask Example:

from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///items.db'
db = SQLAlchemy(app)

class Item(db.Model):
__tablename__ = 'items'

id = db.Column(db.Integer, primary_key=True, index=True)
name = db.Column(db.String, nullable=False)
description = db.Column(db.String, nullable=True)
price = db.Column(db.Integer, nullable=False)

class ItemCreate:
def __init__(self, name, description=None, price=None):
self.name = name
self.description = description
self.price = price

class ItemRead:
def __init__(self, id, name, description=None, price=None):
self.id = id
self.name = name
self.description = description
self.price = price

# Create the database tables
db.create_all()

# Define the endpoints for the CRUD operations
@app.route('/items/', methods=['POST'])
def create_item():
data = request.json
new_item = Item(name=data['name'], description=data.get('description'), price=data['price'])
db.session.add(new_item)
db.session.commit()
return jsonify(ItemRead(id=new_item.id, name=new_item.name, description=new_item.description, price=new_item.price).__dict__)

@app.route('/items/', methods=['GET'])
def read_items():
items = Item.query.all()
items_list = [ItemRead(id=item.id, name=item.name, description=item.description, price=item.price).__dict__ for item in items]
return jsonify(items_list)

@app.route('/items/<int:item_id>', methods=['GET'])
def read_item(item_id):
item = Item.query.get(item_id)
if not item:
return jsonify({"error": "Item not found"}), 404
return jsonify(ItemRead(id=item.id, name=item.name, description=item.description, price=item.price).__dict__)

@app.route('/items/<int:item_id>', methods=['PUT'])
def update_item(item_id):
data = request.json
item = Item.query.get(item_id)
if not item:
return jsonify({"error": "Item not found"}), 404
item.name = data['name']
item.description = data.get('description')
item.price = data['price']
db.session.commit()
return jsonify(ItemRead(id=item.id, name=item.name, description=item.description, price=item.price).__dict__)

@app.route('/items/<int:item_id>', methods=['DELETE'])
def delete_item(item_id):
item = Item.query.get(item_id)
if not item:
return jsonify({"error": "Item not found"}), 404
db.session.delete(item)
db.session.commit()
return jsonify({"message": "Item deleted successfully"})

Both examples illustrate the simplicity of implementing a RESTful web service that enables users to perform CRUD operations on items in a database. While FastAPI emphasizes automatic data validation and documentation, Flask embraces a minimalist design that allows developers to choose their preferred tools and libraries.

In conclusion, the choice between FastAPI and Flask depends on the specific requirements of your web service project. FastAPI excels in performance and modern features, making it a strong contender for data-intensive applications. On the other hand, Flask’s simplicity and flexibility make it an excellent choice for smaller projects where a lightweight framework is preferred.

Whether you prioritize performance, ease of use, or a combination of both, carefully evaluating the strengths of FastAPI and Flask will guide you towards selecting the most suitable framework for your web service endeavors.

--

--

Samrat Kumar Das
Samrat Kumar Das

Written by Samrat Kumar Das

" Is there anything that we cannot achieve ? "

No responses yet