Skip to main content
When deploying FastAPI applications, you’ll often want to run multiple worker processes to take advantage of multiple CPU cores and handle more concurrent requests.

Why Multiple Workers?

Running multiple processes provides several benefits:
  • Utilize multiple CPU cores - Each worker can run on a different core
  • Handle more requests - Distribute load across workers
  • Improve fault tolerance - If one worker crashes, others continue serving requests
  • Increase throughput - Process multiple requests in parallel
During development, you typically run a single process. In production, you’ll want multiple workers for better performance.

Process and Replication

From the deployment concepts, multiple workers address:
  • Replication - Running multiple processes
  • Restarts - Process managers can restart dead workers
  • ⚠️ Security (HTTPS) - Still needs external handling
  • ⚠️ Running on Startup - Needs system configuration
  • ⚠️ Memory - Each worker consumes memory

Using Uvicorn with Workers

Uvicorn can manage multiple worker processes directly.

With FastAPI CLI

Output:

With Uvicorn Directly

1

Parent Process

One parent process (PID 27365) acts as the process manager.
2

Worker Processes

Four worker processes (PIDs 27368-27371) handle actual requests.
3

Load Balancing

The parent distributes incoming requests among workers.
4

Health Monitoring

The parent monitors workers and restarts them if they crash.

Determining Worker Count

The optimal number of workers depends on your application and hardware.

General Formula

For example:
  • 2 cores: 5 workers
  • 4 cores: 9 workers
  • 8 cores: 17 workers

Check CPU Cores

Application-Specific Considerations

I/O-Bound Applications (database queries, API calls):
  • Fewer workers needed
  • FastAPI’s async capabilities handle many concurrent requests per worker
  • Start with workers = CPU_cores
CPU-Bound Applications (data processing, image manipulation):
  • More workers beneficial
  • Use the formula: (2 * CPU_cores) + 1
Start conservatively and monitor CPU/memory usage. Adjust worker count based on actual performance metrics.

Using Gunicorn with Uvicorn Workers

Gunicorn is a mature process manager that can use Uvicorn workers.

Installation

Basic Usage

With Configuration File

Create gunicorn_conf.py:
Run with:
Gunicorn provides more advanced process management features than Uvicorn’s built-in worker support.

Advanced Gunicorn Configuration

Auto-Restart on Code Changes

For development (not production):

Worker Lifecycle Hooks

Custom Worker Management

max_requests is useful for preventing memory leaks but can impact performance. Use it cautiously.

Process Managers for Production

For production deployments, use a system-level process manager.

Systemd (Linux)

Create /etc/systemd/system/fastapi.service:
Manage the service:

Supervisor

Create /etc/supervisor/conf.d/fastapi.conf:
Manage with:
Systemd is built into most modern Linux distributions. Supervisor is useful when you need more flexibility or are on older systems.

Container Environments

Docker Compose

For Docker Compose, use workers in the container:

Kubernetes

For Kubernetes, don’t use workers. Instead, run one process per container and let Kubernetes replicate:
In Kubernetes, use one Uvicorn process per pod (no --workers). Let Kubernetes handle replication with multiple pods.

Memory Considerations

Each worker process consumes memory independently.

Example Memory Usage

If your application uses 500MB per process:
  • 1 worker: 500MB
  • 4 workers: 2GB
  • 8 workers: 4GB
Ensure your server has enough RAM:

Large Memory Objects

If loading large objects (ML models, cached data):
Each worker loads its own copy:
  • Model size: 1GB
  • 4 workers: 4GB total memory for models
Consider using fewer workers or shared memory solutions when dealing with large in-memory objects.

Load Balancing

When running multiple workers, the process manager handles load balancing automatically.

Uvicorn Workers

Uvicorn’s parent process distributes requests using the OS scheduler.

Gunicorn Workers

Gunicorn uses a pre-fork model:
  1. Master process accepts connections
  2. Connections distributed to workers
  3. Workers process requests independently

External Load Balancers

For multiple servers, use external load balancers:
  • Nginx - HTTP load balancer
  • HAProxy - TCP/HTTP load balancer
  • Traefik - Modern reverse proxy with automatic service discovery
  • Cloud Load Balancers - AWS ALB, GCP Load Balancer, Azure Load Balancer

Monitoring Workers

Using ps

Using htop

Programmatic Monitoring

Graceful Shutdown

Ensure workers shutdown gracefully to finish processing requests.

Gunicorn Configuration

FastAPI Lifespan Events

Use lifespan events for cleanup tasks like closing database connections or saving state.

Performance Tuning

Worker Timeout

Increase timeout for long-running requests:

Keep-Alive Connections

Worker Connections

For Uvicorn workers, control max connections:

Troubleshooting

Workers Dying Unexpectedly

Check logs for:
  • Memory issues: Out of memory (OOM) killer
  • Timeouts: Requests taking too long
  • Exceptions: Unhandled errors crashing workers

High CPU Usage

  • Too many workers for available cores
  • CPU-intensive operations blocking workers
  • Consider reducing worker count or optimizing code

Memory Leaks

Use max_requests to periodically restart workers:
If workers frequently die, investigate the root cause rather than just increasing max_requests.

Recap

Multiple workers improve FastAPI performance by:
  • Utilizing multiple CPU cores for better throughput
  • Handling more concurrent requests across workers
  • Providing fault tolerance if a worker crashes
  • Automatic restarts with process managers
Key decisions:
  • Single server: Use --workers with Uvicorn or Gunicorn
  • Containers (Kubernetes): One process per container, let orchestrator replicate
  • Worker count: Start with (2 * CPU_cores) + 1, adjust based on monitoring
  • Memory: Ensure enough RAM for all workers plus overhead
Monitor your application’s CPU and memory usage to find the optimal worker count for your specific workload.