CodexBloom - Programming Q&A Platform

GCP Cloud Pub/Sub message ordering guide when using Spring Boot with async subscription

๐Ÿ‘€ Views: 56 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-09
gcp pubsub spring-boot Java

I've looked through the documentation and I'm still confused about I've encountered a strange issue with I'm working with a important scenario with message ordering in GCP Cloud Pub/Sub while using Spring Boot's async subscription feature... Despite enabling the 'orderingKey' in my Pub/Sub topic, messages aren't being processed in the expected order. For instance, when I publish messages with the same ordering key, they appear to be delivered out of order to my subscribers. Hereโ€™s a snippet of how Iโ€™m publishing messages: ```java String orderingKey = "my-ordering-key"; PubsubMessage message = PubsubMessage.newBuilder() .setData(ByteString.copyFromUtf8("My message")) .setOrderingKey(orderingKey) .build(); publisher.publish(message); ``` Iโ€™ve confirmed that the topic is set up with ordering enabled and that my subscription is using the same ordering key. On the subscriber side, Iโ€™m using the Spring Cloud GCP library version 2.0.0, with the following configuration: ```yaml spring: cloud: gcp: pubsub: subscriber: subscriber-name: my-subscriber async: true ``` I also tried setting the `maxOutstandingMessages` property in the subscriber configuration to see if that would help with the ordering: ```yaml max-outstanding-messages: 10 ``` However, I still notice that the order of the messages is inconsistent. Sometimes, I see messages processed in the correct order, but other times they arrive out of sequence. Iโ€™ve verified that there's no other processing that could affect the order. Any suggestions on what might be going wrong or how to better configure the subscriber to ensure strict ordering? Thanks in advance! I recently upgraded to Java 3.11. Thanks for taking the time to read this!