Skip to content
advanced Phase 80 · Scaling Advanced

Queue Scaling

Queue scaling with multiple consumers, queue partitioning, message throughput, and backpressure mechanisms

45m
0 problems
Topic Progress 0%

Multiple Consumer Configuration

Consumer Scaling Architecture

┌─────────────────────────────────────────┐
│            Message Queue (RabbitMQ)      │
│  ┌─────────┐ ┌─────────┐ ┌─────────┐  │
│  │ Queue A │ │ Queue B │ │ Queue C │  │
│  └────┬────┘ └────┬────┘ └────┬────┘  │
└───────┼───────────┼───────────┼────────┘
        │           │           │
   ┌────┴────┐ ┌────┴────┐ ┌────┴────┐
   │Consumer1│ │Consumer2│ │Consumer3│
   │Consumer2│ │Consumer3│ │Consumer4│
   └─────────┘ └─────────┘ └─────────┘

Magento Queue Consumer Config

// app/etc/env.php
'queue' => [
    'consumers' => [
        'product.action.update' => [
            'maxMessages' => 1000,
            'consumer' => 'product.action.update'
        ],
        'codegeneratorProcessor' => [
            'maxMessages' => 100
        ]
    ]
],

Running Multiple Consumers

# Start multiple consumer processes
bin/magento queue:consumers:start product.action.update --max-messages=1000 &
bin/magento queue:consumers:start product.action.update --max-messages=1000 &
bin/magento queue:consumers:start product.action.update --max-messages=1000 &

# Or use supervisor
# /etc/supervisor/conf.d/magento-consumer.conf
[program:magento-consumer-product]
command=bin/magento queue:consumers:start product.action.update --max-messages=1000
process_name=%(program_name)s_%(process_num)02d
numprocs=3
autostart=true
autorestart=true

Queue Partitioning

Partitioning Strategies

Strategy        | Description              | Use Case
────────────────|──────────────────────────|──────────────
Round Robin     | Messages rotated         | Equal processing
Hash-based      | Hash(key) % partitions   | Ordered processing
Priority        | Separate priority queues  | Critical messages
Topic           | Separate topics per type | Different workloads

Topic-Based Partitioning

Topic: catalog.update
├── Partition 0: Product updates
├── Partition 1: Category updates
└── Partition 2: Attribute updates

Each partition consumed independently
Enables parallel processing per type

Hash-Based Partitioning

// Partition by product ID for ordered processing
$productId = $message->getProductId();
$partitionId = $productId % $totalPartitions;

// Route to specific queue
$queueName = 'catalog.update.partition.' . $partitionId;
$publisher->publish($queueName, $message);

Message Throughput Optimization

Throughput Configuration

# RabbitMQ tuning
# Increase prefetch count for batch processing
rabbitmqctl set_policy prefetch-count 50 "^magento" '{"prefetch-count": 50}'

# Increase memory limit
rabbitmqctl set_vm_memory_high_watermark 0.6

# Disk free limit
rabbitmqctl set_disk_free_limit 2GB

Batch Processing

// Process messages in batches
$batchSize = 50;
$messages = [];

while ($message = $queue->dequeue()) {
    $messages[] = $message;
    
    if (count($messages) >= $batchSize) {
        $this->processBatch($messages);
        $messages = [];
    }
}

// Process remaining
if (!empty($messages)) {
    $this->processBatch($messages);
}

Performance Metrics

Metric                | Target
──────────────────────|────────────
Messages/sec          | >1000
Queue depth           | <10000
Consumer lag          | <100 messages
Processing latency    | <100ms
Ack time              | <50ms

Backpressure Mechanisms

Backpressure Strategies

1. Queue Depth Limit: Reject when queue full
2. Rate Limiting: Throttle message intake
3. Consumer Scaling: Auto-scale consumers
4. Circuit Breaker: Stop intake on failure

Queue Depth Alerts

# Alert if queue depth > 10000
rabbitmqctl list_queues name messages | while read queue msgs; do
    if [ $msgs -gt 10000 ]; then
        echo "ALERT: Queue $queue depth $msgs"
    fi
done

Auto-Scaling Consumers

#!/bin/bash
# Auto-scale based on queue depth
DEPTH=$(rabbitmqctl list_queues -q magento_catalog | awk '{print $2}')
CURRENT=$(supervisorctl status | grep -c "RUNNING")

if [ $DEPTH -gt 5000 ] && [ $CURRENT -lt 10 ]; then
    supervisorctl start magento-consumer
elif [ $DEPTH -lt 1000 ] && [ $CURRENT -gt 2 ]; then
    supervisorctl stop magento-consumer
fi

Dead Letter Queue Handling

Message fails 3 times → Dead Letter Queue

Monitor DLQ depth
Alert if DLQ growing
Reprocess or discard DLQ messages

Quiz

1. How do you scale Magento queue consumers?

Question 1 options

2. What is backpressure in message queues?

Question 2 options

3. What happens when a message fails processing multiple times?

Question 3 options

Flashcards

Question

Queue scaling method?

Answer

Run multiple consumer processes in parallel

Question

Backpressure purpose?

Answer

Prevent queue overload when consumers can't keep up

Question

Dead letter queue?

Answer

Holds messages that failed processing after max retries

Question

Queue partitioning types?

Answer

Round robin, hash-based, priority, topic-based

Revision Notes

Key Takeaways

  • 1. Scale consumers by running multiple processes in parallel
  • 2. Queue partitioning enables parallel processing by type
  • 3. Target >1000 messages/sec throughput with <100ms latency
  • 4. Backpressure prevents queue overload during traffic spikes
  • 5. Monitor dead letter queue for failed message investigation

Interview Tips

  • Explain consumer scaling strategies and supervisor config
  • Discuss partitioning strategies for ordered vs unordered processing
  • Describe backpressure mechanisms and when to apply each

Cheat Sheet

Queue Scaling:
  Multiple consumers: parallel processing
  Supervisor: auto-restart, process management

Partitioning:
  Round Robin: Equal load
  Hash-based: Ordered processing
  Topic: Different workloads

Throughput:
  Target: >1000 msgs/sec
  Prefetch: 50 messages
  Batch: 50 messages/batch

Backpressure:
  Queue depth limit
  Rate limiting
  Auto-scaling consumers
  Circuit breaker