Skip to content

Latest commit

Β 

History

History
259 lines (196 loc) Β· 11.4 KB

File metadata and controls

259 lines (196 loc) Β· 11.4 KB

Architecture Overview

This document provides an overview of the fastapi-base project architecture, including the technology stack, project structure, and key design decisions.

πŸ—οΈ System Architecture

The fastapi-base project follows a modern, microservices-oriented architecture with the following components:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Load Balancer β”‚    β”‚     FastAPI     β”‚    β”‚   PostgreSQL    β”‚
β”‚    (Optional)   │────│   Application   │────│    Database     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚
                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                       β”‚      Redis      β”‚    β”‚     Celery      β”‚
                       β”‚    (Cache)      β”‚    β”‚   Workers       β”‚
                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

High-Level Request Flow

User Request β†’ Load Balancer β†’ FastAPI App β†’ Business Logic β†’ Database
                                    ↓
                              Cache Check (Redis) β†’ Background Tasks (Celery)

πŸ› οΈ Technology Stack

Core Framework

  • FastAPI - Modern, fast web framework for building APIs with Python
  • Pydantic - Data validation and settings management using Python type annotations
  • SQLModel - SQL databases in Python, designed for simplicity, compatibility, and robustness

Database & Storage

  • PostgreSQL - Advanced open-source relational database
  • Alembic - Database migration tool for SQLAlchemy
  • Redis - In-memory data structure store for caching and sessions

Background Processing

  • Celery - Distributed task queue for background job processing
  • Celery Beat - Scheduler for periodic tasks

Development & Deployment

  • Docker - Containerization for consistent development and deployment
  • uv - Fast Python package manager and resolver
  • Gunicorn + Uvicorn - ASGI server for production deployment

Code Quality & Testing

  • pytest - Testing framework with fixtures and plugins
  • ruff - Fast Python linter and code formatter
  • black - Code formatter for consistent style
  • mypy - Static type checker
  • pre-commit - Git hooks for code quality

πŸ“ Project Structure

fastapi-base/
β”œβ”€β”€ fastapi-base/                   # Main application directory
β”‚   β”œβ”€β”€ src/                        # Source code
β”‚   β”‚   β”œβ”€β”€ api/                    # API layer
β”‚   β”‚   β”‚   β”œβ”€β”€ v1/                 # API version 1
β”‚   β”‚   β”‚   β”‚   β”œβ”€β”€ endpoints/      # API endpoints
β”‚   β”‚   β”‚   β”‚   └── deps.py         # Dependencies
β”‚   β”‚   β”‚   └── __init__.py
β”‚   β”‚   β”œβ”€β”€ core/                   # Core configuration
β”‚   β”‚   β”‚   β”œβ”€β”€ config.py           # Settings and configuration
β”‚   β”‚   β”‚   β”œβ”€β”€ security.py         # Security utilities
β”‚   β”‚   β”‚   └── __init__.py
β”‚   β”‚   β”œβ”€β”€ db/                     # Database layer
β”‚   β”‚   β”‚   β”œβ”€β”€ base.py             # Database base classes
β”‚   β”‚   β”‚   β”œβ”€β”€ init_db.py          # Database initialization
β”‚   β”‚   β”‚   └── session.py          # Database session management
β”‚   β”‚   β”œβ”€β”€ models/                 # Data models
β”‚   β”‚   β”‚   β”œβ”€β”€ user.py             # User model
β”‚   β”‚   β”‚   └── __init__.py
β”‚   β”‚   β”œβ”€β”€ schemas/                # Pydantic schemas
β”‚   β”‚   β”‚   β”œβ”€β”€ user.py             # User schemas
β”‚   β”‚   β”‚   └── __init__.py
β”‚   β”‚   β”œβ”€β”€ services/               # Business logic layer
β”‚   β”‚   β”‚   β”œβ”€β”€ user_service.py     # User business logic
β”‚   β”‚   β”‚   └── __init__.py
β”‚   β”‚   β”œβ”€β”€ utils/                  # Utility functions
β”‚   β”‚   β”‚   β”œβ”€β”€ cache.py            # Caching utilities
β”‚   β”‚   β”‚   └── __init__.py
β”‚   β”‚   β”œβ”€β”€ migrations/             # Alembic migrations
β”‚   β”‚   β”‚   β”œβ”€β”€ versions/           # Migration files
β”‚   β”‚   β”‚   β”œβ”€β”€ env.py              # Alembic configuration
β”‚   β”‚   β”‚   └── script.py.mako      # Migration template
β”‚   β”‚   β”œβ”€β”€ tasks/                  # Celery tasks
β”‚   β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”‚   └── example_tasks.py    # Example background tasks
β”‚   β”‚   └── main.py                 # Application entry point
β”‚   β”œβ”€β”€ tests/                      # Test suite
β”‚   β”‚   β”œβ”€β”€ api/                    # API tests
β”‚   β”‚   β”œβ”€β”€ models/                 # Model tests
β”‚   β”‚   β”œβ”€β”€ services/               # Service tests
β”‚   β”‚   β”œβ”€β”€ conftest.py             # Pytest configuration
β”‚   β”‚   └── __init__.py
β”‚   β”œβ”€β”€ pyproject.toml              # Project dependencies & config
β”‚   β”œβ”€β”€ Dockerfile                  # Development Docker image
β”‚   β”œβ”€β”€ alembic.ini                 # Alembic configuration
β”‚   └── logconfig.yml               # Logging configuration
β”œβ”€β”€ ops/                            # Operations & deployment
β”‚   └── production.Dockerfile       # Production Docker image
β”œβ”€β”€ docs/                           # Documentation
β”‚   β”œβ”€β”€ architecture.md             # This file
β”‚   β”œβ”€β”€ developing.md               # Development guide
β”‚   β”œβ”€β”€ README-pt.md                # Portuguese README
β”‚   β”œβ”€β”€ README-fr.md                # French README
β”‚   └── README-es.md                # Spanish README
β”œβ”€β”€ docker-compose.yml              # Development environment
β”œβ”€β”€ Makefile                        # Development commands
β”œβ”€β”€ .env.example                    # Environment variables template
β”œβ”€β”€ CONTRIBUTING.md                 # Contribution guidelines
└── README.md                       # Main documentation

