来自:http://www.hivemq.com/blog/mqtt-essentials-part-6-mqtt-quality-of-service-levels

Quality of Service

What is Quality of Service?

The Quality of Service (QoS) level is an agreement between sender and receiver of a message regarding the guarantees of delivering a message. There are 3 QoS levels in MQTT:

  • At most once (0)
  • At least once (1)
  • Exactly once (2).

When talking about QoS there are always two different parts of delivering a message: publishing client to broker and broker to subscribing client. We need to look at them separately since there are subtle differences. The QoS level for publishing client to broker is depending on the QoS level the client sets for the particular message. When the broker transfers a message to a subscribing client it uses the QoS of the subscription made by the client earlier. That means, QoS guarantees can get downgraded for a particular receiving client if subscribed with a lower QoS.

Why is Quality of Service important?

QoS is a major feature of MQTT, it makes communication in unreliable networks a lot easier because the protocol handles retransmission and guarantees the delivery of the message, regardless how unreliable the underlying transport is. Also it empowers a client to choose the QoS level depending on its network reliability and application logic.

How does it work?

So how is the quality of service implemented in the MQTT protocol ? We will look at each level one by one and explain the functionality.

QoS 0 – at most once

The minimal level is zero and it guarantees a best effort delivery. A message won’t be acknowledged by the receiver or stored and redelivered by the sender. This is often called “fire and forget” and provides the same guarantee as the underlying TCP protocol.

QoS 1 – at least once

When using QoS level 1, it is guaranteed that a message will be delivered at least once to the receiver. But the message can also be delivered more than once.

The sender will store the message until it gets an acknowledgement in form of a PUBACK command message from the receiver.

The association of PUBLISH and PUBACK is done by comparing the packet identifier in each packet. If the PUBACK isn’t received in a reasonable amount of time the sender will resend the PUBLISH message. If a receiver gets a message with QoS 1, it can process it immediately, for example sending it to all subscribing clients in case of a broker and then replying with the PUBACK.
The duplicate (DUP) flag, which is set in the case a PUBLISH is redelivered, is only for internal purposes and won’t be processed by broker or client in the case of QoS 1. The receiver will send a PUBACK regardless of the DUP flag.

QoS 2

The highest QoS is 2, it guarantees that each message is received only once by the counterpart. It is the safest and also the slowest quality of service level. The guarantee is provided by two flows there and back between sender and receiver.

If a receiver gets a QoS 2 PUBLISH it will process the publish message accordingly and acknowledge it to the sender with a PUBREC message.

The receiver will store a reference to the packet identifier until it has send the PUBCOMP. This is important for avoid processing the message a second time. When the sender receives the PUBREC it can safely discard the initial publish, because it knows that the counter part has successfully received the message. It will store the PUBREC and respond with a PUBREL.

After the receiver gets the PUBREL it can discard every stored state and answer with a PUBCOMP. The same is true when the sender receives the PUBCOMP.

When the flow is completed both parties can be sure that the message has been delivered and the sender also knows about it.

Whenever a packet gets lost on the way, the sender is responsible for resending the last message after a reasonable amount of time. This is true when the sender is a MQTT client and also when a MQTT broker sends a message. The receiver has the responsibility to respond to each command message accordingly.

Good to know

There are a few things you should have in mind when using QoS. These are not obvious or clear on first sight.

Downgrade of QoS

As already said, the QoS flows between a publishing and subscribing client are two different things as well as the QoS can be different. That means the QoS level can be different from client A, who publishes a message, and client B, who receives the published message. Between the sender and the broker the QoS is defined by the sender. When the broker sends out the message to all subscribers, the QoS of the subscription from client B is used. If client B has subscribed to the broker with QoS 1 and client A sends a QoS 2 message, it will be received by client B with QoS 1. And of course it could be delivered more than once to client B, because QoS 1 only guarantees to deliver the message at least once.

Packet identifiers are unique per client

Also important to know is that each packet identifier (used for QoS 1 and QoS 2) is unique between one client and a broker and not between all clients. If a flow is completed the same packet identifier can be reused anytime. That’s also the reason why the packet identifier doesn’t need to be bigger than 65535, because it is unrealistic that a client sends a such large number of message, without completing the flow.

Best Practice

We are often asked, when to choose which QoS level. The following should provide you some guidance if you are also confronted with this decision. Often this is heavily depending on your use case.

Use QoS 0 when …

  • You have a complete or almost stable connection between sender and receiver. A classic use case is when connecting a test client or a front end application to a MQTT broker over a wired connection.
  • You don’t care if one or more messages are lost once a while. That is sometimes the case if the data is not that important or will be send at short intervals, where it is okay that messages might get lost.
  • You don’t need any message queuing. Messages are only queued for disconnected clients if they have QoS 1 or 2 and a persistent session.

Use QoS 1 when …

  • You need to get every message and your use case can handle duplicates. The most often used QoS is level 1, because it guarantees the message arrives at least once. Of course your application must be tolerating duplicates and process them accordingly.
  • You can’t bear the overhead of QoS 2. Of course QoS 1 is a lot fast in delivering messages without the guarantee of level 2.

