来自: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服务质量 (Quality of Service 0, 1 & 2)概念学习

    什么是 QoS ? QoS (Quality of Service) 是发送者和接收者之间,对于消息传递的可靠程度的协商. QoS 的设计是 MQTT 协议里的重点.作为专为物联网场景设计的协议,MQ ...

  2. [转] Quality Of Service In OpenStack

    http://tropicaldevel.wordpress.com/2013/07/15/quality-of-service-in-openstack/ In this post I will b ...

  3. neutron qos Quality of Service

    Quality of Service advanced service is designed as a service plugin. The service is decoupled from t ...

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

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

  5. HP quality center 9.0 邮件设置

    [转载]HP quality center 9.0 邮件设置 (2010-09-20 10:28:03) 转载▼ 标签: 转载   原文地址:HP quality center 9.0 邮件设置作者: ...

  6. Quality of service

    w https://en.wikipedia.org/wiki/Quality_of_service Quality of service (QoS) is the overall performan ...

  7. [译]Ocelot - Quality of Service

    原文 可以针对每个ReRoute设置对下游服务的熔断器circuit breaker.这部分是通过Polly实现的. 将下面的配置添加到一个ReRoute下面去. "QoSOptions&q ...

  8. Quality of Service (QoS) in LTE

    Background: Why we need QoS ? There are premium subscribers who always want to have better user expe ...

  9. ITU-T G.1080 IPTV的体验质量(QoE)要求 (Quality of experience requirements for IPTV services)

    IPTV的服务质量(QoE)要求 Quality of experience requirements for IPTV services Summary This Recommendation de ...

随机推荐

  1. pytest十:用例 a 失败,跳过测试用例 b 和 c 并标记失败 xfail

    当用例 a 失败的时候,如果用例 b 和用例 c 都是依赖于第一个用例的结果,那可以直接跳过用例 b 和 c 的测试,直接给他标记失败 xfail用到的场景,登录是第一个用例,登录之后的操作 b 是第 ...

  2. python 全栈开发,Day8(文件操作)

    一.文件操作流程 文件以什么编码存储的,就以什么编码打开 参数: 1.文件路径 2.编码方式,encode 3.执行动作(打开方式):只读,只写,追加,读写,写读... 打开一个已经存在的文件 f = ...

  3. 《剑指offer》-整数中1出现的次数

    题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...

  4. hdu 1181 以b开头m结尾的咒语 (DFS)

    咒语是以a开头b结尾的一个单词,那么它的作用就恰好是使A物体变成B物体现在要将一个B(ball)变成一个M(Mouse),比如 "big-got-them". Sample Inp ...

  5. 安装oracle11g时遇到INS-13001环境不满足最低要求

    在安装oracle11g,点击setup.exe之后,弹出了如下提示框: 解决方法: 首先,打开你解压后的database文件夹,找到stage,然后cvu,找到cvu_prereq.xml文件,用记 ...

  6. Codeforces 679C Bear and Square Grid

    Bear and Square Grid 枚举k * k 的位置, 然后接上它周围白色连通块的数量, 再统计完全在k * k范围里的连通块, 这个只要某个连通块全部的方格 在k * k里面就好, 并且 ...

  7. In Action HDU3339

    这是最短路问题和01背包问题的相结合 第一次用01背包 把j打成了i检查了半个小时  下次要注意! 使用的油耗相当于容量  而power相当于价值 先用dijkstra把从基地到所有路的最短情况算出来 ...

  8. 【Java】 剑指offer(20) 表示数值的字符串

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如, ...

  9. TotoriseGit安装

    1.前提 前提是有安装过git. 2.下载 3.安装 二:参考的文档 1.不错的文档 https://www.cnblogs.com/xinlj/p/5978730.html http://blog. ...

  10. CI入门

    CI入门 一.[查]按条件获取一条记录 获取数据有返回数组形式或对象形式,row_array().result_array()是以数组形式返回:row().result()是以对象形式返回.同样的,更 ...