Worker Processes

RouteMQ supports horizontal scaling through MQTT shared subscriptions, allowing multiple worker processes to handle messages from the same topic for high-throughput applications.

Shared Subscriptions Overview

MQTT shared subscriptions distribute messages across multiple subscribers, enabling load balancing and horizontal scaling:

┌─────────────────┐    ┌─────────────────┐
│   MQTT Broker   │    │  Shared Group   │
│                 │    │  "workers"      │
│ Topic:          │    │                 │
│ sensors/+/data  │───▶│  ┌──────────┐   │
│                 │    │  │ Worker 1 │   │
│ Messages:       │    │  └──────────┘   │
│ [Msg1, Msg2,    │    │  ┌──────────┐   │
│  Msg3, Msg4,    │    │  │ Worker 2 │   │
│  Msg5, Msg6]    │    │  └──────────┘   │
│                 │    │  ┌──────────┐   │
│                 │    │  │ Worker 3 │   │
│                 │    │  └──────────┘   │
└─────────────────┘    └─────────────────┘
        │                       │
        │              Load Balancing:
        │              Worker 1: Msg1, Msg4
        │              Worker 2: Msg2, Msg5  
        │              Worker 3: Msg3, Msg6

  Round-robin or
  broker-specific
  distribution

Enabling Shared Subscriptions

Route Configuration

Enable shared subscriptions for specific routes:

Group Configuration

Configure the shared subscription group name:

Worker Manager

The WorkerManager handles the lifecycle of worker processes:

Starting Workers

Worker Process Management

Worker Process Architecture

Process Isolation

Each worker runs in a separate process:

Independent Router Loading

Each worker loads its own router instance:

MQTT Client per Worker

Each worker maintains its own MQTT connection:

Shared Subscription Topics

Topic Format

Shared subscriptions use the $share prefix:

Subscription Management

Workers automatically subscribe to shared topics:

Topic Extraction

Workers extract the original topic from shared subscription messages:

Load Balancing Strategies

Broker-Based Distribution

Most MQTT brokers implement round-robin distribution:

Route-Specific Worker Counts

Configure different worker counts per route:

Scaling Patterns

Auto-Scaling Based on Load

Geographic Distribution

Performance Characteristics

Throughput Benefits

Latency Considerations

  • Process Startup: Initial latency for worker process creation

  • Message Distribution: Small overhead for shared subscription routing

  • Context Switching: Minimal impact due to process isolation

Memory Usage

Configuration and Deployment

Environment Configuration

Docker Deployment

Kubernetes Scaling

Monitoring and Observability

Worker Health Monitoring

Metrics Collection

Best Practices

When to Use Shared Subscriptions

Use for:

  • High-throughput topics (>100 messages/second)

  • CPU-intensive processing

  • I/O-bound operations that can be parallelized

  • Bulk data processing

Don't use for:

  • Low-frequency topics (<10 messages/second)

  • Order-dependent processing

  • Stateful operations requiring message sequence

  • Simple, fast operations (<1ms processing time)

Worker Count Guidelines

Resource Management

  1. Memory: Monitor memory usage per worker

  2. CPU: Ensure CPU cores available for workers

  3. Connections: Limit database connections per worker

  4. File Handles: Monitor open file descriptors

Error Handling

Troubleshooting

Common Issues

Workers not receiving messages:

Uneven load distribution:

High memory usage:

Debug Mode

Next Steps

Last updated