Pub/Sub Guide
Topics & Subscriptions
# Create topic
gcloud pubsub topics create my-topic
# Create pull subscription
gcloud pubsub subscriptions create my-sub \
--topic=my-topic \
--ack-deadline=60 \
--message-retention-duration=7d \
--expiration-period=never
# Create push subscription
gcloud pubsub subscriptions create my-push-sub \
--topic=my-topic \
--push-endpoint=https://my-service.run.app/push \
--push-auth-service-account=push-sa@my-project.iam.gserviceaccount.com \
--ack-deadline=30
# List topics and subscriptions
gcloud pubsub topics list
gcloud pubsub subscriptions list
# Delete
gcloud pubsub topics delete my-topic
gcloud pubsub subscriptions delete my-sub
Publishing Messages
# Publish single message
gcloud pubsub topics publish my-topic \
--message='{"event":"user_signup","user_id":"123"}' \
--attribute=source=web,version=2
# Publish multiple messages from file
gcloud pubsub topics publish my-topic \
--message="$(cat payload.json)"
# Python SDK example
# from google.cloud import pubsub_v1
# publisher = pubsub_v1.PublisherClient()
# topic_path = publisher.topic_path("my-project", "my-topic")
# future = publisher.publish(
# topic_path,
# b'{"event": "order_placed"}',
# ordering_key="customer-123"
# )
# print(f"Published: {future.result()}")
Pull & Acknowledge
# Pull messages (synchronous)
gcloud pubsub subscriptions pull my-sub --limit=10 --auto-ack
# Pull without auto-ack (then manually ack)
gcloud pubsub subscriptions pull my-sub --limit=5
# Acknowledge message
gcloud pubsub subscriptions ack my-sub \
--ack-ids=ACK_ID_1,ACK_ID_2
# Seek subscription to timestamp (replay messages)
gcloud pubsub subscriptions seek my-sub \
--time="2024-01-15T10:00:00Z"
# Seek to snapshot
gcloud pubsub snapshots create my-snapshot --subscription=my-sub
gcloud pubsub subscriptions seek my-sub --snapshot=my-snapshot
Dead Letter Topics
# Create dead letter topic
gcloud pubsub topics create my-topic-dead-letter
# Create subscription with dead letter policy
gcloud pubsub subscriptions create my-sub \
--topic=my-topic \
--dead-letter-topic=my-topic-dead-letter \
--max-delivery-attempts=5
# Grant Pub/Sub SA permission to forward to DLT
PROJECT_NUMBER=$(gcloud projects describe my-project --format='value(projectNumber)')
gcloud pubsub topics add-iam-policy-binding my-topic-dead-letter \
--member="serviceAccount:service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com" \
--role="roles/pubsub.publisher"
gcloud pubsub subscriptions add-iam-policy-binding my-sub \
--member="serviceAccount:service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com" \
--role="roles/pubsub.subscriber"
Message Ordering
# Enable message ordering on subscription
gcloud pubsub subscriptions create ordered-sub \
--topic=my-topic \
--enable-message-ordering
# Publish with ordering key (messages with same key are ordered)
gcloud pubsub topics publish my-topic \
--message="step-1" \
--ordering-key="user-456"
gcloud pubsub topics publish my-topic \
--message="step-2" \
--ordering-key="user-456"
# Note: ordering requires publishing to the same region
# Use --enable-exactly-once-delivery for stronger guarantees
Flow Control & Monitoring
| Setting | CLI Flag | Description |
|---|---|---|
| Ack deadline | --ack-deadline | Seconds to process before redelivery (10โ600) |
| Max delivery attempts | --max-delivery-attempts | Before forwarding to DLT (5โ100) |
| Retention | --message-retention-duration | How long messages stay in topic (10mโ31d) |
| Filter | --message-filter | CEL expression to filter messages |
| Min backoff | --min-retry-delay | Minimum retry backoff (10sโ600s) |