This document gives an overview of how different types of subscriptions work in Pub/Sub.
To receive messages published to a topic, you must create a subscription to that topic. Only messages published to the topic after the subscription is created are available to subscriber clients. However, you can also enable topic retention to allow a subscription attached to the topic to seek back in time and replay previously published messages. The subscriber client receives and processes the messages published to the topic. A topic can have multiple subscriptions, but a given subscription belongs to a single topic.
After a message is sent to a subscriber, the subscriber must acknowledge the message.
If a message is sent out for delivery and a subscriber is yet to acknowledge it, the message is called outstanding.
Pub/Sub repeatedly attempts to deliver any message that is not yet acknowledged. However, Pub/Sub tries not to deliver an outstanding message to any other subscriber on the same subscription.
The subscriber has a configurable, limited amount of time, known as the
ackDeadline, to acknowledge the outstanding message. After the deadline passes, the message is no longer considered outstanding, and Pub/Sub attempts to redeliver the message.
Types of subscriptions
When you create a subscription, you must specify the type of message delivery. Pub/Sub offers two types of message delivery that corresponds to the following two types of subscriptions. Each type of subscription is described in brief in later sections of this document.
- Pull subscription
- Push subscription
You can update the type of subscription at any time.
For a pull subscription, your subscriber client initiates requests to a Pub/Sub
server to retrieve messages using the REST
API, the REST
StreamingPullRequest API, or the RPC
StreamingPullRequest API. Most subscriber clients do not make these requests directly and
instead rely on the Google Cloud-provided high-level client library that performs streaming pull
requests internally and delivers messages asynchronously. For a subscriber client that needs greater
control over how messages are pulled, Pub/Sub uses a low-level and automatically
generated gRPC library to make pull or streaming pull requests directly. These requests can be
synchronous or asynchronous.
The following two images show the workflow between a subscriber client and a pull subscription.
The pull workflow is as follows and references Figure 1:
The subscriber client explicitly calls the pull method, which requests messages for delivery. This request is the PullRequest as shown in the image.
The Pub/Sub server responds with zero or more messages and acknowledgement IDs. A response with zero messages or with an error does not necessarily indicate that there are no messages available to receive. This response is the PullResponse as shown in the image.
The subscriber client explicitly calls the acknowledged method, using the returned acknowledgment ID to acknowledge that the message is processed and does not need be delivered again. This request is the AckRequest as shown in the image.
The main difference between streaming pull and pull workflows is that for a single streaming pull request, a subscriber client can have multiple responses returned because there is a open connection. In contrast, only one response is returned for each pull request.
For more information about how a pull subscription works and examples of configuration, see Pull subscriptions.
In a push subscription, a Pub/Sub server initiates a request to your subscriber client to deliver messages.
The following image shows the workflow between a subscriber client and a push subscription.
Here is a brief description of the workflow that references Figure 3:
The Pub/Sub server sends each message as an HTTPS request to the subscriber client at a pre-configured endpoint. This request is shown as a PushRequest in the image.
The endpoint acknowledges the message by returning an HTTP success status code. A non-success response indicates that the message must be re-sent. This response is shown as a PushResponse in the image.
Pub/Sub dynamically adjusts the rate of push requests based on the rate at which it receives success responses.
For more information about how a push subscription works and examples of configuration, see Push subscriptions.
Decide on your subscription type
The following table offers some guidance in choosing the appropriate delivery mechanism for your application:
|Endpoints||Any device on the internet that has authorized credentials is able to call the Pub/Sub API.||An HTTPS server with non-self-signed certificate accessible on the public web. The receiving endpoint may be decoupled from the Pub/Sub subscription, so that messages from multiple subscriptions may be sent to a single endpoint.|
|Load balancing||Multiple subscribers can make pull calls to the same "shared" subscription. Each subscriber receives a subset of the messages.||The push endpoint can be a load balancer.|
|Configuration||No configuration is necessary.||No configuration is necessary for App Engine apps in the same project as the subscriber.
Verification of push endpoints is not required in the Google Cloud console. Endpoints must be reachable using DNS names and have SSL certificates installed.
|Flow control||The subscriber client controls the rate of delivery. The subscriber can dynamically modify the acknowledgment deadline, allowing message processing to be arbitrarily long.||The Pub/Sub server automatically implements flow control. There is no need to handle message flow at the client side, although it is possible to indicate that the client cannot handle the current message load by passing back an HTTP error.|
|Efficiency and throughput||Achieves high throughput at low CPU and bandwidth by allowing batched delivery and acknowledgments as well as massively parallel consumption. May be inefficient if aggressive polling is used to minimize message delivery time.||Delivers one message per request and limits the maximum number of outstanding messages.|
Default subscription properties
By default, Pub/Sub offers at-least-once delivery with no ordering guarantees on all subscription types. Alternatively, if messages have the same ordering key and are in the same region, you can enable message ordering. 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.
Pub/Sub also supports exactly-once delivery in preview mode.
In general, Pub/Sub delivers each message once and in the order in which it was published. However, messages may sometimes be delivered out of order or more than once. Accommodating more-than-once delivery requires your subscriber to be idempotent when processing messages.
By default, subscriptions expire after 31 days of subscriber inactivity or if there are no updates made to the subscription. Examples of subscriber activities include open connections, active pulls, or successful pushes. If Pub/Sub detects subscriber activity or an update to the subscription properties, the subscription deletion clock restarts. Using subscription expiration policies, you can configure the inactivity duration or make the subscription persistent regardless of activity. You can also delete a subscription manually.
Although you can create a new subscription with the same name as a deleted one, the new subscription has no relationship to the old one. Even if the deleted subscription had a large number of unacknowledged messages, a new identically-named subscription would have no backlog (no messages waiting for delivery) at the time it is created.
For more information about working with subscriptions, see Create and use subscriptions.