Skip to content
Back to Cloud Services

GCP Pub/Sub Messaging

Global messaging — at-least-once delivery, ordering, schema registry, and push/pull subscriptions.

GCPIntermediate

Why

Pub/Sub scales to millions of msgs/sec, global, no broker management. 10GB/mo free. Ordering per key, schema registry for Avro/Protobuf.

Push to HTTP endpoints or pull.

When

Use for event streaming, async work, microservice comms. Avoid for strict ordering across keys (use ordering key) or transactional (use Cloud Spanner).

How

1. Topic — gcloud pubsub topics create my-topic 2. Subscription — pull or push (HTTP endpoint) 3. Publish — gcloud pubsub topics publish my-topic --message "..." 4. Order — --ordering-key, msgs with same key ordered 5. Schema — Avro/Protobuf, validates messages 6. Dead letter — maxDeliveryAttempts, route failures 7. Filter — subscription filter expression, only matching msgs 8. Exactly-once — enable, dedup on message ID

Example

# Create topic + subscription with ordering gcloud pubsub topics create orders --ordering-key-order gcloud pubsub subscriptions create order-processor \ --topic orders \ --enable-message-ordering \ --dead-letter-topic orders-dlq \ --max-delivery-attempts 10 // Publish with ordering key import { PubSub } from "@google-cloud/pubsub"; const pubsub = new PubSub(); await pubsub.topic("orders").publishMessage({ data: Buffer.from(JSON.stringify({ orderId: "456" })), orderingKey: "user-123", // msgs for this user ordered }); // Pull subscription const sub = pubsub.subscription("order-processor"); sub.on("message", (msg) => { process(JSON.parse(msg.data.toString())); msg.ack(); });

Common Problems

  • ⚠Duplicate messages — at-least-once, make consumers idempotent
  • ⚠No global ordering — only per ordering-key, design accordingly
  • ⚠Backpressure — use flow control, don't ack faster than process

Best Practices

  • ✓Make consumers idempotent — at-least-once means duplicates, dedup by ID
  • ✓Use ordering key for related msgs — preserves order per key, parallel across keys
  • ✓Set dead-letter topic — captures failures, prevents infinite retry

FAQ

What is GCP Pub/Sub Messaging and what problem does it solve?

Global messaging — at-least-once delivery, ordering, schema registry, and push/pull subscriptions. It lets developers deploy, store, and scale without self-managing infrastructure — a core reference for cloud service selection.

How do I get started with GCP Pub/Sub Messaging in 5 minutes?

Two steps to run it: (1) Topic — gcloud pubsub topics create my-topic; (2) Subscription — pull or push (HTTP endpoint). Validate on the free tier first, then wire into production.

What is the #1 pitfall with GCP Pub/Sub Messaging?

The most frequent issue is: Duplicate messages — at-least-once, make consumers idempotent. Run a pre-launch checklist item by item, and add policy/config scanning in CI.

What are the best practices for GCP Pub/Sub Messaging?

Best practices for GCP Pub/Sub Messaging include: Make consumers idempotent — at-least-once means duplicates, dedup by ID; Use ordering key for related msgs — preserves order per key, parallel across keys; Set dead-letter topic — captures failures, prevents infinite retry. The first one — "Make consumers idempotent — at-least-once means duplicates, dedup by ID" — should be a team default baked into your IaC templates.

How do I control cost and avoid bill surprises with GCP Pub/Sub Messaging?

Three ways to prevent overspend: (1) tag every resource with env/team and enable billing alarms; (2) set budget thresholds to trigger Slack/email alerts; (3) review idle resources in Cost Explorer regularly and clean them up. Most GCP Pub/Sub Messaging options offer a free tier — validate within free quota before scaling.