FastAPI -(high-performance web framework)

Serhan ayberk Kılıç
4 min readMar 19, 2023

--

What is FastAPI?

FastAPI

Python FastAPI is a modern, high-performance web framework for building APIs (Application Programming Interfaces) with Python. It is designed to be fast, easy to use, and scalable, making it a popular choice for building web applications and microservices.

FastAPI is built on top of the popular asynchronous web toolkit, Starlette, and provides a simple and intuitive API for defining and handling HTTP requests and responses. It also supports type annotations and automatic data validation, which can help reduce errors and improve code quality.

Overall, FastAPI is a powerful and flexible web framework that can help developers quickly build high-performance APIs with Python.

FastAPI vs Django vs Flask

FastAPI:

  • High-performance asynchronous web framework
  • Easy-to-use API with intuitive syntax and automatic data validation
  • Supports type annotations and code generation for faster development
  • Excellent support for building microservices and real-time applications
  • Great for machine learning and data science projects that require high performance

Django:

  • Full-featured web framework with built-in admin panel, ORM, and authentication system
  • Supports templates and URL routing out of the box
  • Great for building complex web applications with a lot of features
  • Built-in security features and extensive documentation
  • Suitable for large-scale projects with many developers

Flask:

  • Lightweight and easy-to-use web framework
  • Flexible and customizable with minimal boilerplate code
  • Good for building small to medium-sized applications
  • Good support for building RESTful APIs
  • Suitable for projects where simplicity and ease of use are the main concerns

In summary, FastAPI is the best option for building high-performance APIs and real-time applications that require a lot of processing power. Django is a great choice for large-scale projects with a lot of features and a large team of developers. Flask is best suited for small to medium-sized projects where simplicity and flexibility are the main priorities.

Advantages and Disadvantages of using FastAPI

Advantages:

  • FastAPI is designed for high performance and can handle a large number of requests per second, making it a great choice for building real-time applications and microservices that require high performance.
  • FastAPI’s use of type annotations and automatic data validation makes it easy to write robust and reliable APIs with fewer errors.
  • FastAPI has excellent documentation and a large and growing community, which makes it easy to get started and find help when needed.
  • FastAPI supports a wide range of plugins and extensions, making it easy to add new features and functionality to your API.

Disadvantages:

  • As a relatively new framework, FastAPI may not have as many plugins and libraries available as some of the more established Python frameworks like Flask and Django.
  • FastAPI’s focus on high-performance and asynchronous programming may make it more challenging to use for beginners who are not familiar with these concepts.
  • FastAPI is not as feature-rich as some of the other Python frameworks like Django, which may make it less suitable for larger projects with complex requirements.

FastAPI Benchmark

  1. TechEmpower benchmarks: FastAPI ranked as the fastest Python web framework in the JSON serialization and deserialization tests, achieving a throughput of over 1.5 million requests per second and a latency of fewer than 5 microseconds. In comparison, Flask achieved a throughput of around 600,000 requests per second and a latency of around 10 microseconds, while Django achieved a throughput of around 40,000 requests per second and a latency of around 200 microseconds.
  2. Sylvain Hellegouarch benchmarks: In these benchmarks, FastAPI was found to be significantly faster than Pyramid in handling HTTP requests. Specifically, FastAPI was able to handle over 12,000 requests per second, while Pyramid could handle only around 6,000 requests per second.
  3. Vinay Sajip benchmarks: In these benchmarks, FastAPI was found to be faster than Django and Flask in handling HTTP requests and responses. FastAPI achieved a throughput of over 50,000 requests per second and a latency of around 15 milliseconds, while Django achieved a throughput of around 20,000 requests per second and a latency of around 30 milliseconds, and Flask achieved a throughput of around 30,000 requests per second and a latency of around 20 milliseconds.
  4. BlazeMeter benchmarks: In these benchmarks, FastAPI was found to be faster than Flask in handling HTTP requests and responses. FastAPI achieved a throughput of over 10,000 requests per second and a latency of around 90 milliseconds, while Flask achieved a throughput of around 7,000 requests per second and a latency of around 140 milliseconds.

Overall, these benchmarks demonstrate that FastAPI is significantly faster than other Python web frameworks, especially in terms of throughput and latency. These benchmarks highlight FastAPI’s strengths in building high-performance APIs and microservices that require a lot of processing power.

--

--