How to scale self-hosted Prefect
Learn how to run multiple Prefect server instances for high availability and load distribution
Running multiple Prefect server instances enables high availability and distributes load across your infrastructure. This guide covers configuration and deployment patterns for scaling self-hosted Prefect.
Requirements
Multi-server deployments require:
- PostgreSQL database (SQLite does not support multi-server synchronization)
- Redis for event messaging
- Load balancer for API traffic distribution
Architecture
A scaled Prefect deployment typically includes:
- Multiple API server instances - Handle UI and API requests
- Background services - Runs the scheduler, automation triggers, and other loop services
- PostgreSQL database - Stores all persistent data and synchronizes state across servers
- Redis - Distributes events between services
- Load balancer - Routes traffic to healthy API instances (e.g. NGINX or Traefik)
Configuration
Database setup
Configure PostgreSQL as your database backend:
PostgreSQL is required for multi-server deployments. SQLite does not support the features needed for state synchronization across multiple servers.
Redis setup
Configure Redis as your message broker:
Each server instance automatically generates a unique consumer name to prevent message delivery conflicts.
Service separation
For optimal performance, run API servers and background services separately:
API servers (multiple instances):
Background services:
Database migrations
Disable automatic migrations in multi-server deployments:
Run migrations separately before deployment:
Load balancer configuration
Configure health checks for your load balancer:
- Health endpoint:
/api/health
- Expected response: HTTP 200 with JSON
{"status": "healthy"}
- Check interval: 5-10 seconds
Example NGINX configuration:
Reverse proxy configuration
When hosting Prefect behind a reverse proxy, ensure proper header forwarding:
UI proxy settings
When self-hosting the UI behind a proxy:
PREFECT_UI_API_URL
: Connection URL from UI to APIPREFECT_UI_SERVE_BASE
: Base URL path to serve the UIPREFECT_UI_URL
: URL for clients to access the UI
SSL certificates
For self-signed certificates:
-
Add certificate to system bundle and set:
-
Or disable verification (testing only):
Environment proxy settings
Prefect respects standard proxy environment variables:
Deployment examples
Docker Compose
Deploying Prefect self-hosted somehow else? Consider opening a PR to add your deployment pattern to this guide.
Operations
Migration considerations
Handling large databases
When running migrations on large database instances (especially where tables like events
, flow_runs
, or task_runs
can reach millions of rows), the default database timeout of 10 seconds may not be sufficient for creating indexes.
If you encounter a TimeoutError
during migrations, increase the database timeout:
For Docker deployments:
Index creation time scales with table size. A database with millions of events may require 30+ minutes for some migrations. If a migration fails due to timeout, you may need to manually clean up any partially created indexes before retrying.
Recovering from failed migrations
If a migration times out while creating indexes, you may need to manually complete it. For example, if migration 7a73514ca2d6
fails:
-
First, check which indexes were partially created:
-
Manually create the missing indexes using
CONCURRENTLY
to avoid blocking: -
Mark the migration as complete:
Only use manual recovery if increasing the timeout and retrying the migration doesn’t work. Always verify the correct migration version and index definitions from the migration files.
Monitoring
Monitor your multi-server deployment:
- Database connections: Watch for connection pool exhaustion
- Redis memory: Ensure adequate memory for message queues
- API response times: Track latency across different endpoints
- Background service lag: Monitor time between event creation and processing
Best practices
- Start with 2-3 API instances and scale based on load
- Use connection pooling to manage database connections efficiently
- Monitor extensively before scaling further (e.g. Prometheus + Grafana or Logfire)
- Test failover scenarios regularly
Further reading
- Server concepts
- Deploy Helm charts for Kubernetes