An another Django vs Flask comparison. which one is better.!!

Titus Joyson
7 min readOct 27, 2019

--

Having trouble deciding on choosing a python framework for building up REST APIs for your web application? Here comes a quick statistical performance comparison between Django and Flask in terms of requests processing that also involves DB transactions. I have also covered feature-based comparisons between those 2.

Many aspects need to be considered before choosing a suitable web framework. And one of the most common aspects is the number of requests a framework can process, how good the ORM is and does it have all the necessary features that are required for developing the web application. If we search the web for “Django vs flask which one is faster” most of the results will end up in favor of flask.

But is it still true when we develop API with them?

A request can be as simple as sending a static resource to client requests and also can be something that would involve heavy computations involving DB transactions for creating the response. When processing a request we may need to do some additional processing based on the request for validating the payload, fetching data from the database and serializing the ORM object to JSON before serving them in API.

This is not an article to compare which one is best but rather than that which one should fit for your applications. In this article we will see some benchmark for req/sec for each framework, ORM benchmarking and exploring the Features for both Frameworks.

Where can I find the code:

GitHub: https://github.com/titusjoyson/django-flask-speedtest

Following parameters and its combinations are used for collecting the performance metrics :

  • Requests with and without ORM
  • With a large number of data in the database
  • With ORM as Django ORM and Flask-Sqlalchemy

Test:

Note: All tests are taken in a local machine with limited resources and no performance optimization for database or any other optimizations are added. So results added in the article may vary accordingly. And the Apache Benchmark tool is used for API benchmarking.

The tests are performed over thousands and lakhs of data with single workers and multiple workers.

Frameworks & Libraries:

Test Environment:

+----------+-------------------------------+
| Hardware | Ubuntu 18.04 |
| | Intel(R)i5-6200U CPU @2.30GHz |
| | 8 GB RAM |
| | SSD hard disk |
|----------|-------------------------------|
| Software | Nginx, Gunicorn |
+----------+-------------------------------+

Test — Requests Processing ThroughPut:

Metrics are collected by triggering API requests that cover the following patterns.

  1. Request for fetching some static data
  2. Request that involves some logical operations on the data to be returned
  3. Request that requires DB transactions for data
  4. Request that involves DB functions such as aggregate, count, create and update

Note: The data retrieved from the ORM will be in objects so before transferring these data its serialized into JSON data

Results:

From the results, it’s clear that Flask has upper hand when there is 10000 predefined records with 2000 requests in 10 concurrency and 4 workers.

The below results are obtained when

How About the results when the number of records in the database is increased, Let’s increase the number of records to ten lakh and see the results:

Results:

Yes, the results vary when we have a large number of data to process and here Django comes closed in all the aspects when comparing to the flask.

The below results are obtained when there are 10,00,000 predefined records with 2000 requests in 10 concurrency and 4 workers

Benchmarking Django ORM and Flask-Sqlalchemy:

We have seen the benchmark of Django and Flask request and response speed but what about Django ORM and SqlAlchemy without the web framework.

Below is the result of the time (seconds) taken to execute 1000 queries per action on 10000 records.

Well based on the results SQLAlchemy has upper hand in operations like select, pagination and save while Django has upper hand in the count, aggregate, create and update operations.

Feature-based comparison:

We saw the performance benchmark for Django and Flask, now let’s see some framework features.

Django:

Django it pretty much has a complete suite for building and managing a stable web application.

  1. Worry less about setting up the base and more concentration on the application itself.
  2. Don’t need to worry about adding multiple libraries and worry about dependencies and security vulnerabilities in the future.
  3. It comes with a nice admin panel and a bunch of helper code to reuse.
  4. Django comes with a bunch of security features like Cross-site scripting (XSS) protection, Cross-site request forgery (CSRF) protection, SQL injection protection Clickjacking protection, etc.., for more details about security click here.
  5. It has a great built-in feature for RBAC for managing user roles and groups which mainly helps in Authorisation.
  6. Database migrations mare made easy with Django migrations
  7. Django ORM doesn’t provide support for NoSql databases

Flask:

  1. Flask is very lightweight and one can write a server in flask within a few lines of code.
  2. One can Plug his/her favorite libraries like ORM, Migrations etc… In short its simply flexible.
  3. It has ORM support for both SQL and NoSQL databases

Feature Extendability:

Django provides a verity of base classes that can be extended to implement new features or modify existing features. And it allows us to easily apply these changes by configuring them in the settings file. Some of them will be Views provided by Django and DRF, Middlewares, logger, Catching and SessionStore, Authentication, Authorization, AbstractUser, etc.. Even though Django provides easy extendability most of them are tightly coupled with the Django core structure. And when it comes to ORM Other than the drivers provided by Django there are not many options to go with and Django ORM is only for SQL databases but there are third party libraries that we can make use of. But still, we can use other ORM instead of Django ORM which will affect the native behavior of Django.

On the Other hand, Flask is minimalistic and we can extend the existing classes to meet our own requirements. Some of the Classes that we can name are Response, BaseView, etc.. in general any class can be inherited and can be extended. As being minimalistic flask has its advantages, for example, one can choose any ORM that they see fit to be integrated with Flask.

Release:

Django uses a time-based release schedule, with feature releases every eight months and a new long-term support release (LTS) every 2 years. LTS releases are supported with security updates for 3 years. For more details about Django release Process visit Django release process

Flask has an iterative release process and when there is a stable system a new release is made and Both Django and Flask uses Simatic versioning for there releases

Community:

When comes to community Django and Flask both have a great community on GitHub, StackOverflow, and other platforms. While Django has its own community page, mailing list, and contributing page.

Based on Stack overflow survey 2019

+---------+--------+-------+
| | Django | Flask |
+---------+--------+-------+
| Used by | 13% | 12.1% |
| Loved | 62.1% | 61.1% |
| Wanted | 7.8% | 4.3% |
+---------+--------+-------+

Based on September 26, 2019 — October 26, 2019, Github insights

Django has 122 Active Pull Requests 0 Active Issues and Flask has 9 Active Pull Requests 19 Active Issues.

+---------+--------+-------+
| | Django | Flask |
+---------+--------+-------+
| Used By | 313K | 334K |
| Stars | 44.9K | 47.2K |
| Fork | 19.4K | 13K |
+---------+--------+-------+

Conclusion:

Both Django and Flask are popular in web development. Django ships most of the tools and features required for developing web applications and it is more popular among developers and Enterprise applications. The huge pre-build robust features and stability that it provides makes the time needs to meet deliverable short and which makes it easier to develop and deploy complex applications.

Flask, on the other hand, is a lightweight micro-framework for those who want to develop applications that are simple and need more control over the framework. Which allow the developers to have the ability to add their favorite modules from huge open source community.

Editor: Balaji Rajendran

--

--