Python / FastAPI Basics Interview Questions
What are the key production deployment considerations for a FastAPI application?
Production FastAPI deployments involve several layers beyond just running the app — a reverse proxy, TLS termination, process management, health checks, and observability.
| Concern | Solution |
|---|---|
| Multiple CPU cores | Gunicorn + UvicornWorker, or multiple containers |
| HTTPS / TLS | Nginx or Traefik as reverse proxy with Let's Encrypt |
| Environment secrets | Environment variables / secrets manager (Vault, AWS Secrets Manager) |
| Health checks | /health endpoint + Docker/Kubernetes probes |
| Logging | Structured JSON logs (structlog), forward to Datadog/Loki |
| Metrics | Prometheus + Grafana, or OpenTelemetry |
| Zero-downtime deploys | Rolling updates in Kubernetes, or blue/green |
| Database migrations | Run alembic upgrade head as an init container |
| Rate limiting | Nginx rate limiting or slowapi middleware |
# Health check endpoint from fastapi import FastAPI from sqlalchemy import text app = FastAPI() @app.get("/health") async def health(db: DBDep): try: await db.execute(text("SELECT 1")) db_ok = True except Exception: db_ok = False return { "status": "healthy" if db_ok else "degraded", "database": "ok" if db_ok else "error", } # Nginx config snippet for reverse proxy # server { # listen 443 ssl; # server_name api.example.com; # location / { # proxy_pass http://localhost:8000; # proxy_set_header Host $host; # proxy_set_header X-Real-IP $remote_addr; # } # }
# slowapi â rate limiting middleware # pip install slowapi from slowapi import Limiter, _rate_limit_exceeded_handler from slowapi.util import get_remote_address from slowapi.errors import RateLimitExceeded limiter = Limiter(key_func=get_remote_address) app.state.limiter = limiter app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) @app.get("/limited") @limiter.limit("10/minute") async def limited_route(request: Request): return {"message": "ok"}
More Related questions...