小程序
传感搜
传感圈

MQTT vs AMQP for IoT Communications: Head to Head

2023-10-25 04:54:52
关注

Illustration: © IoT For All

With the exponential growth of the Internet of Things (IoT), managing communication between devices and systems is growing in importance. IoT communication is made possible by message queuing protocols such as MQTT and AMQP that facilitate the exchange of information in a structured and efficient manner.

Two popular protocols powering the IoT are the MQTT protocol (Message Queuing Telemetry Transport) and AMQP (Advanced Message Queuing Protocol). Let’s explore each of these protocols in detail and explain their key differences and the factors to consider when choosing between the two.

Importance of Message Queuing Protocols

In IoT, devices ranging from simple sensors to complex machines need to communicate with each other and with central systems. These devices often operate in environments with constrained resources, such as low power or unreliable networks.

Message queuing protocols like MQTT, which is designed for such environments, enable these devices to communicate efficiently and reliably.

In distributed systems, components often need to exchange information while maintaining loose coupling. Protocols like AMQP, with its robust features and complex routing capabilities, provide a reliable means for this communication.

These protocols ensure that messages are delivered reliably, and the systems can scale and evolve independently.

What Is MQTT?

MQTT, short for Message Queuing Telemetry Transport, is a lightweight publish/subscribe messaging protocol. It was introduced by IBM in 1999.

Designed for constrained devices and low-bandwidth, high-latency, or unreliable networks, MQTT is perfect for machine-to-machine or IoT use cases where a small code footprint is required.

MQTT operates based on the publish/subscribe model. In this model, a producer, known as a publisher, creates messages, and a consumer, known as a subscriber, receives them.

The interaction between the publisher and subscriber is managed by a broker. The broker is responsible for distributing the messages from publishers to subscribers.

The simplicity of MQTT lies in its minimal protocol commands. It has only a handful of commands, making it easy to implement in various devices and systems.

Also, its quality-of-service levels allows for message delivery confirmation, ensuring that no message is lost in transmission. Let’s explore MQTT in more detail.

MQTT Transport and Framing

MQTT can use TCP, TLS, WebSocket, or QUIC as its transport layer, creating connections, establishing sessions, and transmitting messages reliably between clients and brokers.

MQTT frames consist of a 2-byte fixed header, a variable header, and a variable-length payload. The header contains information such as the packet type, quality of service level, remaining length, packet ID, and properties. The binary payload contains the actual message being transmitted.

MQTT defines 15 types of control packets based on the binary frames in the version 5.0 protocol specifications. Some of the common packets used to connect, publish and subscribe include CONNECT, CONACK, PUBLISH, PUBACK, and SUBSCRIBE.

MQTT QoS

MQTT supports three qualities of service for message delivery:

  • QoS0 “At most once”: Messages are delivered according to the best efforts of the operating environment; message loss can occur.
  • QoS1 “At least once”: Messages are assured to arrive, but duplicates can occur.
  • QoS2 “Exactly once”: Messages are assured to arrive exactly once.

MQTT Advantages and Disadvantages

Pros:

  • Simplicity: The simplest publish-subscribe design, easier to set up, develop, and manage.
  • Lightweight and Efficient: MQTT messages have only 2-type header overhead with low bandwidth usage making the protocol ideal for low-power, low-bandwidth devices.
  • Scalability: Scale to tens of millions of MQTT connections, topics, and subscriptions.
  • Reliable message delivery: Three Quality of Service levels to ensure reliable message delivery, even over unreliable networks.
  • Low latency: Near real-time message delivery with one-digit millisecond latency due to the simple topic-based pub/sub model.
  • Security: MQTT supports secure communications over TLS/SSL or QUIC and various authentication mechanisms using LDAP, JWT, PSK, and X.509 certificates.
  • Compatibility and Integration: MQTT can be integrated with many programming languages, operating systems, and software platforms.

Cons:

  • MQTT lacks store-and-forward queuing.

What is AMQP?

AMQP, Advanced Message Queuing Protocol, is a protocol that supports a wide range of messaging patterns and offers a robust set of features. It was created by J.P. Morgan Chase in 2003. It is designed for systems that require a high level of reliability and functionality.

Unlike MQTT, AMQP is a peer-to-peer protocol, meaning it supports direct communication between the producer and consumer.

AMQP uses a model where messages are sent to exchanges, which then route the messages to appropriate queues based on rules called bindings. The consumer then retrieves the message from the queue.

This model allows for complex routing and distribution strategies, making AMQP suitable for complex distributed systems.

