Back

AMQP

Loading views...

AMQP

Category: Networking/IoT

Overview

AMQP (Advanced Message Queuing Protocol) is an open standard for enterprise message-oriented middleware with Exchange→Binding→Queue routing, persistent messages, ACK guarantees, and rich QoS. Used in financial systems, enterprise IoT, and cloud messaging (RabbitMQ, Azure Service Bus).


1. Theory & Fundamentals

  • Exchange → Binding → Queue routing model
  • Reliability: Persistent messages, publisher confirms, consumer ACKs
  • AMQP 0-9-1: Most deployed (RabbitMQ native)
  • AMQP 1.0: ISO 19464; Azure Service Bus, Apache Qpid
  • Transport: TCP port 5672 (plain), 5671 (TLS)
  • Security: SASL auth + TLS encryption

2. Frame / Packet Structure

AMQP 0-9-1 Frame:
  Type(1B) | Channel(2B) | Size(4B) | Payload | FrameEnd(0xCE)

Frame Types: Method(1) | Header(2) | Body(3) | Heartbeat(8)

Method classes: Connection | Channel | Exchange | Queue | Basic | Tx

Basic.Publish:
  Exchange | Routing Key | Mandatory | Immediate

Basic.Deliver:
  Consumer Tag | Delivery Tag | Redelivered | Exchange | Routing Key

3. Protocol Mechanics

  • Exchange types: Direct (exact match), Topic (wildcard), Fanout (broadcast), Headers
  • Consumer ACK: ack/nack/reject delivery tags
  • Prefetch: basic.qos limits unacked messages per consumer
  • Dead Letter Exchange: Undeliverable messages rerouted
  • Publisher confirms: Broker ACKs published messages

4. Hardware Implementation

  • RabbitMQ: Most popular AMQP 0-9-1 broker
  • Python: pika library
  • C: librabbitmq (official RabbitMQ C client)
  • Embedded Linux (Raspberry Pi, industrial PC)
  • Azure Service Bus: AMQP 1.0 cloud messaging

5. Register-Level / Configuration

import pika
conn = pika.BlockingConnection(
    pika.ConnectionParameters('localhost'))
ch = conn.channel()
ch.exchange_declare('sensors', 'topic', durable=True)
ch.queue_declare('temps', durable=True)
ch.queue_bind('temps', 'sensors',
              routing_key='sensor.*.temperature')
# Publish
ch.basic_publish('sensors',
    routing_key='sensor.room1.temperature',
    body=b'{"temp":23.5}',
    properties=pika.BasicProperties(delivery_mode=2))

6. Driver / Software Development

// librabbitmq C client
amqp_connection_state_t conn = amqp_new_connection();
amqp_socket_t *sock = amqp_tcp_socket_new(conn);
amqp_socket_open(sock, "localhost", 5672);
amqp_login(conn, "/", 0, 131072, 0,
    AMQP_SASL_METHOD_PLAIN, "guest", "guest");
amqp_channel_open(conn, 1);
amqp_basic_publish(conn, 1,
    amqp_cstring_bytes("sensors"),
    amqp_cstring_bytes("sensor.temp"),
    0, 0, NULL,
    amqp_cstring_bytes("{\"temp\":23.5}"));

7. Debugging & Testing

  • RabbitMQ Management UI (port 15672): Queues, exchanges, bindings, rates
  • rabbitmqctl CLI: Broker management
  • Wireshark AMQP dissector
  • Common issues: Exchange type mismatch; missing binding; prefetch too low; dead letter loop

8. Real-World Applications

  1. Financial order routing
  2. Enterprise IoT data pipeline
  3. Microservices communication
  4. Azure Service Bus workloads
  5. Email delivery systems

9. Advanced Topics & Edge Cases

  • Quorum queues: Replicated for HA
  • Shovel: Move messages between brokers
  • Federation: Connect distributed clusters
  • Priority queues: Higher priority delivered first
  • AMQP vs MQTT: AMQP richer routing/reliability; MQTT simpler IoT pub/sub

10. Standards & Variants

Version Notes
AMQP 0-9-1 RabbitMQ native
AMQP 1.0 ISO 19464, Azure
MQTT Simpler IoT alternative
STOMP Text-based over WebSocket

💡 Practical Examples

Ex 1: Topic routing — sensor.*.temperature routes to temp queue.

Ex 2: DLX: queue_declare(args={'x-dead-letter-exchange':'dlx'})

Ex 3: Publisher confirms: ch.confirmdelivery(); ch.basicpublish(...) raises on failure.


🧪 Practice Questions

Beginner: 1) 4 exchange types? 2) What is a queue? 3) Fanout vs direct? 4) AMQP port? 5) What is delivery_mode=2?

Intermediate: 1) Topic exchange with wildcards. 2) ACK on consumer crash. 3) Prefetch count. 4) Publisher confirms. 5) TTL + dead letter.

Advanced: 1) 10,000-device IoT with guaranteed delivery. 2) RabbitMQ shovel cross-DC. 3) Priority queue for alerts. 4) AMQP 1.0 Azure Service Bus. 5) AMQP vs MQTT at scale.


Checklist

  • [ ] Implement publisher and consumer in Python
  • [ ] Configure topic exchange routing
  • [ ] Implement consumer ACK/NACK
  • [ ] Use publisher confirms
  • [ ] Configure dead letter exchange
  • [ ] Set up RabbitMQ Management UI
  • [ ] Implement C AMQP client
  • [ ] Handle consumer crash recovery
  • [ ] Design quorum queues for HA
  • [ ] Compare AMQP vs MQTT