Search code examples
google-cloud-platformpublish-subscribegoogle-cloud-pubsub

Google Pub/Sub sending to only one subscription every ~5 messages


I've made 3 clients connected to a subscription, and one publisher. In the image 2 of the subscriptions are on the terminal, and one subscription is not seen as it is hosted on a DigitalOcean Droplet. It seems every 5 messages, it switches which subscriber actually receives the message, which should not happen. I've also varied the speed and it's always about 5 messages.

Here is the code used on all clients for subscriptions:

sub.on("message", (msg) => {
  console.log(`Message:1 ${msg.data.toString("utf-8")}`)
  msg.ack()
})

And here is the code for publishing:

console.log("send")
topic.publish(Buffer.from("hey"), {
        channelId: "641273551806267403"
    })

enter image description here


Solution

  • In Cloud Pub/Sub, a subscription is a logical entity that wants all messages published to the topic with which the subscription is associated. A subscriber is a client that receives messages on behalf of a subscription. When there are multiple subscribers receiving messages for a single subscription, then each subscriber receives a subset of the messages. This is the load balancing case, where one uses multiple subscribers to process messages at scale; if more messages need to be supported, one just turns up more subscribers to receive messages from the same subscription.

    When a topic has multiple subscriptions, then every message has to be sent to a subscriber receiving messages on behalf of each subscription. This is the fan out use case.

    Here is a graphic that tries to make it a little clearer. The left side is load balancing, the right side is fan out:

    load balancing and fan out