A comprehensive learning repository covering essential backend development concepts with practical FastAPI implementations. This repository is organized into modular sections, each focusing on specific backend patterns and technologies.
- Python 3.8+
- Docker & Docker Compose (for services like PostgreSQL, Redis, Kafka)
- pip package manager
- Clone the repository:
git clone <repository-url>
cd backend_learning- Each module has its own
requirements.txt. Navigate to the module and install dependencies:
cd <module-name>
pip install -r requirements.txt- Run individual modules:
# For simple modules
python main.py
# For modules with Docker services
docker-compose up
# In another terminal:
python <script-name>.pybackend_learning/
βββ api_versioning/ # API version management patterns
βββ auth_api/ # Authentication & JWT implementation
βββ crud_api/ # Complete CRUD operations with auth
βββ dependency_injection/ # FastAPI dependency injection patterns
βββ Error_handling/ # Error handling best practices
βββ filtering/ # Query filtering techniques
βββ kafka/ # Event-driven architecture with Kafka
βββ middleware/ # Custom middleware implementation
βββ pagination/ # Pagination strategies
βββ payment_and_subscription/# Payment processing & subscriptions
βββ postgre_sql/ # SQL database integration with SQLAlchemy
βββ pydantic/ # Data validation with Pydantic
βββ redis/ # Caching & rate limiting with Redis
βββ README.md # This file
What you'll learn: Basic authentication flow with FastAPI
Key files:
app/main.py- FastAPI application setupapp/auth.py- Authentication routerapp/schemas.py- Request/response validationapp/security.py- Password hashing and JWT utilities
Key concepts:
- User registration and login
- Password hashing (bcrypt)
- Basic JWT token generation
- Protected routes with token validation
How to run:
cd auth_api
pip install -r requirements.txt
python -m uvicorn app.main:app --reload
# Visit http://localhost:8000/docsWhat you'll learn: SQL database setup, SQLAlchemy ORM, and database operations
Key files:
models.py- SQLAlchemy ORM modelsdatabase.py- Database connection and session managementschemas.py- Pydantic schemas for validationmain.py- CRUD endpoints
Key concepts:
- Database connection pooling
- ORM model definitions
- Transaction management
- Foreign keys and relationships
- Error handling for database operations
How to run:
cd postgre_sql
docker-compose up # Starts PostgreSQL
python main.py # In another terminalWhat you'll learn: Combining authentication with full CRUD operations on a database
Key files:
models.py- Database modelscrud.py- CRUD operationsdependencies.py- Dependency injection helperssecurity.py- JWT and auth utilitiesmain.py- API endpoints
Key concepts:
- Secure CRUD operations
- Role-based access control (RBAC)
- Dependency injection for database sessions
- Protected endpoints with user context
How to run:
cd crud_api
docker-compose up
pip install -r requirements.txt
python -m uvicorn app.main:app --reloadWhat you'll learn: Input validation, serialization, and schema definition with Pydantic
Key concepts:
- Request/response models
- Field validation (min, max, regex patterns)
- Custom validators
- Nested models
- JSON schema generation
What you'll learn: Implementing offset-limit pagination for large datasets
Key files:
main.py- Pagination endpoint
Example:
# Endpoint: GET /users?page=1&limit=10
# Returns:
{
"page": 1,
"limit": 10,
"total": 100,
"next_page": 2,
"prev_page": null,
"data": [...]
}Key concepts:
- Offset-limit pagination
- Next/previous page calculation
- Query parameter validation with
Query
What you'll learn: Implementing dynamic filtering on API endpoints
Key concepts:
- String search/contains filtering
- Case-insensitive matching
- Multiple field filtering
- Combining filtering with pagination
What you'll learn: Maintaining multiple API versions simultaneously
Key files:
v1_offset_pagination.py- Version 1 with offset paginationv2.py- Version 2 with potentially different schema
Key concepts:
- URL-based versioning (
/api/v1/,/api/v2/) - Using APIRouter with prefix
- Backward compatibility
- Gradual API evolution
Example:
router_v1 = APIRouter(prefix="/api/v1", tags=["v1"])
# Endpoints registered under /api/v1/*What you'll learn: Implementing middleware for cross-cutting concerns
Key files:
main.py- Custom middleware implementation
Key concepts:
- Request/response interception
- Rate limiting per client IP
- Request logging
- Execution time tracking
- Custom headers manipulation
What you'll learn: Performance optimization with Redis
Key files:
cache.py- Cache-aside pattern implementationrate_limiter.py- Rate limiting logicredis_client.py- Redis connection managementmain.py- API with caching and rate limiting
Key concepts:
- Cache-aside pattern (lazy loading)
- Cache invalidation strategies
- Rate limiting by client IP
- HTTP caching headers
- Redis data expiration (TTL)
How to run:
cd redis
docker-compose up
pip install -r requirements.txt
python -m uvicorn main:app --reloadWhat you'll learn: Advanced dependency injection in FastAPI
Key concepts:
- Constructor-based injection
- Function parameter injection
- Dependency hierarchy
- Service locator pattern
- Testing with mock dependencies
What you'll learn: Comprehensive error handling strategies
Key concepts:
- HTTP exception mapping
- Custom exception classes
- Error response formatting
- Validation error handling
- Logging and error tracking
What you'll learn: Asynchronous event processing with message queues
Key files:
order_service.py- Kafka producer for order eventsemail_service.py- Kafka consumer for email processing
Key concepts:
- Publish-subscribe pattern
- Event serialization (JSON)
- Asynchronous processing
- Multiple consumers
- Message ordering guarantees
How to run:
cd kafka
docker-compose up # Start Kafka and Zookeeper
python order_service.py & # Producer
python email_service.py # Consumer
# Send POST request to producer to trigger eventsWhat you'll learn: Implementing subscription and payment workflows
Key files:
models.py- Payment and subscription modelsdatabase.py- Database setuprabbitmq.py- Message queue integrationworker.py- Background job processormain.py- API endpoints
Key concepts:
- Subscription state management
- Payment processing integration
- Background task queuing (RabbitMQ)
- Worker pattern for async jobs
- Webhook handling for payment updates
How to run:
cd payment_and_subscription
docker-compose up
python create_tables.py # Initialize database
python -m uvicorn main:app --reload # API server
python worker.py # Background worker (separate terminal)-
Start here:
pagination/- Understand API pagination basicsfiltering/- Add filtering to queriespydantic/- Master data validation
-
Build on the foundation:
auth_api/- Implement authenticationpostgre_sql/- Work with databasesmiddleware/- Handle cross-cutting concerns
-
Combine concepts:
crud_api/- Full CRUD with authapi_versioning/- Manage API evolutionError_handling/- Production-ready error handling
-
Advanced patterns:
redis/- Performance optimizationkafka/- Event-driven systemspayment_and_subscription/- Complex workflows
Several modules use Docker Compose for external services. Common services include:
- PostgreSQL - SQL database
- Redis - In-memory cache and rate limiting
- Kafka - Message broker
- RabbitMQ - Task queue
# Start services in background
docker-compose up -d
# View logs
docker-compose logs -f
# Stop services
docker-compose down
# Remove volumes (WARNING: deletes data)
docker-compose down -vUsing curl or FastAPI's built-in Swagger UI:
# Start any service and navigate to:
http://localhost:8000/docs
# Or use curl:
curl http://localhost:8000/users?page=1&limit=10- FastAPI: Modern, fast web framework with automatic API documentation
- SQLAlchemy: Powerful ORM for database operations
- Pydantic: Type-safe data validation
- Docker: Containerization for consistent environments
- Async/Await: Python's asynchronous programming model
- Middleware: Request/response interception and modification
- Message Queues: Decoupling services (Kafka, RabbitMQ)
- Caching: Performance optimization with Redis
- Authentication: Secure user identity management with JWT
- Each module is self-contained and can be run independently
- Remember to install dependencies before running each module
- Docker services require Docker and Docker Compose to be installed
- FastAPI automatically generates API documentation at
/docs(Swagger UI) - Use
--reloadflag when running with uvicorn for development
As you progress through this learning repository, feel free to:
- Extend existing modules with new features
- Add comments explaining key concepts
- Create additional examples
- Document any issues or improvements
Happy learning! π