CodexBloom - Programming Q&A Platform

GCP Pub/Sub message ordering issues when using Java client with multiple subscribers

👀 Views: 183 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-09
google-cloud-pubsub java message-ordering Java

I've been banging my head against this for hours... I'm experimenting with I've been struggling with this for a few days now and could really use some help... I've looked through the documentation and I'm still confused about I'm working with issues with message ordering when using Google Cloud Pub/Sub in my Java application. I've set up a topic and a subscription configured for message ordering, but I'm observing that messages are not being processed in the order they are published. Here's a snippet of how I'm publishing messages in sequence: ```java Publisher publisher = Publisher.newBuilder(topicName).build(); for (int i = 0; i < 10; i++) { String message = "Message " + i; PubsubMessage pubsubMessage = PubsubMessage.newBuilder() .setData(ByteString.copyFromUtf8(message)) .setOrderingKey("order-key") .build(); publisher.publish(pubsubMessage); } publisher.shutdown(); ``` I'm using the Java client library version `1.113.1`. I've verified that the subscription is set with the `enableMessageOrdering` option. However, when I consume the messages with multiple subscribers, I'm not receiving them in the expected order. Here's how I'm consuming them: ```java MessageReceiver receiver = (message, consumer) -> { System.out.println("Received: " + message.getData().toStringUtf8()); consumer.ack(); }; Subscriber subscriber = Subscriber.newBuilder(subscriptionName, receiver).build(); subscriber.startAsync().awaitRunning(); ``` I've tried using a single subscriber to see if that resolves the scenario, but I'm still working with the same question. Additionally, I checked the message retention duration and ensured it's set appropriately. Can anyone shed light on why the ordering might unexpected result with multiple subscribers? Are there specific configurations or best practices I should follow to ensure proper message ordering in Pub/Sub? Any insights would be greatly appreciated! This is part of a larger API I'm building. What's the best practice here? I'm using Java latest in this project. I'd love to hear your thoughts on this. This is happening in both development and production on macOS. Could someone point me to the right documentation?