If you publish messages to the global Pub/Sub endpoint, Pub/Sub automatically stores the messages in the nearest Google Cloud region. If you want to control the regions in which your messages are stored and processed, you can configure a message storage policy on your topic.
Message storage policy overview
You can set a message storage policy when you create a new topic or when you update a topic using the console, Google Cloud CLI, or the REST APIs.
The message storage policy applies only to message contents. The policy does not apply to other data such as topic names, labels, or Identity and Access Management (IAM) settings.
Pub/Sub stores messages when a client publishes the messages to Pub/Sub. A message storage policy ensures that Pub/Sub stores and processes messages only in the set of Google Cloud regions that you specify, regardless of where the publish or subscribe requests originate. If the policy allows multiple regions for publish operations, Pub/Sub stores the message in an allowed region closest to where the published message enters the Google Cloud network.
When you specify a message storage policy, you can set
enforceInTransit to True. This flag governs the following:
Publish, pull, and streamingPull requests received in a region not allowed in the message storage policy are rejected with a
FAILED_PRECONDITIONerror.If a client runs within Google Cloud in one of the allowed regions — for example, in a Compute Engine VM — it can use the global endpoint. The client's requests are routed locally in the allowed region. The client can also use a locational endpoint or regional endpoint that targets an allowed region.
If a client runs within Google Cloud in a region that isn't allowed, or runs outside of Google Cloud, it must use a locational or regional endpoint that targets a region in the allowed list of regions.
Delivery for push subscriptions is handled only within the allowed Cloud regions. In some cases, this restriction can completely pause message delivery for push subscriptions. When a push subscription enters such a state due to the push locations being overly constrained by a combination of factors such as message storage location, allowed regions, and export resource location, this state becomes visible in Stackdriver.
Message storage policies for new topics
If you don't specify a message storage policy when you create a topic, the message storage policy is automatically determined based on the effective Resource Location Restriction organization policy. When no organization policy is in effect, the message storage policy allows all regions.
Similarly, in the absence of a specified message storage policy, the
enforceInTransitflag is determined based on the effective Enforce in-transit regions for Pub/Sub messages organization policy. For more information about this organization policy, see Organization policy constraints.If you specify a message storage policy when you create a topic, the message storage policy can contain only the regions allowed by the effective Resource Location Restriction organization policy. When no organization policy is in effect, the message storage policy can contain any region.
Message storage policies for existing topics
When an organization policy is updated, the changes do not automatically propagate to existing topics. As such, an existing topic's message storage policy can get out of sync with the latest organization policy. For more information, see Manage differences between organization and topic policies.
When a topic's message storage policy is updated, the changes do not propagate to already-published messages. Messages already stored based on an older policy are not moved to be consistent with the new policy. Rather, the changes apply only to messages published after the update.
Exceptions
The policy specifies a list of allowed Google Cloud region names. As such, the following items are not supported:
- Exclusion lists
- Zones or multi-region locations
If you publish a message with an ordering key and the message storage policy excludes the nearest region, the Pub/Sub service returns an error.
Configure message storage policies
There are two ways to configure message storage policies for topics, including:
- Set a message storage policy using an organization policy.
- Configure a message storage policy when creating a topic.