Answer
A production FastAPI deployment typically involves:
- ASGI server - Uvicorn is the standard. For multi-core utilization, run Uvicorn workers behind Gunicorn:
gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker
- Reverse proxy - Nginx or Caddy in front for SSL termination, static files, load balancing, and request buffering.
- Docker - Containerize the app with a multi-stage Dockerfile. Use a slim Python image.
- Process management - Use Docker's restart policy, systemd, or supervisord to keep the app running.
- HTTPS - Terminate SSL at the reverse proxy level (Let's Encrypt with Certbot or Caddy's auto-SSL).
Cloud options: AWS ECS/Fargate, Google Cloud Run, Azure Container Apps, Railway, Render, or traditional VPS with Docker Compose.
Code examples
Production Dockerfile
# Dockerfile
FROM python:3.12-slim AS base
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
# Run with Uvicorn
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"]
# Or with Gunicorn + Uvicorn workers (recommended)
# CMD ["gunicorn", "main:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker",
# "--bind", "0.0.0.0:8000"]
Key points
- Use `--workers` based on CPU cores: `2 * num_cores + 1` is a common formula
- Never run with `--reload` in production - it's for development only
- Set `--proxy-headers` and `--forwarded-allow-ips` when behind a reverse proxy
Common follow-up questions
- How do you handle zero-downtime deployments?
- What is the difference between Uvicorn workers and Gunicorn workers?
Interview tips
- Mention the Gunicorn + Uvicorn worker pattern - it's the gold standard for production FastAPI