A comprehensive comparison of three popular Python web frameworks: FastAPI, Django REST Framework (DRF), and Flask. This project includes production-ready Docker configurations with separate PostgreSQL instances for each application, optimized database connection pools for high-load benchmarking, comprehensive Locust load testing, and a benchmarking script to test CRUD operations across all three frameworks.
If this script was useful to you, consider donating to support the Developer Service Blog: https://buy.stripe.com/bIYdTrggi5lZamkdQW
FastAPI-DjangoDRF-Flask-Comparison/
├── api_benchmark.py # High-performance benchmark script
├── locustfile.py # Locust load testing scenarios
├── run_locust_tests.py # Automated Locust test runner
├── docker-compose.yml # Docker Compose configuration
├── fastapi_app/ # FastAPI application
│ ├── Dockerfile # Production Dockerfile
│ ├── requirements.txt # Python dependencies
│ ├── main.py # FastAPI application
│ ├── models.py # SQLAlchemy models
│ └── database.py # Optimized database configuration
├── flask_app/ # Flask application
│ ├── Dockerfile # Production Dockerfile
│ ├── requirements.txt # Python dependencies
│ ├── app.py # Flask application
│ ├── models.py # SQLAlchemy models
│ └── database.py # Optimized database configuration
└── drf_app/ # Django REST Framework application
├── Dockerfile # Production Dockerfile
├── requirements.txt # Python dependencies
├── manage.py # Django management script
├── django_app/ # Django project settings
└── api/ # DRF API app
├── models.py # Django models
├── serializers.py # DRF serializers
├── views.py # DRF views
└── urls.py # URL routing
- Docker and Docker Compose
- Python 3.11+ (for local development)
-
Clone the repository:
git clone https://github.com/nunombispo/FastAPI-DjangoDRF-Flask-Comparison cd FastAPI-DjangoDRF-Flask-Comparison -
Start all services:
docker-compose up -d
-
Run the benchmark:
python api_benchmark.py
-
Run Locust load tests:
python run_locust_tests.py
-
Set up virtual environments:
# FastAPI cd fastapi_app python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install -r requirements.txt # Flask cd ../flask_app python -m venv venv source venv/bin/activate pip install -r requirements.txt # DRF cd ../drf_app python -m venv venv source venv/bin/activate pip install -r requirements.txt
-
Start the applications:
# FastAPI (Terminal 1) cd fastapi_app uvicorn main:app --host 0.0.0.0 --port 8000 # Flask (Terminal 2) cd flask_app gunicorn --bind 0.0.0.0:5000 app:app # DRF (Terminal 3) cd drf_app python manage.py migrate gunicorn --bind 0.0.0.0:8001 django_app.wsgi:application
-
Run the benchmark:
python api_benchmark.py
All three applications provide the same REST API endpoints:
| Method | Endpoint | Description |
|---|---|---|
GET |
/items/ |
List all items |
POST |
/items/ |
Create a new item |
GET |
/items/{id} |
Get a specific item |
PUT |
/items/{id} |
Update a specific item |
DELETE |
/items/{id} |
Delete a specific item |
{
"id": 1,
"name": "Test Item",
"description": "A performance benchmark item",
"price": 99.99,
"in_stock": true
}- Server: Uvicorn (ASGI)
- Port: 8000
- ORM: SQLAlchemy with optimized connection pool
- Documentation: Auto-generated at
/docs - Features: Async/await, automatic validation, OpenAPI
- Server: Gunicorn (WSGI)
- Port: 5000
- ORM: SQLAlchemy with Flask-SQLAlchemy and optimized connection pool
- Features: Lightweight, flexible, minimal boilerplate
- Server: Gunicorn (WSGI)
- Port: 8001
- ORM: Django ORM with optimized connection settings
- Features: Built-in admin, comprehensive ecosystem, ViewSets
The api_benchmark.py script performs comprehensive CRUD operations testing with optimized settings for high-load scenarios:
- Operations: POST (create), GET (read), PUT (update), DELETE (delete)
- Concurrency: 20 concurrent requests
- Requests per test: 500 items per framework (configurable)
- Connection Management: Optimized connection pooling and limits
- Metrics: Duration, success rate, failures, requests per second (RPS)
- Connection Pool Optimization: Prevents database connection exhaustion
- Strategic Delays: Allows database stabilization between operations
- Error Handling: Robust exception handling with detailed logging
- Progress Tracking: Real-time progress updates for each operation
- Performance Metrics: Comprehensive performance analysis
2025-08-05 15:11:27,332 - INFO - Starting API Benchmark...
2025-08-05 15:11:27,333 - INFO - ==================================================
2025-08-05 15:11:27,335 - INFO - Testing FASTAPI at http://localhost:8000/items/
2025-08-05 15:11:27,336 - INFO - Starting benchmark for FASTAPI...
2025-08-05 15:11:27,337 - INFO - FASTAPI - Creating 500 items...
2025-08-05 15:11:27,338 - INFO - FASTAPI - Created 500 items
2025-08-05 15:11:27,339 - INFO - FASTAPI - Reading 500 items...
2025-08-05 15:11:27,340 - INFO - FASTAPI - Updating 500 items...
2025-08-05 15:11:27,341 - INFO - FASTAPI - Deleting 500 items...
2025-08-05 15:11:27,342 - INFO - FASTAPI - Benchmark completed
2025-08-05 15:11:27,343 - INFO - FASTAPI =>
2025-08-05 15:11:27,344 - INFO - Duration: 2.46s
2025-08-05 15:11:27,345 - INFO - Success: 2000/2000
2025-08-05 15:11:27,346 - INFO - Failures: 0
2025-08-05 15:11:27,347 - INFO - RPS: 813.01
In addition to the custom benchmark script, this project includes comprehensive Locust load testing for realistic user scenarios.
- Realistic User Behavior: Simulates actual user interactions with random data
- CRUD Operations: Tests all Create, Read, Update, Delete operations
- Multiple User Classes: Dedicated test classes for each framework
- Comprehensive Metrics: Response times, failure rates, throughput
- HTML Reports: Detailed visual reports with charts and statistics
- CSV Export: Raw data export for further analysis
# Run automated test suite
python run_locust_tests.pyThe Locust test suite includes:
- Random API Selection: Tests all three APIs randomly
- Realistic Workload: 3:2:2:1 ratio for GET:POST:PUT:DELETE operations
- Dynamic Data: Random item names, prices, and descriptions
- Error Handling: Graceful handling of 404s and invalid responses
- FastAPIUser: Dedicated FastAPI testing
- FlaskUser: Dedicated Flask testing
- DRFUser: Dedicated DRF testing
| Operation | Weight | Description |
|---|---|---|
| List Items | 3 | GET /items/ - Most common operation |
| Create Item | 2 | POST /items/ - Add new items |
| Get Item | 2 | GET /items/{id} - Retrieve specific item |
| Update Item | 1 | PUT /items/{id} - Modify existing item |
| Delete Item | 1 | DELETE /items/{id} - Remove item |
🚀 Starting Locust Load Testing Suite
📅 2025-08-05 15:30:00
============================================================
Starting Locust test for FASTAPI
Host: http://localhost:8000
Users: 50
Spawn Rate: 5 users/second
Run Time: 60 seconds
============================================================
✅ FASTAPI test completed successfully
📊 Results saved to: locust_results/fastapi_load_test_20250805_153000
Locust generates comprehensive reports including:
- Response Time Statistics: Min, max, median, 95th percentile
- Request Rate: Requests per second
- Failure Rate: Percentage of failed requests
- User Count: Concurrent users over time
- Response Time Distribution: Histogram of response times
Each API has its own dedicated PostgreSQL instance for complete isolation:
| Application | Database Container | Database Name | External Port | Internal Port |
|---|---|---|---|---|
| FastAPI | pg-fastapi |
testdb_fastapi |
5432 |
5432 |
| Flask | pg-flask |
testdb_flask |
5433 |
5432 |
| DRF | pg-drf |
testdb_drf |
5434 |
5432 |
- Complete Isolation: Each application has its own database instance
- Independent Scaling: Each database can be scaled independently
- No Shared Dependencies: Issues with one database won't affect others
- Better Performance: No resource contention between applications
- Easier Debugging: Isolated issues and easier troubleshooting
All Dockerfiles include:
- Base Image: Python 3.11-slim
- Security: Non-root user (
appuser) - Performance: Optimized layer caching
- Health Checks: Application-specific endpoints
- Environment: Production-ready settings
- Database Waiting: Applications wait for their respective databases to be ready
- Connection Optimization: High-performance database connection pools
- postgres-fastapi: PostgreSQL instance for FastAPI (port 5432)
- postgres-flask: PostgreSQL instance for Flask (port 5433)
- postgres-drf: PostgreSQL instance for DRF (port 5434)
- fastapi: FastAPI application on port 8000
- flask: Flask application on port 5000
- drf: Django REST Framework on port 8001
| Aspect | FastAPI | Flask | Django REST Framework |
|---|---|---|---|
| Learning Curve | Moderate | Easy | Steep |
| Performance | Excellent | Good | Good |
| Async Support | Native | Limited | Limited |
| Documentation | Auto-generated | Manual | Manual |
| Admin Interface | None | None | Built-in |
| Validation | Pydantic | Manual | Serializers |
| URL Routing | Path parameters | Decorators | URL patterns |
| Connection Pool | Optimized | Optimized | Optimized |
- FastAPI: Add endpoints in
fastapi_app/main.py - Flask: Add routes in
flask_app/app.py - DRF: Add views in
drf_app/api/views.py
- FastAPI/Flask: SQLAlchemy handles migrations automatically
- DRF: Run
python manage.py makemigrationsandpython manage.py migrate
# Test individual applications
curl http://localhost:8000/items/ # FastAPI
curl http://localhost:5000/items/ # Flask
curl http://localhost:8001/items/ # DRF# Connect to individual databases
psql -h localhost -p 5432 -U testuser -d testdb_fastapi # FastAPI DB
psql -h localhost -p 5433 -U testuser -d testdb_flask # Flask DB
psql -h localhost -p 5434 -U testuser -d testdb_drf # DRF DB# api_benchmark.py
NUM_REQUESTS = 500 # Number of items to create per framework
CONCURRENCY = 20 # Concurrent requestsThe benchmark script creates timestamped log files:
- File:
api_benchmark_YYYYMMDD_HHMMSS.log - Console: Real-time output
- Level: INFO and above
- Format: Timestamp, level, message
- Progress: Detailed operation progress tracking
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is open source and available under the MIT License.
- FastAPI team for the excellent async framework
- Flask team for the lightweight WSGI framework
- Django team for the comprehensive web framework
- SQLAlchemy team for the powerful ORM