GCP Pub/Sub Cheat Sheet
Topics, subscriptions, gcloud commands, and client library snippets for asynchronous messaging on Google Cloud Pub/Sub.
2 PagesIntermediateFeb 27, 2026
Create Topic & Subscription
Provision a topic and a pull subscription with gcloud.
bash
gcloud pubsub topics create orders-topicgcloud pubsub subscriptions create orders-sub \ --topic=orders-topic \ --ack-deadline=30 \ --message-retention-duration=7d# Push subscription instead of pullgcloud pubsub subscriptions create orders-push-sub \ --topic=orders-topic \ --push-endpoint=https://api.example.com/pubsub/push
Publish a Message (Python)
Publish JSON data with attributes using the google-cloud-pubsub client.
python
from google.cloud import pubsub_v1import jsonpublisher = pubsub_v1.PublisherClient()topic_path = publisher.topic_path("my-project", "orders-topic")data = json.dumps({"orderId": "o-123"}).encode("utf-8")future = publisher.publish(topic_path, data, priority="high")print(future.result()) # message ID
Pull & Ack Messages (Python)
Synchronously pull messages and acknowledge them after processing.
python
from google.cloud import pubsub_v1subscriber = pubsub_v1.SubscriberClient()sub_path = subscriber.subscription_path("my-project", "orders-sub")def callback(message): print(message.data.decode("utf-8")) message.ack()future = subscriber.subscribe(sub_path, callback=callback)try: future.result(timeout=60)except TimeoutError: future.cancel()
Core Concepts
Key Pub/Sub terms and delivery guarantees.
- Topic- named resource publishers send messages to
- Subscription- pull or push delivery channel attached to a topic
- Ack deadline- window to acknowledge before Pub/Sub redelivers (default 10s)
- At-least-once delivery- consumers must be idempotent; duplicates are possible
- Dead-letter topic- routes messages after max delivery attempts are exceeded
- Ordering keys- guarantee per-key ordering when enabled on the subscription
Pro Tip
Set `message-retention-duration` and enable `--enable-message-ordering` only on subscriptions that need replay or per-key ordering — both features add latency and cost that most fire-and-forget event flows don't need.
Was this cheat sheet helpful?
Explore Topics
#GCPPubSub#GCPPubSubCheatSheet#CloudComputing#Intermediate#CreateTopicSubscription#PublishAMessagePython#Pull#Ack#Concurrency#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance