Platformatic Runtime Multithread Architecture
This document describes the multithread system architecture in Platformatic Runtime, which enables horizontal scaling of applications through worker threads.
Overview
Platformatic Runtime implements a sophisticated multithread architecture that allows applications to be scaled horizontally by running multiple worker instances. This system is built on Node.js Worker Threads and provides automatic load balancing, health monitoring, and fault tolerance.
Key Components
- Runtime Manager (
lib/runtime.js
): Main orchestrator managing worker lifecycle - Worker Threads (
lib/worker/main.js
): Individual application instances running in isolated threads - Round Robin Load Balancer (
lib/worker/round-robin-map.js
): Distributes requests across workers - Inter-Thread Communication (ITC) (
lib/worker/itc.js
): Message passing between runtime and workers - Thread Interceptor: Network routing and application mesh capabilities
Thread Management Architecture
Worker Creation and Lifecycle
Runtime Manager
├── Service Configuration
├── Worker Pool Management
├── Health Monitoring
└── Lifecycle Events
Worker Thread Creation Process
- Configuration: Each application can specify worker count via
workers
property - Thread Spawning: Workers are created using
new Worker(kWorkerFile, options)
- Resource Limits: Memory and CPU constraints applied per worker
- Environment Setup: Each worker gets isolated environment and dependencies
Key Configuration Options
- Global Workers: Default worker count for all applications (
config.workers
) - Service-Specific: Override via
service.workers
property - Production Mode: Multiple workers enabled only in production
- Entrypoint Services: Always use single worker for entrypoints (unless
reusePort
supported)
Worker Thread Structure
Each worker thread (lib/worker/main.js
) contains:
- Isolated Application Instance: Complete application instance
- ITC Communication: Bidirectional message channel with runtime
- Network Interceptor: Handles application mesh routing
- Telemetry: Independent monitoring and metrics collection
- Resource Monitoring: Health checks and memory/CPU tracking
Load Balancing and Request Distribution
Round Robin Implementation
The RoundRobinMap
class manages worker selection:
class RoundRobinMap {
configure(applications, defaultInstances, production) {
// Set up worker counts per application
}
next(application) {
// Return next available worker in round-robin fashion
}
}
Distribution Strategy
- Round Robin: Requests distributed evenly across available workers
- Health Awareness: Unhealthy workers excluded from rotation
- Restart Tolerance: Continues operation during worker restarts
- Graceful Degradation: Falls back to fewer workers if some fail
Network Mesh Integration
The system uses undici-thread-interceptor
for application mesh:
- Domain-based Routing: Services accessible via
.plt.local
domain - Worker Registration: Each worker registers with mesh interceptor
- Automatic Discovery: Services can communicate without explicit configuration
- Load Balancing: Requests automatically distributed across worker instances
Inter-Thread Communication (ITC)
Communication Patterns
The ITC system enables bidirectional communication between runtime and workers:
Runtime → Worker Commands
start
: Initialize and start applicationstop
: Graceful shutdowngetStatus
: Service status querygetMetrics
: Performance metrics collectiongetHealth
: Health check databuild
: Trigger application build process
Worker → Runtime Events
init
: Worker initialization completechanged
: Service file changes detected- Runtime events forwarding for management API
Message Handling
// ITC Handler Example
const itc = new ITC({
name: 'worker-name',
port: parentPort,
handlers: {
async start() {
await app.start()
return application.entrypoint ? app.getUrl() : null
},
async stop() {
await app.stop()
await dispatcher.interceptor.close()
}
}
})
Health Monitoring and Fault Tolerance
Health Check System
Each worker is continuously monitored for:
- Event Loop Utilization (ELU): Prevents thread blocking
- Memory Usage: Heap and RSS monitoring
- Response Times: Application performance tracking
- Custom Health Checks: Service-specific health indicators
Health Check Configuration
const health = {
enabled: true,
interval: 5000, // Check frequency
maxELU: 0.98, // Maximum event loop utilization
maxHeapUsed: 0.95, // Maximum heap usage percentage
maxUnhealthyChecks: 3, // Failures before replacement
gracePeriod: 30000 // Grace period after start
}