This page explains how to receive messages in order.
For more information about receiving messages, see the subscriber overview.
Receiving messages in order
If messages have the same ordering key and are in the same region, you can enable message ordering and receive the messages in the order that the Pub/Sub service receives them.
Pub/Sub delivers each message at least once, so the Pub/Sub service might redeliver messages. When you receive messages in order and the Pub/Sub service redelivers a message with an ordering key, Pub/Sub maintains order by also redelivering the subsequent messages with the same ordering key. The Pub/Sub service redelivers these messages in the order that it originally received them.
When the Pub/Sub service redelivers a message with an ordering key, the Pub/Sub service also redelivers every subsequent message with the same ordering key, including acknowledged messages. If both message ordering and a dead-letter topic are enabled on a subscription, this may not be true, as Pub/Sub forwards messages to dead-letter topics on a best-effort basis. When redelivered messages are received, you must acknowledge these messages again. There is no guarantee that subsequent messages will be sent without the previous ones being acknowledged.
Ordered keys and message delivery
Messages with the same ordering key are guaranteed to be delivered in order.
Messages with different ordering keys are not guaranteed to be delivered in order, independent of the publishing time.
The inability to acknowledge a message might hold up the delivery of messages for other ordering keys. This issue is possible when servers restart unexpectedly or there are changes in the set of servers used due to traffic changes. To preserve order across such events, all messages published to the old server must be acknowledged before messages from the new server are delivered, even if they are for different ordering keys. If you cannot ensure timely acknowledgment of all messages, consider attaching a dead-letter topic to the subscription. Order of messages might not be preserved when they are written to the dead-letter topic.
Enabling message ordering
To receive the messages in order, set the message ordering property on the subscription you receive messages from. Receiving messages in order might increase latency.
You can set the message ordering property when you create a subscription using the Google Cloud console, the Google Cloud CLI, or the Pub/Sub API.
Console
To create a subscription with the message ordering property, follow these steps:
In the Google Cloud console, go to the Subscriptions page.
Click Create subscription.
Enter a Subscription ID.
Choose a topic to receive messages from.
In the Message ordering section, select Order messages with an ordering key.
Click Create.
gcloud
To create a subscription with the message ordering property, use the
gcloud pubsub subscriptions create
command and the --enable-message-ordering
flag:
gcloud pubsub subscriptions create SUBSCRIPTION_ID \ --enable-message-ordering
Replace SUBSCRIPTION_ID with the ID of the subscription.
If the request is successful, the command line displays a confirmation:
Created subscription [SUBSCRIPTION_ID].
REST
To create a subscription with the message ordering property, send a
PUT
request like the following:
PUT https://pubsub.googleapis.com/v1/projects/PROJECT_ID/subscriptions/SUBSCRIPTION_ID Authorization: Bearer $(gcloud auth application-default print-access-token)
Replace the following:
- PROJECT_ID: the project ID of the project with the topic
- SUBSCRIPTION_ID: the ID of the subscription
In the request body, specify the following:
{ "topic": TOPIC_ID, "enableMessageOrdering": true, }
Replace TOPIC_ID with the ID of the topic to attach to the subscription.
If the request is successful, the response is the subscription in JSON format:
{ "name": projects/PROJECT_ID/subscriptions/SUBSCRIPTION_ID, "topic": projects/PROJECT_ID/topics/TOPIC_ID, "enableMessageOrdering": true, }
C++
Before trying this sample, follow the C++ setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub C++ API reference documentation.
C#
Before trying this sample, follow the C# setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub C# API reference documentation.
Go
Before trying this sample, follow the Go setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Go API reference documentation.
Java
Before trying this sample, follow the Java setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Java API reference documentation.
Node.js
Before trying this sample, follow the Node.js setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Node.js API reference documentation.
Python
Before trying this sample, follow the Python setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Python API reference documentation.
Ruby
Before trying this sample, follow the Ruby setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Ruby API reference documentation.
After the message ordering property is set, the Pub/Sub service delivers messages with the same ordering key in the order that the Pub/Sub service receives the messages. For example, if a publisher sends two messages with the same ordering key, the Pub/Sub service delivers the oldest message first.
What's next
Read the blog post about ordered delivery.
Monitor your subscription.
Read about publishing messages with ordering keys.