← Back to the chapter · Previous detail · Next detail →

Technical detail · AMQP Reliability: Delivery Controls

Severity routing and sending configuration

The fragments show the three queue policies. Warning traffic uses persistent messages to match the stated design. Priority 9 is a message property; the quorum queue declaration relies on queue-type-specific priority behaviour. Provision consumer acknowledgement and downstream alert evidence separately; these declarations do not implement SMS or batch scheduling.

# Topic exchange allows flexible pattern matching
channel.exchange_declare(
    exchange='alerts',
    exchange_type='topic',
    durable=True
)

# Routing key format: {severity}.{line}.{sensor_type}
# Examples: critical.line3.temperature
#           warning.line7.pressure
#           info.line1.vibration
# Critical alerts: persistent, high availability
channel.queue_declare(
    queue='critical-alerts',
    durable=True,
    arguments={
        'x-queue-type': 'quorum'         # Replicated for HA
    }
)
channel.queue_bind(
    queue='critical-alerts',
    exchange='alerts',
    routing_key='critical.#'            # All critical alerts
)

# Warning alerts: persistent but standard queue
channel.queue_declare(queue='warning-alerts', durable=True)
channel.queue_bind(
    queue='warning-alerts',
    exchange='alerts',
    routing_key='warning.#'
)

# Info alerts: can lose some, optimize for throughput
channel.queue_declare(
    queue='info-logs',
    durable=False,                       # Queue definition does not survive restart
    arguments={
        'x-max-length': 100000,          # Buffer limit
        'x-overflow': 'drop-head'        # Drop oldest if full
    }
)
channel.queue_bind(
    queue='info-logs',
    exchange='alerts',
    routing_key='info.#'
)

# Also send all alerts to data warehouse
channel.queue_bind(
    queue='info-logs',
    exchange='alerts',
    routing_key='*.#'                    # Everything
)
def send_alert(severity, line, sensor_type, message):
    routing_key = f"{severity}.{line}.{sensor_type}"

    # Set delivery mode based on severity
    if severity == 'critical':
        properties = pika.BasicProperties(
            delivery_mode=2,             # Persistent
            priority=9,                  # High priority
            expiration='300000'          # 5 min TTL
        )
        # Use publisher confirms for critical
        channel.confirm_delivery()
    elif severity == 'warning':
        properties = pika.BasicProperties(delivery_mode=2)
    else:
        properties = pika.BasicProperties(delivery_mode=1)

    channel.basic_publish(
        exchange='alerts',
        routing_key=routing_key,
        body=json.dumps({
            'severity': severity,
            'line': line,
            'sensor': sensor_type,
            'message': message,
            'timestamp': datetime.utcnow().isoformat()
        }),
        properties=properties,
        mandatory=(severity == 'critical')  # Return if unroutable
    )

← Back to the chapter · Previous detail · Next detail →