CodexBloom - Programming Q&A Platform

GCP Pub/Sub Message Ordering Not Working with Multiple Subscribers on Node.js

πŸ‘€ Views: 51 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-07
gcp pubsub node.js JavaScript

After trying multiple solutions online, I still can't figure this out... Quick question that's been bugging me - I'm stuck on something that should probably be simple. I'm currently working on a GCP Pub/Sub implementation where I need to maintain the order of messages sent to multiple subscribers. I have set up a topic and a subscription, and I'm using the Node.js client library `@google-cloud/pubsub` version 2.19.0. I thought that by setting `enableMessageOrdering` to true when creating the subscription, the messages would be delivered in order to each subscriber. However, I'm observing that messages are being processed out of order when two subscribers are pulling messages concurrently. Here’s the code I used to create the subscription: ```javascript const { PubSub } = require('@google-cloud/pubsub'); const pubsub = new PubSub(); async function createSubscription() { const topicName = 'my-topic'; const subscriptionName = 'my-subscription'; const options = { enableMessageOrdering: true, flowControl: { maxMessages: 10, }, }; await pubsub.topic(topicName).subscription(subscriptionName, options).create(); console.log(`Subscription ${subscriptionName} created with message ordering enabled.`); } createSubscription().catch(console.error); ``` When I publish messages with the same ordering key, I expect that each subscriber would receive them in the same order. I’m using a simple publishing routine like this: ```javascript async function publishMessages() { const topic = pubsub.topic('my-topic'); const messages = ['msg1', 'msg2', 'msg3']; messages.forEach(async (msg, index) => { const dataBuffer = Buffer.from(msg); await topic.publish(dataBuffer, { orderingKey: 'order-key' }); console.log(`Message ${index + 1} published.`); }); } publishMessages().catch(console.error); ``` Upon testing, I've noticed that the subscribers process messages in different orders, which is not what I expected. The logs show that subscriber A processed `msg2` before `msg1`. I also checked the GCP console, and it confirms that ordering key is applied correctly. Could this behavior be caused by the parallel processing of messages by the subscribers? Is there a limitation or a specific configuration I might be missing to ensure strict ordering across multiple subscribers that I am not aware of? Any help would be appreciated! My team is using Javascript for this CLI tool. I appreciate any insights! Any advice would be much appreciated. Thanks for taking the time to read this! What would be the recommended way to handle this?