Pub/Sub

Asynchronous Messaging

Pub/Sub decouples services: publishers send messages to a topic; every subscription on that topic receives a copy. When an order is placed, the order service publishes once, and email, inventory and analytics each consume it independently.

Example

gcloud pubsub topics create order-placed
gcloud pubsub subscriptions create order-emails --topic=order-placed
gcloud pubsub topics publish order-placed --message='{"orderId": 1001, "total": 499}'
gcloud pubsub subscriptions pull order-emails --auto-ack --limit=1

Example output

messageIds:
- '9876543210123'
┌──────────────────────────────────┬───────────────┐
│               DATA               │   MESSAGE_ID  │
├──────────────────────────────────┼───────────────┤
│ {"orderId": 1001, "total": 499}  │ 9876543210123 │
└──────────────────────────────────┴───────────────┘

Delivery Guarantees

  • At-least-once delivery: make consumers idempotent (processing a message twice must be harmless).
  • Push subscriptions can call a Cloud Run URL for each message.
  • Dead-letter topics collect messages that keep failing.