You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Google Cloud Pub/Sub supports two primary delivery mechanisms for messages: pull and push subscriptions. Choosing the right delivery mode is a critical architectural decision that affects latency, scalability, cost, and operational complexity. This lesson covers both modes in depth, comparing their characteristics and providing guidance on when to use each.
With a pull subscription, the subscriber actively requests messages from Pub/Sub. The subscriber calls the pull API (or uses streaming pull) and Pub/Sub returns available messages. The subscriber processes each message and sends an acknowledgement.
Pull or StreamingPull to request messagesAcknowledge request for each successfully processed message| Feature | Streaming Pull | Unary Pull |
|---|---|---|
| Connection | Long-lived bidirectional gRPC stream | Short-lived request-response |
| Latency | Low — messages delivered as they arrive | Higher — must poll periodically |
| Throughput | High — continuous flow of messages | Lower — limited by poll frequency |
| Complexity | More complex (connection management) | Simpler to implement |
| Recommended | Yes — used by all client libraries | Only for simple scripts |
The official Google Cloud client libraries use streaming pull by default. This opens a long-lived gRPC connection and receives messages as they are published, providing near-real-time delivery with high throughput.
from google.cloud import pubsub_v1
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path("my-project", "my-sub")
def callback(message):
print(f"Received: {message.data.decode()}")
message.ack()
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print(f"Listening on {subscription_path}...")
try:
streaming_pull_future.result()
except KeyboardInterrupt:
streaming_pull_future.cancel()
streaming_pull_future.result()
const { PubSub } = require("@google-cloud/pubsub");
const pubsub = new PubSub();
const subscription = pubsub.subscription("my-sub");
subscription.on("message", (message) => {
console.log("Received:", message.data.toString());
message.ack();
});
subscription.on("error", (error) => {
console.error("Error:", error);
});
The acknowledgement deadline is the time a subscriber has to process and acknowledge a message before Pub/Sub redelivers it. The default is 10 seconds, but you can configure it from 10 to 600 seconds.
# Set ack deadline to 120 seconds
gcloud pubsub subscriptions update my-sub --ack-deadline=120
Client libraries also support deadline extension (modifyAckDeadline), which automatically extends the deadline while the message is being processed. This prevents redelivery of messages that take longer than the configured deadline.
With a push subscription, Pub/Sub sends messages to a subscriber endpoint via HTTPS POST requests. The subscriber does not need to poll — Pub/Sub proactively delivers messages as they arrive.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.