Shared subscription

Shared subscription is a subscription method that achieves load balancing among multiple subscribers:

  1. [subscriber1] got msg1
  2. msg1, msg2, msg3 /
  3. [publisher] ----------------> "$share/g/topic" -- [subscriber2] got msg2
  4. \
  5. [subscriber3] got msg3

In the above picture, three subscribers subscribe to the same topic $share/g/topic using a shared subscription method, where topic is the real topic name they subscribed to, and $share/g/ is a shared subscription Prefix. EMQ X Broker supports shared subscription prefixes in two formats:

ExamplePrefixReal topic name
$queue/t/1$queue/t/1
$share/abc/t/1$share/abct/1

Shared subscription with groups

Shared subscriptions prefixed with $ share/<group-name> are shared subscriptions with groups:

group-name can be any string. Subscribers who belong to the same group will receive messages with load balancing, but EMQ X Broker will broadcast messages to different groups.

For example, suppose that subscribers s1, s2, and s3 belong to group g1, and subscribers s4 and s5 belong to group g2. Then when EMQ X Broker publishes a message msg1 to this topic:

  • EMQ X Broker will send msg1 to both groups g1 and g2
  • Only one of s1, s2, s3 will receive msg1
  • Only one of s4 and s5 will receive msg1
  1. [s1]
  2. msg1 /
  3. [emqx] ------> "$share/g1/topic" - [s2] got msg1
  4. | \
  5. | [s3]
  6. | msg1
  7. ----> "$share/g2/topic" -- [s4]
  8. \
  9. [s5] got msg1

Shared subscription without group

Shared subscriptions prefixed with $queue/ are shared subscriptions without groups. It is a special case of $share subscription, which is quite similar to all subscribers in a subscription group:

  1. [s1] got msg1
  2. msg1,msg2,msg3 /
  3. [emqx] ---------------> "$queue/topic" - [s2] got msg2
  4. \
  5. [s3] got msg3

Balancing strategy and distribution of Ack configuration

EMQ X Broker’s shared subscription supports balancing strategy and distribution of Ack configuration:

  1. # etc/emqx.conf
  2. # balancing strategy
  3. broker.shared_subscription_strategy = random
  4. # Applicable to QoS1 QoS2 messages, when enabled, message will be distributed to another group when one group is offline
  5. broker.shared_dispatch_ack_enabled = false
Balancing strategyDescription
randomSelect randomly among all subscribers
round_robinAccording to the order of subscription
stickyAlways sent to the last selected subscriber
hashAccording to the hash value of the publisher ClientID

TIP

Whether it is a single client subscription or a shared subscription, pay attention to the client performance and message reception rate, otherwise it will cause errors such as message accumulation and client crash.