0
votes

I'm using a Google Cloud Platform Cloud Function. I trigger it via Pub/Sub. In the function logs, the messages are appearing in the order in which they were triggered (newest on top). But if I create a subscription to the published topic and view it in the console like this:

cloud beta pubsub subscriptions pull test_sub --limit 1000 --auto-ack

the messages appear in random order.

Any idea why?

1

1 Answers

3
votes

Google Cloud Pub/Sub does not guarantee the order of messages. There is no attempt to order messages at all. This would break or complicate sharding and clustering of resources.

To quote Google Cloud:

Even in this simple case, guaranteed ordering of messages would put severe constraints on throughput.

For a best-case design, your software should not assume, nor rely upon any specific order for messages. Messages should be atomic units that are not dependent on other messages either before or after. If this is the case for your designs, then you will need to implement time windows and process messages independently of delivery/pull.

For more specific information on Pub/Sub message ordering:

Pub/Sub Ordering messages