Push Subscription in Pub/Sub: What It Is and How It Works
push subscription in Google Cloud Pub/Sub automatically sends messages to a specified endpoint via HTTP POST as soon as they arrive. This means the subscriber does not need to poll for messages; instead, Pub/Sub pushes messages directly to the subscriber's service.How It Works
Imagine you have a mailbox that automatically delivers letters to your door instead of you having to check the mailbox yourself. A push subscription works like that for messages in Google Cloud Pub/Sub. When a message is published to a topic, Pub/Sub immediately sends it to a web address you provide, called a push endpoint.
This push endpoint is usually a web server or cloud function that listens for incoming HTTP POST requests. When Pub/Sub sends a message, it waits for a confirmation response to know the message was received successfully. If the response is not successful, Pub/Sub retries sending the message until it succeeds or the message expires.
Example
gcloud pubsub subscriptions create my-push-subscription --topic=my-topic --push-endpoint=https://example.com/push-handler --ack-deadline=10
When to Use
Use push subscriptions when you want messages delivered immediately to your service without polling. This is useful for real-time processing, such as updating dashboards, triggering workflows, or sending notifications.
For example, a chat app can use push subscriptions to instantly deliver new messages to users. Or an order system can push new orders to a processing service as soon as they arrive.
Key Points
- Push subscriptions send messages automatically to a web endpoint via HTTP POST.
- The subscriber must have a reachable HTTPS endpoint to receive messages.
- Pub/Sub retries sending messages if the endpoint does not acknowledge receipt.
- Push subscriptions are good for real-time, event-driven architectures.