This practical course demonstrates how to develop powerful, scalable APIs using Python. We compare the popular frameworks FastAPI and Flask, demonstrating their respective strengths and areas of application. Through hands-on exercises, you will learn to build and optimise your own RESTful APIs for performance and scalability, developing a sound understanding of modern API architectures in Python.
1 Overview and comparison: Flask vs. FastAPI
- History and objectives of both frameworks
- Synchronous vs. asynchronous programming model
- Differences in routing, middleware, dependency injection
- Community, extensibility and ecosystem
- When is which framework suitable?
2 Architecture of modern, scalable APIs
- Layered architecture: routers, services, repositories
- API design principles (REST, stateless, idempotent)
- OpenAPI / Swagger integration out-of-the-box
- Separation of business and infrastructure logic
- Caching, rate limiting, authentication
3 Asynchronous processing and performance tuning
- Introduction to async/await in Python
- WSGI vs. ASGI – differences and implications
- Performance benefits of async I/O (e.g. for database access, HTTP requests)
- Use of background tasks and queues
- Comparison of servers: Uvicorn, Gunicorn, Hypercorn
4 Practical development of REST APIs with both frameworks
- Project setup and structuring for Flask and FastAPI
- Route definition, path parameters, query parameters
- CRUD endpoints based on an example (e.g. «ToDo» app, blog, shop)
- Database connection with SQLAlchemy or Tortoise ORM
- Integration of external APIs / services
5 Validation, serialisation and documentation with Pydantic & Swagger
- Introduction to Pydantic: Typing, validation, defaults
- Creation of request and response models
- Automatic OpenAPI generation and Swagger UI
- Use of tags, description texts and examples
- Efficient validation of data
6 Focus on testability and extensibility
- Unit tests and integration tests for API endpoints
- Test clients in FastAPI and Flask
- Mocking external dependencies
- Structuring for extensibility: services, blueprints, routers
- Dependency injection and configuration management
7 Benchmarking and scaling scenarios
- API benchmarking with tools such as ab, wrk, locust, k6
- Comparison of the runtime performance of FastAPI vs. Flask
- Scaling with Gunicorn, Uvicorn Workers, Docker & Kubernetes
- Load balancing and horizontal scaling
- Health checks and readiness probes
8 Best practices for production-ready API projects
- Logging, monitoring, tracing (e.g. with Prometheus, OpenTelemetry)
- Dealing with errors & exceptions (global handlers, structured error outputs)
- Security aspects: CORS, Auth, HTTPS, rate limiting
- Continuous integration / deployment (CI/CD) with Docker & GitHub Actions
- Versioning of APIs (URI, header, media type)