0
0
GCPcloud~5 mins

Pull vs push subscriptions in GCP - CLI Comparison

Choose your learning style9 modes available
Introduction
When you want to receive messages from a topic, you can choose how to get them. Pull subscriptions let you ask for messages when you are ready. Push subscriptions send messages to your app automatically. This helps you control how your app handles incoming data.
When your app wants to control when it processes messages to avoid overload.
When you want messages sent directly to your web server without asking.
When you have a service that can handle incoming HTTP requests instantly.
When you want to pull messages in batches for efficient processing.
When you want to avoid managing a web endpoint and prefer to fetch messages manually.
Commands
Create a pull subscription named 'my-pull-sub' for the topic 'my-topic'. The ack-deadline is 20 seconds, meaning you have 20 seconds to acknowledge a message after pulling it.
Terminal
gcloud pubsub subscriptions create my-pull-sub --topic=my-topic --ack-deadline=20
Expected OutputExpected
Created subscription [projects/your-project/subscriptions/my-pull-sub].
--ack-deadline - Sets how long you have to acknowledge a pulled message.
Create a push subscription named 'my-push-sub' for the topic 'my-topic'. Messages will be sent automatically to the specified HTTPS endpoint.
Terminal
gcloud pubsub subscriptions create my-push-sub --topic=my-topic --push-endpoint=https://example.com/push-handler
Expected OutputExpected
Created subscription [projects/your-project/subscriptions/my-push-sub].
--push-endpoint - Specifies the URL where messages will be pushed.
Pull up to 5 messages from the 'my-pull-sub' subscription and automatically acknowledge them so they are removed from the queue.
Terminal
gcloud pubsub subscriptions pull my-pull-sub --limit=5 --auto-ack
Expected OutputExpected
Received message: "Hello world 1" Received message: "Hello world 2" Received message: "Hello world 3" Received message: "Hello world 4" Received message: "Hello world 5"
--limit - Limits the number of messages pulled.
--auto-ack - Automatically acknowledges messages after pulling.
Show details about the push subscription 'my-push-sub', including its push endpoint and configuration.
Terminal
gcloud pubsub subscriptions describe my-push-sub
Expected OutputExpected
name: projects/your-project/subscriptions/my-push-sub pushConfig: pushEndpoint: https://example.com/push-handler ackDeadlineSeconds: 10 topic: projects/your-project/topics/my-topic
Key Concept

If you remember nothing else from this pattern, remember: pull subscriptions let your app ask for messages when ready, while push subscriptions send messages automatically to your app's endpoint.

Common Mistakes
Creating a push subscription without a valid HTTPS endpoint.
Messages cannot be delivered if the endpoint is missing or not reachable, causing message loss or retries.
Always provide a reachable HTTPS URL that can accept POST requests for push subscriptions.
Not acknowledging pulled messages in pull subscriptions.
Messages remain unacknowledged and will be redelivered, causing duplicate processing.
Use --auto-ack flag or explicitly acknowledge messages after processing.
Setting too short ack-deadline for pull subscriptions.
Your app may not finish processing before the deadline, causing message redelivery.
Set ack-deadline long enough for your processing time, or extend it dynamically.
Summary
Use 'gcloud pubsub subscriptions create' with --push-endpoint to make a push subscription that sends messages automatically.
Use 'gcloud pubsub subscriptions create' without --push-endpoint to make a pull subscription that waits for your app to ask for messages.
Pull messages with 'gcloud pubsub subscriptions pull' and acknowledge them to remove from the queue.
Check subscription details with 'gcloud pubsub subscriptions describe' to verify configuration.