Author: EMQX Team
Original: https://www.emqx.com/zh/blog/mqtt-vs-amqp-for-iot-communications

Table of Contents
- The Importance of Message Queue Protocols in IoT and Distributed Systems
- What is MQTT?
- What is AMQP?
- Detailed Comparison of MQTT and AMQP
- Factors to Consider When Choosing MQTT and AMQP
- Implementing Large-Scale MQTT Scenarios with EMQX
With the rapid development of the Internet of Things (IoT), managing communication between devices and systems has become increasingly important. IoT communication typically relies on message queue protocols to facilitate information exchange in an orderly and efficient manner.
The MQTT protocol (Message Queuing Telemetry Transport) and the AMQP protocol (Advanced Message Queuing Protocol) are two widely used protocols in IoT. This article will provide a detailed introduction to these two protocols, clarify their main differences, and discuss the factors to consider when choosing between them.
The Importance of Message Queue Protocols in IoT and Distributed Systems
In IoT, various devices, whether simple sensors or complex machines, need to communicate with each other and with central systems. These devices often operate in resource-constrained environments, such as low power or unstable networks. Message queue protocols like MQTT are designed for such scenarios, enabling efficient and reliable communication between these devices.
In distributed systems, components need to exchange information while maintaining loose coupling. Protocols like AMQP offer powerful features and complex routing capabilities, providing reliable communication for such scenarios. These protocols ensure that messages are accurately transmitted, and systems can independently scale and evolve.
What is MQTT?
MQTT is a lightweight message transport protocol based on a publish/subscribe model, introduced by IBM in 1999. It is designed for resource-constrained devices and low-bandwidth, high-latency, or unstable networks, making it ideal for applications that require minimal code footprint (e.g., machine-to-machine communication or IoT).
MQTT is based on a publish/subscribe model, where producers (also known as publishers) create information, and consumers (also known as subscribers) receive it. The interaction between publishers and subscribers is managed by a Broker, which is responsible for distributing messages from publishers to subscribers.
The simplicity of MQTT lies in its few protocol commands. It has only a handful of commands, making it easy to implement across various devices and systems. Additionally, its quality of service levels ensure message transmission reliability, preventing message loss during transmission. Let’s take a closer look at MQTT.
MQTT Transport and Frames
MQTT can use TCP, TLS, WebSocket, or QUIC as the transport layer to establish connections, create sessions, and reliably transmit information between clients and Brokers.
MQTT frames consist of a 2-byte fixed header, a variable header, and a length-variable payload. The header contains information such as message type, quality of service level, remaining length, message ID, and properties. The payload is in binary format and contains the actual message content to be transmitted.
According to the binary frame specification in version 5.0, MQTT defines 15 types of control messages. Common messages used for connection, publishing, and subscribing include CONNECT, CONACK, PUBLISH, PUBACK, and SUBSCRIBE.
MQTT QoS
MQTT supports three different levels of message delivery quality:
-
QoS0 “At most once”:Message delivery depends on the network environment, and message loss may occur.
-
QoS1 “At least once”:Messages are guaranteed to arrive, but duplicates may occur.
-
QoS2 “Exactly once”:Messages are guaranteed to arrive exactly once, with no duplicates or loss.
Advantages and Disadvantages of MQTT
Advantages
-
Simple:Utilizes the simplest publish-subscribe model, making it easy to configure, develop, and manage.
-
Lightweight and Efficient:MQTT messages have only a 2-byte header overhead, saving bandwidth resources. This makes the protocol very suitable for low-power, low-bandwidth devices.
-
Highly Scalable:Can support millions of MQTT connections, topics, and subscriptions.
-
Reliable Message Delivery:Offers three levels of quality of service, ensuring reliable message transmission even in unstable network environments.
-
Low Latency:Due to its simple topic-based publish/subscribe model, it achieves near real-time message transmission with latency in the single-digit millisecond range.
-
Secure:Supports secure communication via TLS/SSL or QUIC, and offers various authentication methods such as LDAP, JWT, PSK, and X.509 certificates.
-
Strong Compatibility and Integration:Can be compatible and integrated with various programming languages, operating systems, and software platforms.
Disadvantages
- MQTT lacks storage and forwarding queue functionality.
What is AMQP?
AMQP is a protocol that supports multiple messaging patterns and has a powerful feature set. It was created by JPMorgan Chase in 2003, designed for systems that require high reliability and complex features.
Unlike MQTT, AMQP is a point-to-point protocol that allows direct communication between producers and consumers.
AMQP employs a model where messages are sent to an exchange, which routes the messages to the appropriate queues based on binding rules. Consumers then retrieve messages from the queues. This model supports complex routing and distribution strategies, making AMQP suitable for complex distributed systems.
The advantage of AMQP lies in its rich feature set. It supports various message attributes and transport modes, including persistent messages, ensuring that messages are not lost even if the Broker restarts.
AMQP Exchanges and Bindings
In AMQP, an exchange acts like an email transport agent, responsible for checking emails and deciding how to send them to one or more mailboxes based on routing keys and tables. The routing key is akin to an email with recipient, CC, or BCC addresses but does not contain server information (routing occurs only within the AMQP server). Bindings are like entries in the routing table of the email transport agent.
AMQP defines four types of exchanges:
-
Direct (Point-to-Point):Messages are sent directly to the specified queue bound to the exchange.
-
Fanout:Messages are sent to all queues bound to the exchange.
-
Topic (Publish-Subscribe):Messages are sent to the appropriate queues based on routing keys and exchange bindings.
-
Headers (Publish-Subscribe):Messages are sent to the appropriate queues based on matching rules in the message headers.
AMQP Transport and Frames
AMQP is a binary protocol based on TCP/IP that establishes reliable, persistent, stream-oriented connections between clients and Brokers. Multiple channels can be created over a single socket connection, allowing simultaneous transmission of multiple data streams.
In AMQP version 1.0, frames consist of an 8-byte fixed header, an optional extended header, and a length-variable binary payload. The fixed header contains information about the frame type, channel number, and payload size. The payload contains the actual message to be transmitted, along with relevant metadata.
Advantages and Disadvantages of AMQP
Advantages
-
Supports Storage and Forwarding Queues:AMQP supports storage and forwarding queues, but sacrifices some efficiency and adds complexity compared to MQTT.
-
Flexible Message Routing:AMQP provides various message routing methods, including point-to-point, publish-subscribe, and fanout.
-
Secure:AMQP supports encryption and authentication security mechanisms such as TLS and SASL.
-
Mature Ecosystem:The AMQP ecosystem has a rich and mature set of open-source server implementations and client libraries covering various programming languages.
Disadvantages
-
Complex:The AMQP model involves many concepts, which may be difficult to understand, configure, and manage.
-
Heavyweight:AMQP achieves multiplexing at the transport layer through “channels,” and each AMQP frame has an 8-byte header overhead.
-
Poor Backward Compatibility:The biggest issue with AMQP is that versions 0.9.1 and 1.0 are completely incompatible, increasing the complexity of solutions.
-
Scalability and Performance Limitations:AMQP may face scalability and performance limitations in some scenarios due to its architecture, which introduces more complexity and overhead compared to lightweight protocols like MQTT.
Detailed Comparison of MQTT and AMQP
The table below provides a detailed comparison of AMQP and MQTT.
| AMQP | MQTT | |
|---|---|---|
| Full Name | Advanced Message Queuing Protocol | Message Queueing Telemetry Transport |
| Origin | Invented by JPMorgan Chase in 2003 | Invented by IBM in 1999 |
| Architecture | EBQ (Exchange-Binding-Queue) | Topic-based Publish/Subscribe |
| Core Concepts | Exchange, Queue, Binding, Routing Key | Topic, Subscription |
| Main Protocol Versions | · 0.9.1 released in November 2008 · 1.0 released in October 2012 | · 3.1.1 released in December 2015 · 5.0 released in March 2019 |
| Message Paradigms | ||
| Point-to-Point | ✅ (Storage and Forwarding Queues) | Partially Supported |
| Publish/Subscribe | ✅ | ✅ |
| Fanout | ✅ | ✅ More Scalable |
| Fanin | ✅ | ✅ |
| Request/Response | ✅ | ✅ Supported in 5.0 |
| Push/Pull | ❌ | ❌ |
| Transport | ||
| TCP | ✅ | ✅ |
| TLS/SSL | ✅ | ✅ |
| WebSocket | ❌ | ✅ |
| QUIC | ❌ | ✅ |
| Frames | ||
| Frame Structure | Frames are divided into three different areas: fixed-length frame header, variable-length extended header, and variable-length frame body. | MQTT control messages consist of up to three parts: fixed header, variable header, and payload. |
| Fixed Header Size | 8 bytes | 2 bytes |
| Payload Content | Binary | Binary |
| Maximum Payload Size | 2GB | 256MB |
| Transport | ||
| QoS 0: At most once | ✅ | ✅ |
| QoS 1: At least once | ✅ | ✅ |
| QoS 2: Exactly once | ❌ | ✅ |
| Security | SSL/TLS | SSL/TLS |
Factors to Consider When Choosing MQTT and AMQP
When choosing between MQTT and AMQP, several factors need to be considered.
User Scenario-Based Assessment
The choice between MQTT and AMQP primarily depends on the requirements of the user scenario. If you are dealing with resource-constrained devices or unstable networks, MQTT, with its lightweight characteristics, may be the better choice. If your user scenario requires complex routing and high reliability, then AMQP would be more suitable.
System Architecture-Based Assessment
The system architecture will also influence the decision. The simplicity of MQTT makes it an ideal choice for systems with a clear and simple communication model. AMQP, with its flexibility and powerful feature set, is better suited for complex systems with diverse communication needs.
Network Condition-Based Assessment
Network conditions are another important factor. MQTT performs excellently in low-bandwidth, high-latency, or unreliable networks. In contrast, AMQP requires stable network connections due to its higher overhead.
Quality of Service Requirements Assessment
MQTT offers three levels of quality of service, flexibly ensuring message delivery. AMQP, through its persistent message feature, ensures high reliability in message transmission.
In summary, both MQTT and AMQP have their advantages and are suitable for different application scenarios. Understanding their main differences and assessing your needs based on these differences will help you make an informed decision. Remember, the right choice depends on your specific requirements and constraints.
Implementing Large-Scale MQTT Scenarios with EMQX
EMQX is the most scalable MQTT message transport platform, designed for high performance, scalability, and fault tolerance. EMQX enables large-scale IoT devices to communicate with each other and supports seamless integration with edge servers and cloud applications.
EMQX provides the following features to support large-scale MQTT connections and message routing:
-
Cluster Architecture:EMQX adopts a distributed cluster architecture, allowing the creation of interconnected node networks. Each node in the cluster is responsible for handling MQTT connections and message transmission, providing strong horizontal scalability to cope with the rapid growth of connected devices and message volume.
-
Load Balancing:EMQX clusters distribute MQTT traffic across nodes through load balancing. This ensures that no single node becomes overloaded, effectively handling a large number of connections.
-
Dynamic Message Routing:It supports the creation of a large number of topics and subscriptions at runtime, enabling dynamic routing and forwarding of messages.
-
Fast and Reliable Data Transmission:It offers various message delivery quality levels, as well as features like persistent sessions and offline message queues, ensuring fast and reliable data transmission even in unstable networks.
-
High Performance and Low Latency:The cluster can transmit millions of messages with latencies as low as 1 millisecond, providing foundational support for latency-sensitive IoT business scenarios.
-
Low-Code Real-Time Data Processing:EMQX provides a powerful built-in rule engine based on SQL statements, enabling one-stop IoT data extraction, filtering, and transformation without the need for coding.
-
One-Stop Enterprise System Integration:Through the rule engine and data bridging capabilities provided by EMQX, you can easily configure the integration of IoT data with over 40 enterprise systems (such as Kafka, SQL, NoSQL, and time-series databases) through a visual interface, without writing code.
EMQX effectively addresses the challenges of handling large-scale MQTT message transmission, making it highly suitable for large-scale IoT application scenarios that require efficient connection and communication among millions of devices.
Reprint Statement: All reprinted articles must indicate the original source or reprint source (in cases where the reprint source does not indicate the original source). If there is any infringement, please contact for removal.