ApeRAG icon indicating copy to clipboard operation
ApeRAG copied to clipboard

[Improvement] Improve Production Deployment by Using Gunicorn as a Process Manager for Uvicorn Workers

Open iziang opened this issue 5 months ago • 0 comments

Is your feature request related to a problem? Please describe. Currently, our FastAPI application is deployed for production using a single, direct uvicorn command. While this is straightforward and works for development, it's not optimal for a production environment. A single process cannot leverage multi-core CPUs, and there is no automatic process recovery if the worker crashes, leading to potential downtime and underutilization of server resources.

Describe the solution you'd like We should adopt the officially recommended deployment strategy for Uvicorn in production by using Gunicorn as a process manager to run multiple Uvicorn worker processes. This involves:

  1. Adding gunicorn as a project dependency.
  2. Updating our deployment scripts (e.g., Dockerfile, run.sh, or systemd service file) to use Gunicorn to launch the application.

The command would look similar to this:

gunicorn -w 4 -k uvicorn.workers.UvicornWorker my_app.main:app
  • -w 4: Specifies the number of worker processes. This should be configured based on the server's CPU cores (a common recommendation is (2 * number_of_cores) + 1).
  • -k uvicorn.workers.UvicornWorker: Tells Gunicorn to use Uvicorn's high-performance worker class.

Describe alternatives you've considered We could continue using Uvicorn directly with its built-in --workers flag. However, Gunicorn is a more mature and battle-tested process manager for the Python ecosystem, offering robust features for logging, signal handling, and graceful reloads, making it a more standard and reliable choice for production.

Additional context This improvement aligns with the best practices outlined in the official Uvicorn documentation. The documentation explicitly states: "Run gunicorn -k uvicorn.workers.UvicornWorker for production." This change will lead to:

  • Improved Performance: Better utilization of multi-core processors by running multiple worker processes.
  • Increased Reliability: Gunicorn will automatically restart worker processes that die unexpectedly, improving the application's fault tolerance.
  • Zero-Downtime Deployments: Gunicorn's graceful restart mechanism allows for application updates without dropping client connections.

Reference:

  • Uvicorn Deployment Documentation: https://www.uvicorn.org/deployment/

iziang avatar Jul 12 '25 15:07 iziang