Use QoS 2 when …

  • It is critical to your application to receive all messages exactly once. This is often the case if a duplicate delivery would do harm to application users or subscribing clients. You should be aware of the overhead and that it takes a bit longer to complete the QoS 2 flow.

Queuing of QoS 1 and 2 messages

All messages sent with QoS 1 and 2 will also be queued for offline clients, until they are available again. But queuing is only happening, if the client has a persistent session.

Quality of Service 0, 1 2相关推荐

  1. MQTT:QoS服务质量 0,1,2(Quality of Service 0, 1 2)

    原文链接:http://www.hivemq.com/blog/mqtt-essentials-part-6-mqtt-quality-of-service-levels 转载链接:http://bl ...

  2. 【精讲】PCIe Quality of Service(QoS)机制

    目录 本章概述 Traffic Class Vitual Channels TC/VC mapping 端口仲裁 示例分享 本章概述 本章主要讲解PCIe 在TLP层的一个特性,Quality of ...

  3. QOS是什么?(Quality of Service,服务质量)

    看 单播.广播.组播的区别和特点 时候,看到QOS不知道是什么意思 文章目录 定义 QoS(Quality of Service,服务质量)指一个网络能够利用各种基础技术,为指定的网络通信提供更好的服 ...

  4. QoS(Quality of Service)服务质量【转自微信公众号开源Linux】

    QoS(Quality of Service)即服务质量.在有限的带宽资源下,QoS为各种业务分配带宽,为业务提供端到端的服务质量保证.例如,语音.视频和重要的数据应用在网络设备中可以通过配置QoS优 ...

  5. 【移动通信】5GC:5G的QoS (Quality of Service) 控制 服务质量管理

    Quality of Service Control QoS QoS框架 QFI Uplink/Downlink 用户平面协议框架 DRB&QoS 映射 5QI与5G QoS特征 QoS框架( ...

  6. Qos(Quality of Service)

    QOS(即Quality of Service,服务质量)主要指网络环境下服务满足用户的程度,在视频服务的语境下也可认为是Quality of Streaming,即流媒体服务的质量.通常,QOS可以 ...

  7. QC学习一:Windows环境中Quality Center 9.0安装详解

    一.安装前准备 1.安装环境:windows XP.SQL Server2005 2.准备安装文件:Quality Center 9.0 (qc10以上,包括qc10,qc只支持安装在服务器操作系统上 ...

  8. 服务质量QoS(Quality of Service)的基础知识

    QoS概述 浅谈QoS--QoS概述 网络抓包工具Wireshark下载安装&使用详细教程 QoS简介 在传统的计算机网络中,网络设备对于数据包的处理方式都是相同的,平等的对待所有的数据流量. ...

  9. win10系统吃鸡游戏服务启动失败,出现 Failed to start Battleye service(0)正确的解决方法

    近期有玩家反馈自己的游戏在启动的过程中一直遇见Failed to start Battleye service(0),在系统服务里启动Battleye service也显示失败,这个问题到底要如何解决 ...

最新文章

  1. 【hibernate系列】采用p6spy+SQLProfiler完整显示hibernate的S...
  2. ​KDD 2020 开源论文 | 图神经网络多变量时序预测
  3. TCL座机日期时间调整
  4. linux禁用IPv6地址
  5. python 源码编译教程_python源码编译
  6. LeetCode算法题-Design LinkedList(Java实现)
  7. [论文写作] vscode + latex
  8. Dubbo--002--例子程序
  9. [CTSC1999][网络流24题]家园
  10. 手把手教你安装Latex(保姆级教程)
  11. 一文读懂hosts文件
  12. arm android开发板推荐,arm7开发板推荐 最新arm开发板了解
  13. 美团校招笔试题-算法8.20-python
  14. 德国计算机留学难度,德国留学,真的很难毕业吗?德国留学生的真实感受
  15. C语言99乘法口诀函数,99乘法口诀妙用C语言输出
  16. 四川大学计算机系统 赵树龙,王俊峰
  17. SAP SHDB(BDC)心得
  18. IO复用模型同步,异步,阻塞,非阻塞及实例详解
  19. RedHat 全部镜像
  20. 会声会影最新版:会声会影2021中文版它来啦!

热门文章

  1. k层交叉检验(k-flod cross-validation)
  2. 3011-基于二叉树的表达式求值(C++,附详细思路)
  3. php将已有图片缩放尺寸,PHP图片重新采样及大小缩放-imagecopyresampled用法实例
  4. mysql的ps.setmaxrows_mysql自定义函数实现表的指定列进行数据脱敏(PS:来自mysql小白的提问)...
  5. java中如何调用属性_java – 如何从属性文件导入值并在注释中使用它?
  6. 11 | 互联网产品的测试策略应该如何设计?
  7. Redis数据类型使用场景及有序集合SortedSet底层实现详解
  8. ThreadLocal T类的说明 转载 原作者 lujh99
  9. C++之第一个程序Hello World,使用IO库输出Hello World
  10. foo( a )JAVA面试题_Java相关面试题总结+答案(五)