Appearance
FastAPI in Production
36. How do you structure a large FastAPI project?
The short answer
Organise by feature, not by file type, so everything about orders lives together. Keep the web layer thin: routers translate HTTP to calls on services, which hold business rules and call repositories for data. Keep Pydantic schemas separate from ORM models. Put shared dependencies, such as the session, current user and settings, in one deps.py, and create the app, middleware, routers and lifespan in one main.py. Declare the entry point in pyproject.toml so fastapi dev and fastapi run find it.
text
shop/
pyproject.toml [tool.fastapi] entrypoint = "shop.main:app"
shop/
main.py app = FastAPI(lifespan=…); include routers; middleware
config.py Settings with pydantic-settings
deps.py Session, CurrentUser, SettingsDep
db.py engine, SessionLocal, Base
orders/
router.py APIRouter(prefix="/orders"); thin endpoints
schemas.py OrderIn, OrderOut (Pydantic)
models.py Order, OrderItem (SQLAlchemy)
service.py place_order, cancel_order: business rules
repository.py queries
payments/
…
migrations/ AlembicWhy this shape: a change to orders touches one folder; services can be called from an endpoint, a queue worker or a CLI command; and endpoints stay readable because validation, auth and sessions arrive through dependencies. It is the layering described in service layers.
37. How do you run and deploy a FastAPI application?
The short answer
Locally, fastapi dev runs Uvicorn with auto-reload. In production, fastapi run, or uvicorn shop.main:app, without reload, with --workers N on a VM, or one worker per container with several replicas on Kubernetes. Package it in a slim Docker image with dependencies installed from a lock file using uv, run as a non-root user, behind a reverse proxy or load balancer that terminates TLS, with --proxy-headers and the proxy's addresses trusted so client IPs and HTTPS are seen correctly. Add readiness and liveness endpoints and rely on Uvicorn's graceful shutdown on SIGTERM.
dockerfile
FROM python:3.14-slim
COPY --from=ghcr.io/astral-sh/uv:0.11 /uv /usr/local/bin/uv
WORKDIR /app
ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-dev --no-install-project # (1)
COPY shop ./shop
RUN uv sync --frozen --no-dev
RUN useradd --create-home app
USER app # (2)
EXPOSE 8000
CMD ["/app/.venv/bin/fastapi", "run", "--port", "8000", "--proxy-headers"] # (3)- Dependencies first, from the lock file, so this layer is cached until
uv.lockchanges. - Not root.
- Exec form, so
SIGTERMreaches the server and in-flight requests finish.--proxy-headerstrustsX-Forwarded-ForandX-Forwarded-Protofrom the proxy, which must be the only thing that can reach the container.
| Environment | Command |
|---|---|
| Development | fastapi dev |
| VM, 4 cores | fastapi run --workers 4 |
| Kubernetes | fastapi run with one worker; scale replicas; probes on /health/live and /health/ready |
Health checks and graceful shutdown work as for any service (readiness and liveness). The uv image tag above is an example; pin the exact version you use.
38. How do you handle configuration and secrets?
The short answer
Read all configuration through a pydantic-settings class, validated at startup, from environment variables, with secrets typed as SecretStr. Inject secrets at runtime from the platform's secret store or a vault, never bake them into the image or commit a .env. Expose settings through a cached dependency so each environment supplies its own values, and keep non-secret per-environment values, such as feature flags and URLs, in deployment configuration in version control.
The settings pattern with code is in pydantic-settings; secret handling in production is in secrets in production.
39. How do you add logging, metrics and tracing to FastAPI?
The short answer
Log structured JSON to stdout with a request ID and trace ID on every line, using structlog or the standard logging module with a JSON formatter, configured once at startup. For traces and metrics, recent FastAPI versions have native OpenTelemetry support: install fastapi[standard], set FASTAPI_OTEL_AUTO_CONFIGURE=true, OTEL_SERVICE_NAME and OTEL_EXPORTER_OTLP_ENDPOINT, and requests are traced and measured, with database and HTTP-client spans added by their OpenTelemetry instrumentations. Watch p95 and p99 latency per route, error rate and event-loop lag.
bash
FASTAPI_OTEL_AUTO_CONFIGURE=true \
OTEL_SERVICE_NAME=shop-api \
OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318 \
fastapi runpython
import logging, structlog
structlog.configure(
processors=[
structlog.contextvars.merge_contextvars, # (1)
structlog.processors.add_log_level,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.JSONRenderer(),
],
)
log = structlog.get_logger()
@app.middleware("http")
async def bind_request(request: Request, call_next):
structlog.contextvars.bind_contextvars(request_id=request.headers.get("x-request-id", str(uuid.uuid4())))
try:
return await call_next(request)
finally:
structlog.contextvars.clear_contextvars() # (2)
log.info("order placed", order_id=8812, total_pence=8550)
# {"order_id": 8812, "total_pence": 8550, "event": "order placed", "request_id": "…", "level": "info", "timestamp": "…"}- Context variables carry the request ID into every log line written during the request, even from deep inside services.
- Clear them afterwards, so one request's IDs do not leak into the next.
The concepts are the same as for Node services (observability).
40. How do you add rate limiting and API versioning to FastAPI?
The short answer
For rate limiting, enforce coarse per-IP and per-key limits at the gateway or proxy, and per-user or per-route limits in the app with a dependency backed by Redis, so all workers and replicas share counters; libraries such as slowapi or fastapi-limiter do this. Return 429 with Retry-After. For versioning, put the version in the path, /v1/orders, using one APIRouter per version, change versions only for breaking changes, keep additive changes within a version, and announce deprecations with dates.
python
async def rate_limit(request: Request, user: CurrentUser) -> None:
key = f"rl:{user.id}:{request.url.path}"
count = await redis.incr(key)
if count == 1:
await redis.expire(key, 60)
if count > 100:
raise HTTPException(status.HTTP_429_TOO_MANY_REQUESTS, "Too many requests", headers={"Retry-After": "60"})
v1 = APIRouter(prefix="/v1", dependencies=[Depends(rate_limit)])
v1.include_router(orders.router)
app.include_router(v1)The counting algorithms and their trade-offs are in a distributed rate limiter; versioning strategies in API versioning.
41. How does FastAPI compare with Django and Flask?
The short answer
FastAPI is API-first and async, with validation, serialisation and OpenAPI docs driven by type hints; you choose the database, admin and auth libraries yourself. Django is "batteries included": ORM, migrations, admin site, auth, forms and templates, ideal for full web applications and back-offices, with Django REST Framework or Django Ninja for APIs and growing async support. Flask is minimal and synchronous by default, flexible and simple, with extensions for everything. Choose FastAPI for typed APIs and high-concurrency I/O, Django for a full product with an admin, and Flask for small, simple services.
| FastAPI | Django | Flask | |
|---|---|---|---|
| Style | API-first, typed | Full-stack, batteries included | Micro-framework |
| Interface | ASGI, async-native | WSGI and ASGI | WSGI, some async |
| Validation | Pydantic, from type hints | Forms, DRF serializers | Extensions |
| ORM | Your choice: SQLAlchemy, SQLModel | Django ORM built in | Your choice |
| Admin UI | No | Yes | Extensions |
| API docs | Automatic OpenAPI | Via DRF or Ninja | Extensions |
| Best for | JSON APIs, microservices, ML serving | Content-heavy apps, back-offices | Small apps, prototypes |
42. What are the most common FastAPI mistakes?
The short answer
The ones interviewers listen for: calling blocking code in async def endpoints; creating a new database engine or HTTP client per request instead of in the lifespan; one model for input and output, leaking fields or allowing mass assignment; no return type or response model, so internal fields leak and serialisation is slower; lazy-loading relationships in async SQLAlchemy; running Alembic on startup in every worker; using BackgroundTasks for work that must not be lost; mutable global state shared between requests; and trusting X-Forwarded-* from anyone.
| Mistake | Fix |
|---|---|
requests.get inside async def | httpx.AsyncClient, or a plain def endpoint (async versus def) |
| Engine or client created per request | Create once in the lifespan (lifespan) |
| Same model in and out | OrderIn, OrderUpdate, OrderOut |
| No return type | -> OrderOut or response_model= |
| Lazy loads in async | selectinload (N+1) |
| Migrations on startup | A separate deploy step |
Critical jobs in BackgroundTasks | A task queue (background tasks) |
| Module-level mutable caches | Redis, or lru_cache only for immutable data |
--proxy-headers with the port open to the internet | Only the proxy may reach the app |
Using ... defaults and RootModel everywhere | Plain defaults and Annotated types |