FastAPI tutorials end at uvicorn main:app --reload. Production starts where the tutorial stops.
I've deployed FastAPI to production multiple times, and every time I learn something the docs don't cover. Here are the 12 things I wish someone had told me before my first deployment.
1 Don't Use uvicorn Directly
In production, run Gunicorn with Uvicorn workers:
gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
Why: Gunicorn handles process management, graceful restarts, and worker recycling. Uvicorn alone doesn't restart workers that crash or consume too much memory.
2 Add Rate Limiting on Day One
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
limiter = Limiter(key_func=get_remote_address)
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
@app.get("/api/data")
@limiter.limit("100/minute")
async def get_data(request: Request):
...
Without rate limiting, a single bad actor can DoS your entire API. I've seen it happen on day 2 of a launch.
3 Structured Logging, Not print()
import structlog
logger = structlog.get_logger()
@app.middleware("http")
async def log_requests(request: Request, call_next):
logger.info("request_started",
method=request.method,
path=request.url.path,
client=request.client.host
)
response = await call_next(request)
logger.info("request_completed",
status_code=response.status_code
)
return response
When something breaks at 3 AM, you need JSON logs you can grep, not "processing request..." strings.
4 Health Check Endpoint
@app.get("/health")
async def health_check():
# Actually check your dependencies
try:
await db.execute("SELECT 1")
redis_ok = await redis.ping()
except Exception as e:
return JSONResponse(
status_code=503,
content={"status": "unhealthy", "error": str(e)}
)
return {"status": "healthy", "db": "ok", "redis": "ok"}
Your load balancer needs this. Your monitoring needs this. Add it before you add any business logic.
5 CORS Will Bite You
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=["https://yourdomain.com"], # NOT "*" in production
allow_methods=["GET", "POST"],
allow_headers=["Authorization"],
)
allow_origins=["*"] is fine for development. In production, it's a security hole. Specify your actual frontend domains.
6–12: The Rest of the List
The remaining items cover:
- Background tasks — when to use BackgroundTasks vs. Celery vs. ARQ
- Database connection pooling — asyncpg pool settings that actually matter
- Request validation — custom validators beyond Pydantic defaults
- Error handling — structured error responses clients can parse
- Security headers — HSTS, CSP, X-Frame-Options
- API versioning — how to version without breaking clients
- Deployment — Docker multi-stage builds that produce small images
The Complete Boilerplate
I've packaged all 12 of these patterns (and more) into a production-ready starter:
If you want the full Python stack:
- Django + HTMX Starter Kit — full-stack without JavaScript fatigue ($29)
- Modern Python Project Starter Kit — project template with testing, CI/CD, and best practices ($19)
- Full-Stack Python Toolkit Bundle — all 4 products, 49% off ($49)
What's your most painful FastAPI production lesson? Share it in the comments.