Playbooks
This section translates the earlier theory into practical service-design rules. Knowing Pythonic patterns, typing, runtime behavior, asyncio, FastAPI, Pydantic, and SQLAlchemy independently is useful, but what matters in production is how those pieces fit together without collapsing into one giant layer.
Quick takeaway: a playbook is not a summary page. It is a decision aid. You should be able to use these chapters when starting a service, reviewing code, or refactoring a codebase that has begun to tangle its boundaries.
The Questions This Part Answers
How should a new service start?
It proposes a reference layout for routers, services, repositories, schemas, settings, logging, and tests.
Where should a refactor start?
It connects recurring smells such as fat routes, hidden commits, and DTO/ORM collapse to one atlas plus focused refactoring series.
How should configuration be designed?
It explains how to split `settings.py`, `pydantic-settings`, `.env`, secret sources, and test overrides into a clean configuration boundary.
How do FastAPI, Pydantic, and SQLAlchemy stay decoupled?
It separates request DTOs, transactions, ORM entities, and response DTOs so the API can evolve cleanly.
What should type reviews focus on?
It prioritizes `Any` containment, abstraction discipline, protocol usage, and boundary clarity over annotation volume.
How should fixture design work?
It covers yield fixtures, dependency override cleanup, database isolation, and client lifecycles as real service-testing patterns.
What comes after fixtures?
It explains how to add HTTP contract tests, websocket protocol tests, property-based tests, and idempotency invariants on top.
How do you choose Lambda vs Kubernetes?
It compares traffic shape, database connection strategy, long-lived connections, and operational surface area instead of treating hosting as an afterthought.
How do schema changes stay safe during progressive delivery?
It explains ordering for Alembic, backfill, and feature flags across rolling, blue-green, canary, and Lambda alias rollouts.
How do schema, API, and event contracts evolve together?
It brings DB migrations, API versioning, event versioning, and backfill or replay into one contract-evolution frame.
How do retries and duplicate delivery stay safe?
It connects idempotency keys, outbox design, publisher retry, and dedupe into one operational picture.
Where does theory meet service design?
It connects typing, runtime, and framework knowledge to concrete API architecture decisions.
Recommended Reading Order
- Refactoring Atlas
- API Service Template
- Refactoring Fat Routes and Dependency Leakage
- Settings and Pydantic Settings
- ABC + Fake UoW Testing
- Use Case + UoW + ABC
- Refactoring Session Ownership and Hidden Commits
- FastAPI + Pydantic + SQLAlchemy
- Refactoring DTO Boundaries and Over-Abstraction
- Testing with Fixtures
- Testing Beyond Fixtures
- Lambda vs Kubernetes
- Progressive Delivery + Alembic
- Contract Evolution and Sustainable CD
- Idempotency and Outbox
- Typing Review Checklist
How to Read This Part in Review Mode
- Key playbooks now end with
Code Review Lens,Common Anti-Patterns,Likely Discussion Questions, andStrong Answer Frame. API Service TemplateandUse Case + UoW + ABCnow includesub-optimal -> improvedexamples that show concrete refactoring direction.Refactoring Atlasworks as a fast smell index, while the refactoring series pages teach each smell in a deeper narrative form.Lambda vs KubernetesandProgressive Delivery + Alembicnow include symptom-first scenario tables for operational judgment.
How to Use This Part
- Start with
Refactoring Atlasif you want to classify the smell before choosing a deeper chapter. - Start with
Refactoring Fat Routes and Dependency Leakageif route and DI boundaries are tangled. - Start with
API Service Templatewhen creating a new service skeleton. - Start with
Settings and Pydantic Settingsif you need a clean config boundary for env vars, secrets, and test overrides. - Start with
Testing with Fixturesif you need a clean fixture and teardown baseline for service tests. - Start with
Testing Beyond Fixturesif you want to add contract, property-based, and protocol testing on top of that baseline. - Start with
ABC + Fake UoW Testingif you want fast unit tests around use-case branching without touching a real database. - Start with
Use Case + UoW + ABCif you want a concrete SOLID-aware use-case boundary with SQLAlchemy and explicit abstract base classes. - Start with
Refactoring Session Ownership and Hidden Commitsif hidden commits or split session ownership are the main smell. - Start with
FastAPI + Pydantic + SQLAlchemyif you already have a service and want cleaner boundaries. - Start with
Refactoring DTO Boundaries and Over-Abstractionif DTO/ORM collapse or low-value abstractions are the main problem. - Start with
Lambda vs Kubernetesif the hosting decision is still open and you need a workload-driven rubric. - Start with
Progressive Delivery + Alembicif you need a safe order for schema changes, backfill jobs, and rollout promotion under rolling, blue-green, canary, or Lambda alias deployment. - Start with
Contract Evolution and Sustainable CDif you need one mental model for DB schema, public API, async events, and historical data migration. - Start with
Idempotency and Outboxif retry-safe create endpoints and reliable publication are becoming production concerns. - Use
Typing Review Checklistas a team review baseline.