Shared Subscriptions

Scale your RouteMQ application horizontally using MQTT shared subscriptions and worker processes to handle high-throughput message processing.

Overview

Shared subscriptions enable multiple worker processes to subscribe to the same MQTT topic, with the broker distributing messages across workers for load balancing:

  • Horizontal scaling: Add more workers to handle increased load

  • Load distribution: MQTT broker distributes messages across workers

  • High availability: If one worker fails, others continue processing

  • Per-route configuration: Different routes can have different worker counts

Basic Shared Subscription Usage

Enabling Shared Subscriptions

from core.router import Router
from app.controllers.sensor_controller import SensorController

router = Router()

# Regular subscription (single instance)
router.on("alerts/{device_id}", SensorController.handle_alert)

# Shared subscription with multiple workers
router.on("sensors/{device_id}/data", 
          SensorController.process_data,
          shared=True,           # Enable shared subscription
          worker_count=3)        # Use 3 workers for this route

# High-throughput route with many workers
router.on("telemetry/bulk", 
          SensorController.process_bulk,
          shared=True,
          worker_count=8,
          qos=1)

How Shared Subscriptions Work

Worker Management

Starting Workers

Worker Configuration

Environment Variables

Advanced Worker Configuration

Per-Route Worker Counts

Dynamic Worker Scaling

MQTT Shared Subscription Details

Subscription Topics

RouteMQ automatically handles MQTT shared subscription formatting:

Group Names

Configure shared subscription groups:

Load Balancing Algorithms

Different MQTT brokers use different load balancing strategies:

  • Round-robin: Most common, distributes messages evenly

  • Hash-based: Routes based on message characteristics

  • Least connections: Routes to worker with fewest active connections

  • Random: Randomly distributes messages

Use Cases and Patterns

High-Throughput Data Ingestion

Image and File Processing

Real-time Analytics

Order-Sensitive Processing

Worker Process Implementation

Worker Lifecycle

Worker Coordination

Monitoring and Debugging

Worker Health Monitoring

Performance Metrics

Testing Shared Subscriptions

Unit Testing

Integration Testing

Load Testing

Production Deployment

Docker Configuration

Kubernetes Deployment

Monitoring Setup

Best Practices

When to Use Shared Subscriptions

✅ Use shared subscriptions for:

  • High-throughput data processing

  • CPU-intensive operations

  • Independent message processing

  • Scalable data ingestion

  • Parallel analytics processing

❌ Avoid shared subscriptions for:

  • Order-dependent processing

  • Low-volume topics

  • State-dependent operations

  • Sequential workflows

  • Real-time control systems

Worker Count Guidelines

Resource Management

Next Steps

Last updated