Ordering messages | Cloud Pub/Sub Documentation | Google Cloud


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. Acknowledge these messages again.

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 Cloud Console, the gcloud command-line tool, or the Pub/Sub API.

To create a subscription with the message ordering property, follow these steps:

  1. In the Cloud Console, go to the Subscriptions page.

    Go to the Subscriptions page

  2. Click Create subscription.

  3. Enter a Subscription ID.

  4. Choose a topic to receive messages from.

  5. In the Message ordering section, select Order messages with an ordering key.

  6. Click Create.

To create a subscription with the message ordering property, use the gcloud beta pubsub subscriptions create command and the --enable-message-ordering flag:

gcloud beta 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].

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,
}

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.

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.

import ( "context" "fmt" "io" "time" "cloud.google.com/go/pubsub"
) func createWithOrdering(w io.Writer, projectID, subID string, topic *pubsub.Topic) error { // projectID := "my-project-id" // subID := "my-sub" // topic of type https://godoc.org/cloud.google.com/go/pubsub#Topic ctx := context.Background() client, err := pubsub.NewClient(ctx, projectID) if err != nil { return fmt.Errorf("pubsub.NewClient: %v", err) } // Message ordering can only be set when creating a subscription. sub, err := client.CreateSubscription(ctx, subID, pubsub.SubscriptionConfig{ Topic: topic, AckDeadline: 20 * time.Second, EnableMessageOrdering: true, }) if err != nil { return fmt.Errorf("CreateSubscription: %v", err) } fmt.Fprintf(w, "Created subscription: %v\n", sub) return nil
}

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.

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.

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.

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.