Python / FastAPI 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"}
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