The strength of AMQP lies in its extensive feature set. It supports a variety of message properties and delivery modes, including persistent messaging, which ensures the messages are not lost even if the broker restarts.

AMQP Exchange and Bindings

In AMQP, an exchange is like an email transfer agent that inspects email and decides on the basis of routing keys and tables. A routing key corresponds to an email with To:, Cc:, or Bcc: addresses, without server information (routing is internal within an AMQP server). A binding is like an entry in the email transfer agent’s routing table.

AMQP defines four types of exchanges:

  1. Direct (point-to-point): Messages are routed directly to the queue bound to the exchange.
  2. Fanout: Messages are routed to every queue bound to the exchange.
  3. Topic (publish-subscribe): Messages are routed to queues based on the routing key and the binding pattern to the exchange.
  4. Headers (publish-subscribe): Messages are routed to queues based on pattern matching of message headers.

AMQP Transport and Framing

AMQP is a binary protocol built on TCP/IP, where a reliable, persistent, stream-oriented connection is established between a client and a broker. Multiple channels can be opened on a single socket connection, allowing multiple streams of data to be transferred simultaneously.

AMQP frames in version 1.0 consist of an 8-byte fixed header, an optional extended header, and a variable-length binary payload. The fixed header contains information about the frame type, channel number, and size of the payload.

The payload contains the actual message being transmitted, along with any associated metadata.

AMQP Advantages and Disadvantages

Pros:

  • Store-and-forward queuing: AMQP supports store-and-forward queuing at the expense of some efficiency and additional complexity (compared to MQTT).
  • Flexible message routing: AMQP provides flexible message routing, including point-to-point, publish-subscribe, and fan-out.
  • Security: AMQP supports security measures such as TLS and SASL for encryption and authentication.
  • Ecosystem: AMQP has a large and mature ecosystem of open-source server implementations and client libraries for various programming languages.

Cons:

  • Complex: AMQP introduces many concepts in its model and can be complex and challenging to understand, set up, and manage.
  • Heavyweight: AMQP introduces multiplexing in its transport layer with “channels.” Each AMQP frame has a header overhead of 8 bytes.
  • Backward compatibility: The biggest problem of AMQP is that versions 0.9.1 and 1.0 are completely different, leading to more complexity in this solution space.
  • Scalability and performance: AMQP may have limited scalability and low performance in some use cases. This is due to its architecture, which imposes greater complexity and overhead compared to lightweight protocols like MQTT.

MQTT vs AMQP: Head-to-Head

The following table summarizes our comparison between AMQP and MQTT.

 AMQPMQTT
DefinitionAdvanced Message Queuing ProtocolMessage Queueing Telemetry Transport
OriginsInvented by JPMorgan Chase in 2003Invented by IBM in 1999
ArchitectureEBQ (Exchange-Binding-Queue)Topic-based Publish/Subscribe
Core ConceptsExchangesQueuesBindingsRouting KeysTopicsSubscriptions
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
Messaging Paradigms  
Point-to-Point✅ (store-and-forward queues)Partial support
Publish/Subscribe
Fan-out✅ more scalable
Fan-in
Request/Reply✅ in version 5.0
Push/Pull
Transports  
TCP
TLS/SSL
WebSocket
QUIC
Framing  
Frame StructureFrames are divided into three distinct areas:
Fixed width frame header,
Variable width extended header,
Variable width frame body.
An MQTT Control Packet consists of up to three parts:
Fixed Header
Variable Header
Payload
Fix Header Size8 Bytes2 Bytes
Payload ContentBinaryBinary
Max Payload Size2GB256MB
Delivery  
QoS 0: At Most Once
QoS 1: At Least Once
QoS 2: Exactly Once
SecuritySSL/TLSSSL/TLS

Factors to Consider when Choosing

When faced with a decision between MQTT vs AMQP, there are several factors to consider.

Evaluation Based on Use Case Requirements

The choice between MQTT and AMQP largely depends on the use case requirements. If you are dealing with constrained devices or unreliable networks, MQTT with its lightweight nature might be the better option. If your use case requires complex routing and high reliability, then AMQP would be more suitable.

Evaluation Based on System Architecture

The system architecture also plays a role in the decision. MQTT’s simplicity makes it a good choice for systems with a clear and simple communication model. AMQP, with its flexibility and robust feature set, is better suited for complex systems with diverse communication needs.

Evaluation Based on Network Conditions

Network conditions are another critical factor. MQTT performs well in low-bandwidth, high-latency, or unreliable networks. AMQP, on the other hand, requires a reliable network connection due to its higher overhead.

