Sentry Answers>FastAPI>

Number of Uvicorn workers needed in production

Number of Uvicorn workers needed in production

David Y.

The Problem

How many Uvicorn workers should I use for a production deployment of a Python web service? How does the number of workers relate to the number of CPU cores I have, and what are the benefits of running additional workers?

The Solution

The recommended baseline number of Uvicorn workers is one per thread plus one. Modern CPUs generally use hyperthreading and each CPU core will have two threads. Therefore, a 4-core server should be running 9 workers, an 8-core 17 workers, and a 16-core 33 workers, according to the formula below:

Click to Copy
number of workers = number of cores * number of threads + 1

This gives each core one worker to read and write to and from the network socket and another to process requests. Make sure not to confuse the physical number of CPU cores with the system’s vCPUs (i.e. its threads) or you may start up too many.

However, the optimal number of workers will depend on the nature of the service and the hardware it’s running on, as well as how it is used. A good way to figure out the optimal number of workers for a particular service is to increase and decrease the number of workers while it’s operating under load, using the TTIN and TTOU signals and noting the points at which performance is enhanced and degraded.

Starting up too many workers can cause the server to run out of resources, which will slow down all operations.

Note that this guidance applies to both the asynchronous Uvicorn and the synchronous Gunicorn.

  • Syntax.fmListen to the Syntax Podcast
  • Community SeriesIdentify, Trace, and Fix Endpoint Regression Issues
  • ResourcesBackend Error Monitoring 101
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

    SEE EPISODES

Considered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.