This implementation creates multiple MediaSoup workers to distribute room load across CPU cores, improving performance and scalability.
- Configurable worker count via
MEDIASOUP_WORKER_COUNT(default: 4) - Each worker gets isolated RTC port ranges (1000 ports per worker)
- Worker 0: 10000-10999, Worker 1: 11000-11999, etc.
- Room Count Priority: Distributes rooms evenly across workers
- CPU Usage Monitoring: Considers worker CPU usage when selecting
- Last Used Time: Round-robin for workers with similar load
- Real-time CPU usage tracking per worker
- Room count tracking per worker
- Worker health monitoring
GET /stats- Complete system overview with room distributionGET /workers- Worker-specific statistics
# Number of MediaSoup workers (default: 4)
MEDIASOUP_WORKER_COUNT=4
# Base IP for WebRTC (default: 127.0.0.1)
WEBRTC_LISTEN_IP=127.0.0.1
# Announced IP for external connections
ANNOUNCED_IP=your.public.ipEach worker gets a dedicated port range:
- Worker 0: 10000-10999
- Worker 1: 11000-11999
- Worker 2: 12000-12999
- Worker 3: 13000-13999
When creating a new room, the system:
- Checks room count - Selects worker with fewest rooms
- Evaluates CPU usage - If room counts are equal, chooses worker with lower CPU
- Uses round-robin - If CPU usage is similar, uses least recently used worker
- CPU Distribution: Spreads load across multiple cores
- Memory Isolation: Each worker has isolated memory space
- Better Fault Tolerance: One worker failure doesn't affect others
- Horizontal Scaling: Can easily increase worker count
- Resource Monitoring: Real-time visibility into system load
- Dynamic Load Balancing: Automatically adapts to changing conditions
# Get complete system overview
curl http://localhost:3000/stats
# Get worker-specific information
curl http://localhost:3000/workers{
"workers": [
{
"workerId": 0,
"roomCount": 3,
"cpuUsage": 0.25,
"pid": 12345,
"closed": false
}
],
"distribution": {
"0": ["room1", "room2", "room3"],
"1": ["room4", "room5"]
},
"totalRooms": 5
}-
services/mediasoup.ts- Multi-worker initialization
- Load balancing logic
- Resource monitoring
-
services/room.ts- Worker-aware room creation
- Room-to-worker mapping
- Cleanup handling
-
index.ts- Monitoring endpoints
- Statistics API
initMediasoup()- Initializes all workersgetLeastLoadedWorker()- Selects optimal workergetWorkerStats()- Returns worker statisticsgetRoomDistribution()- Shows room distribution across workers
- Set worker count to match CPU cores:
MEDIASOUP_WORKER_COUNT=8 - Configure firewall for port ranges: 10000-18000
- Monitor worker health using
/statsendpoint - Use process manager (PM2) for automatic restarts
- Small servers (2-4 cores): 2-4 workers
- Medium servers (8-16 cores): 4-8 workers
- Large servers (32+ cores): 8-16 workers
Ensure firewall allows:
- TCP ports: Base port range (10000 + worker_count * 1000)
- UDP ports: Same range as TCP
- HTTP/WebSocket: 3000 (or configured PORT)
This implementation provides a robust foundation for scaling MediaSoup applications across multiple CPU cores while maintaining optimal performance and reliability.