πŸ”„ Application Flow

Request Lifecycle

  1. Request Reception: HTTP requests are received by the FastAPI application
  2. Middleware Processing: Security, CORS, and logging middleware process the request
  3. Route Resolution: FastAPI routes the request to the appropriate endpoint
  4. Dependency Injection: FastAPI resolves dependencies (database session, authentication, etc.)
  5. Business Logic: Service layer handles the business logic
  6. Data Access: Models interact with the PostgreSQL database through SQLModel
  7. Response Serialization: Pydantic schemas serialize the response data
  8. Response Return: JSON response is returned to the client

Database Operations

  1. Connection Management: Database connections are managed through SQLModel/SQLAlchemy
  2. Query Execution: Async queries are executed against PostgreSQL
  3. Transaction Handling: Database transactions ensure data consistency
  4. Migration Management: Alembic handles database schema changes

Background Tasks

  1. Task Queuing: Tasks are queued in Redis through Celery
  2. Worker Processing: Celery workers process tasks asynchronously
  3. Result Storage: Task results are stored in Redis for retrieval
  4. Scheduling: Celery Beat handles periodic task scheduling

πŸ” Security Architecture

Database Security

  • Connection Encryption: SSL/TLS encryption for database connections
  • Parameter Binding: Parameterized queries to prevent SQL injection
  • Input Validation: Pydantic schemas validate all input data

Environment Configuration

  • Secret Management: Sensitive data stored in environment variables
  • Configuration Validation: Pydantic settings for configuration validation
  • Environment Isolation: Separate configurations for development, testing, and production

πŸ“Š Monitoring & Observability

Logging

  • Structured Logging: JSON-formatted logs for better parsing
  • Log Levels: Configurable log levels for different environments
  • Request Tracing: Correlation IDs for request tracking

Error Handling

  • Sentry Integration: Error tracking and performance monitoring
  • Exception Handling: Comprehensive exception handling with proper HTTP status codes
  • Health Checks: API endpoints for service health monitoring

Performance

  • Database Connection Pooling: Efficient database connection management
  • Redis Caching: In-memory caching for frequently accessed data
  • Async Operations: Non-blocking I/O for better performance

πŸš€ Deployment Architecture

Development Environment

  • Docker Compose: Multi-container development setup
  • Hot Reloading: Automatic code reloading for development
  • Volume Mounting: Source code mounted for real-time changes

Production Environment

  • Multi-stage Docker Build: Optimized production Docker images
  • Gunicorn + Uvicorn: Production ASGI server configuration
  • Environment Variables: Configuration through environment variables
  • Health Checks: Container health monitoring

Scalability Considerations

  • Horizontal Scaling: Stateless design allows for easy horizontal scaling
  • Database Connection Pooling: Efficient resource utilization
  • Caching Strategy: Redis caching reduces database load
  • Background Processing: Celery workers can be scaled independently

πŸ”§ Configuration Management

Environment-based Configuration

  • Development: Debug mode enabled, verbose logging
  • Testing: Isolated test database, mocked external services
  • Production: Optimized for performance, error monitoring enabled

Feature Flags

  • Environment Variables: Feature toggles through environment configuration
  • Runtime Configuration: Dynamic configuration changes without deployment

πŸ“ˆ Performance Optimization

Database Optimization

  • Connection Pooling: Reuse database connections for efficiency
  • Query Optimization: Efficient queries with proper indexing
  • Migration Strategy: Zero-downtime database migrations

Caching Strategy

  • Redis Integration: In-memory caching for frequent data
  • Cache Invalidation: Proper cache invalidation strategies
  • Session Management: Redis-based session storage

API Optimization

  • Response Compression: Gzip compression for API responses
  • Pagination: Efficient data pagination for large datasets
  • Rate Limiting: API rate limiting to prevent abuse

πŸ”„ Development Workflow

Code Quality Pipeline

  1. Pre-commit Hooks: Automated checks before commits
  2. Linting & Formatting: Code quality enforcement
  3. Type Checking: Static type analysis with mypy
  4. Testing: Comprehensive test suite execution
  5. Code Review: Peer review process for all changes

Continuous Integration

  • GitHub Actions: Automated CI/CD pipeline
  • Test Execution: Automated test running on pull requests
  • Code Coverage: Coverage reporting and enforcement
  • Security Scanning: Automated security vulnerability scanning

This architecture ensures a scalable, maintainable, and production-ready FastAPI application with modern development practices and comprehensive tooling.