tezvyn:

From Dev Server to Production: Running FastAPI with Workers

AI-drafted, machine-checkedSource: fastapi.tiangolo.comintermediate

Your dev server is a single process. For production, you need a process manager to run multiple Uvicorn worker processes, handling concurrent requests and providing fault tolerance.

WHY IT EXISTS Deploying an application means making it available with high performance and stability. The simple, single-process server used for development is designed for convenience, not for handling real-world traffic reliably. A multi-process architecture is required to serve users efficiently and without interruption.

THE MENTAL MODEL Think of a single chef versus a full kitchen staff. The development server is a single chef who can only cook one order at a time. For production, you need a head chef (a process manager like Gunicorn) managing a team of line cooks (Uvicorn workers). This kitchen can handle many orders at once, and if one cook gets overwhelmed, the others keep the food coming out while the head chef handles the issue.

HOW IT WORKS A production setup uses a process manager to run multiple Uvicorn worker processes. The manager starts up and, instead of running your app code itself, it launches and supervises several worker processes. Each worker is an independent Uvicorn server running your FastAPI app. The manager distributes incoming HTTP requests among the available workers, allowing your application to process multiple requests in parallel on different CPU cores. It also monitors worker health, automatically restarting any that crash, which provides resilience.

WHEN TO USE IT This manager-worker model is the standard for any FastAPI application in a production environment. Use it when deploying to a virtual machine, a Docker container, or a Platform-as-a-Service (PaaS). It is the correct way to ensure your API is performant and stable for your users.

WHEN NOT TO USE IT The single-process development server (e.g., uvicorn main:app --reload) is perfectly suited for local development. Its simplicity and features like hot-reloading are ideal for the stage where you are constantly changing and testing code. It is not built for the performance or stability demands of a live application.

ONE CANONICAL EXAMPLE While a simple uvicorn command starts the dev server, a production deployment often uses Gunicorn to manage Uvicorn workers. A typical command is gunicorn -w 4 -k uvicorn.workers.UvicornWorker my_app.main:app. This tells Gunicorn to start 4 worker processes, using Uvicorn's worker class to run the FastAPI app instance found in my_app/main.py.

Read the original → fastapi.tiangolo.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.