Evaluation Based on Required Quality of Service

MQTT provides three levels of quality of service, allowing for flexibility in message delivery guarantees. AMQP, with its persistent messaging, ensures high reliability in message delivery.

Making Your Decision

Both MQTT and AMQP have their strengths and are suited for different scenarios. Understanding their key differences and evaluating your requirements against these differences will help you make an informed decision when choosing. Remember, the right choice depends on your specific needs and constraints.

Tweet

Share

Share

Email

  • MQTT
  • Network and Protocols

  • MQTT
  • Network and Protocols

参考译文
物联网通信中MQTT与AMQP的对比:全面分析# 示例输入与输出**输入**人工智能(AI)是计算机科学的一个分支,旨在创建能够表现出人类智能的软件或机器。这可以包括从经验中学习、理解自然语言、解决问题以及识别模式。**输出**人工智能(AI)是计算机科学的一个分支,旨在开发表现出人类智能的软件或机器。这包括从经验中学习、理解自然语言、解决问题以及识别模式。
图片提供:© IoT For All --> 随着物联网(IoT)呈指数级增长,设备与系统之间通信的管理变得愈发重要。物联网通信依赖于诸如MQTT和AMQP之类的报文排队协议,以结构化和高效的方式实现信息交换。在物联网中发挥重要作用的两种流行协议分别为**MQTT协议**(消息队列遥测传输)和AMQP(高级消息队列协议)。接下来我们将深入了解这些协议,详细解释它们之间的主要区别以及在选择时需要考虑的因素。消息队列协议的重要性在物联网中,从简单的传感器到复杂的机器,各种设备都需要与彼此以及中央系统进行通信。这些设备往往运行在资源受限的环境中,例如低功耗或不可靠的网络。为这类环境设计的消息队列协议(如MQTT)使这些设备能够高效且可靠地通信。在分布式系统中,组件通常需要在保持松散耦合的同时交换信息。具有强大功能和复杂路由能力的协议(如AMQP)为此类通信提供了可靠的手段。这些协议确保消息可靠传递,并使系统能够独立扩展和演进。什么是MQTT?MQTT,即Message Queuing Telemetry Transport(消息队列遥测传输),是一种轻量级的发布/订阅消息协议。它于1999年由IBM引入。MQTT专为资源受限设备和低带宽、高延迟或不可靠的网络而设计,非常适合在需要较小代码占用的机器对机器通信或物联网用例中使用。MQTT基于**发布/订阅模型**运行。在此模型中,生产者(称为发布者)创建消息,消费者(称为订阅者)接收消息。发布者和订阅者之间的交互由一个代理(broker)管理。该代理负责将消息从发布者分发给订阅者。MQTT的简单性体现在其最小的协议命令上。它只有少数几种命令,使其易于在各种设备和系统中实现。此外,其服务质量(QoS)级别允许消息传递的确认,确保在传输过程中不会丢失消息。MQTT传输与帧结构MQTT可以使用TCP、TLS、WebSocket或**QUIC**作为其传输层,在客户端和代理之间建立连接、会话并可靠地传输消息。MQTT帧由一个2字节的固定头、一个可变头和一个可变长度的载荷组成。头中包含包类型、服务质量级别、剩余长度、包ID和属性等信息。二进制载荷包含实际传输的消息。MQTT在版本5.0协议规范中定义了基于二进制帧的15种控制包。用于连接、发布和订阅的常见包包括CONNECT、CONACK、PUBLISH、PUBACK和SUBSCRIBE。MQTT服务质量(QoS)MQTT支持三种消息传递的服务质量:- QoS0 “最多一次”:消息根据环境的尽力而为进行交付;可能会丢失消息。- QoS1 “至少一次”:消息确保到达,但可能出现重复消息。- QoS2 “恰好一次”:消息确保恰好到达一次。MQTT的优势与劣势优势:- 简单性:最简单的发布-订阅设计,易于设置、开发和管理。- 轻量高效:MQTT消息仅有2种类型的头部开销,带宽使用低,非常适合低功耗、低带宽设备。- 可扩展性:可扩展到数以千万计的MQTT连接、主题和订阅。- 可靠的消息传递:三种服务质量级别确保即使在不可靠网络中消息也能可靠传递。- 低延迟:由于简单基于主题的发布/订阅模型,消息传递近乎实时,延迟为个位数毫秒。- 安全性:MQTT支持通过TLS/SSL或QUIC进行安全通信,并支持通过LDAP、JWT、PSK和X.509证书的多种身份验证机制。- 兼容性与集成性:MQTT可以与许多编程语言、操作系统和软件平台集成。劣势:- MQTT缺乏存储转发队列功能。什么是AMQP?AMQP,即Advanced Message Queuing Protocol(高级消息队列协议),是一种支持多种消息模式并提供强大功能集的协议。它于2003年由J.P.摩根大通创建。该协议专为需要高度可靠性和功能的系统而设计。与MQTT不同,AMQP是一种对等协议,意味着它支持生产者和消费者之间的直接通信。AMQP采用消息发送到交换器,然后根据称为绑定的规则将消息路由到适当队列的模型。消费者随后从队列中获取消息。此模型允许复杂的路由和分发策略,使AMQP适用于复杂的分布式系统。AMQP的强大之处在于其广泛的功能集。它支持多种消息属性和传递模式,包括持久消息,即使在代理重启时也能确保消息不会丢失。AMQP交换与绑定在AMQP中,一个交换器就像电子邮件传递代理,它检查邮件并根据路由键和表作出路由决策。一个路由键相当于一封邮件的To、Cc或Bcc地址,但不包含服务器信息(路由在AMQP服务器内部进行)。一个绑定就像邮件传递代理的路由表中的一项。AMQP定义了四种类型的交换器:- 直接(点对点):消息直接发送到绑定到交换器的队列。- 扇出:消息发送到绑定到交换器的所有队列。- 主题(发布/订阅):消息根据路由键和绑定到交换器的模式路由到队列。- 标头(发布/订阅):消息根据消息头的模式匹配路由到队列。AMQP传输与帧结构AMQP是一种基于TCP/IP的二进制协议,其中在客户端和代理之间建立可靠、持久的流式连接。可以在单个套接字连接上打开多个通道,从而实现多路复用,同时传输多个数据流。AMQP 1.0版本的帧由一个8字节固定头、一个可选扩展头和一个可变长度的二进制载荷组成。固定头包含帧类型、通道号和载荷大小等信息。载荷包含实际传输的消息以及任何相关元数据。AMQP的优势与劣势优势:- 存储转发队列:AMQP支持存储转发队列,但以一定的效率和额外的复杂性为代价(相对于MQTT)。- 灵活的消息路由:AMQP提供灵活的消息路由,包括点对点、发布/订阅和扇出。- 安全性:AMQP支持安全措施,如TLS和SASL,用于加密和认证。- 生态系统:AMQP拥有一个庞大的成熟开源服务器实现和客户端库的生态系统,适用于各种编程语言。劣势:- 复杂性:AMQP在其模型中引入了许多概念,可能较为复杂,理解、设置和管理都有一定挑战性。- 重量级:AMQP在传输层中通过“通道”引入多路复用。每个AMQP帧有8字节的头部开销。- 向后兼容性:AMQP最大的问题是0.9.1和1.0版本完全不同,导致该解决方案空间更加复杂。- 可扩展性和性能:在某些用例中,AMQP可能可扩展性有限且性能较低。这是由于其架构引入了比MQTT等轻量级协议更高的复杂性和开销。MQTT与AMQP:直接比较下表总结了MQTT与AMQP之间的主要区别。选择时需要考虑的因素在选择MQTT或AMQP时,有几个因素需要考虑。基于用例要求的评估MQTT和AMQP的选择在很大程度上取决于用例要求。如果你处理的是资源受限的设备或不可靠的网络,MQTT由于其轻量级特性可能是更好的选择。如果你的用例需要复杂路由和高可靠性,则AMQP更合适。基于系统架构的评估系统架构也影响决策。MQTT的简单性使其成为通信模型清晰且简单的系统的良好选择。AMQP凭借其灵活性和强大的功能集,更适合通信需求多样的复杂系统。基于网络条件的评估网络条件是另一个关键因素。MQTT在低带宽、高延迟或不可靠网络中表现出色。而AMQP由于其较高的开销,需要可靠的网络连接。基于所需服务质量的评估MQTT提供三种服务质量级别,允许消息传递保障的灵活性。AMQP通过其持久消息功能,确保消息传递的高可靠性。做出你的决策MQTT和AMQP都有其优势,适用于不同的场景。理解它们的关键区别,并将你的需求与这些区别进行评估,将帮助你在做出选择时更加明智。记住,正确的选择取决于你的具体需求和限制条件。
您觉得本篇内容如何
评分

评论

您需要登录才可以回复|注册

提交评论

广告
提取码
复制提取码
点击跳转至百度网盘