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.
Console
To create a subscription with the message ordering property, follow these steps:
-
In the 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 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].
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, }
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.
from Hacker News https://ift.tt/2Joe2bc
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.