Last Updated on : 2025-02-21 02:04:56download
Tuya IoT message queue is a message middleware that is deeply customized based on the open source message component Apache Pulsar and adapted to Tuya’s business scenarios. It retains many features of Pulsar.
A topic is the subject of production and consumption in the Tuya IoT message queue. Subscription is equivalent to a consumer group under a topic. In the same subscription, a message will only be consumed and acknowledged once by a consumer. After you enable the Message Service in your cloud project, the system automatically creates a topic and a default subscription.
A Pulsar cluster consists of at least three parts:
ZK, the metadata storage and configuration center
Bookie, the persistent storage of messages
Broker, the stateless proxy component
{persistent|non-persistent}://tenant/namespace/topic
tenant
: Defaults to the accessId
/clientId
of the current cloud project.namespace
: Defaults to out
.topic
: The production channel topic is event
. It is also a partitioned topic with 11 partitions. The test channel topic is event-test
, which is a non-partitioned topic.tenant-suffix
tenant
: Defaults to the accessId
/clientId
of the current cloud project.suffix
: The suffix of the subscription name. The default value is sub
.Topics in Pulsar are divided into partitioned topics and non-partitioned topics.
There are four subscription types in Pulsar: Exclusive, Failover, Shared, and Key_Shared.
Exclusive: The exclusive type is a subscription type that only allows a single consumer to attach to the subscription. If multiple consumers subscribe to a topic using the same subscription, an error occurs. Note that if the topic is partitioned, all partitions will be consumed by the single consumer allowed to be connected to the subscription.
The exclusive type is suitable for globally ordered messages. However, this subscription type is not recommended if the consumption capacity is limited.
Failover: The failover type is a subscription type that multiple consumers can attach to the same subscription. However, at any given time, a master consumer is picked for a non-partitioned topic or each partition of a partitioned topic and receives messages. When the master consumer disconnects, all (non-acknowledged and subsequent) messages are delivered to the next consumer in line.
For partitioned topics, the broker sorts consumers by priority and lexicographical order of consumer name.
acknowledgeCumulative
. acknowledgeCumulative
is a bulk acknowledgement mechanism. The consumer acknowledges all unacknowledged messages before the message in one operation.If the number of consumers is greater than the number of partitions:
If the number of consumers is less than the number of partitions:
Shared: The shared subscription type in Pulsar allows multiple consumers to attach to the same subscription. Messages are delivered in a round-robin distribution across consumers, and any given message is delivered to only one consumer. When a consumer disconnects, all the messages that were sent to it and not acknowledged will be rescheduled for sending to the remaining consumers.
Shared subscriptions do not guarantee message ordering or support cumulative acknowledgment.
Key_Shared: The Key_Shared subscription type in Pulsar allows multiple consumers to attach to the same subscription. Messages in the Key_Shared type are delivered in distribution across consumers and messages with the same key or same ordering key are delivered to only one consumer. No matter how many times the message is re-delivered, it is delivered to the same consumer.
The Key_Shared subscription type ensures that the same key is processed by a single consumer at any given time.
When a new consumer is connected and thus added to the list of connected consumers, the algorithm re-adjusts the mapping such that some keys currently mapped to existing consumers will be mapped to the newly added consumer. Meanwhile, the broker will record the position of the newest message, such as X, and associate it with the new consumer. The broker will only start delivering messages to the new consumer after all the messages before X have been acknowledged.
Consumers can enable allowOutOfOrderDelivery
to disable ordering, thereby ensuring that new consumers can consume messages immediately.
The production channels of the Message Service are all partitioned topics, and the default number of partitions is 11.
acknowledgeCumulative
is recommended to acknowledge messages. This reduces unexpected scenarios where some messages are not acknowledged, resulting in consumption accumulation.It is normal to have a small amount of message backlog. When messages are delivered from the producer, Pulsar, and to the consumer, a small amount of messages might not be acknowledged. These messages have not reached the consumer or have not been acknowledged by the consumer. They will be counted as a backlog. Typically, when the message production rate is uniform, a message backlog might occur for milliseconds to several seconds.
A large amount of messages might backlog because the consumption speed cannot keep up. By default, in the production environment, 11 Pulsar consumers can be started to achieve the maximum consumption rate. If there are already more than 11 consumers, you can observe the time consumption of local business processing logic by adding logs and monitoring tools. Find out the bottleneck and modify the consumption code.
If the consumption rate matches the production rate but there is still a backlog, you can try restarting the local consumption service. Alternatively, you can check the consumption logic to see if any scenarios might prevent some messages from being acknowledged.
Pulsar does not support broadcast subscription consumption mode. That is, it is impossible to use the same subscription to allow all consumers to receive the same message. If you need to start multiple consumers and make them receive the same message, it is recommended that you subscribe to receive messages individually. After the consumers receive the message, the distribution logic is implemented in the code.
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback