</>

Pub/Sub — Asynchronous Messaging

Beginner

Decouple producers from consumers with topics, subscriptions, delivery guarantees, and dead-letter queues.

messagingeventingasync

Mental model

Pub/Sub is a fully managed message bus. Producers publish messages to a topic; consumers receive them via a subscription. Producers and consumers are fully decoupled — they don’t know about each other, scale independently, and a slow consumer doesn’t block the producer.

publisher ─▶ TOPIC ─┬─▶ subscription A ─▶ worker(s)   (e.g. process order)
                    └─▶ subscription B ─▶ worker(s)   (e.g. analytics)

Each subscription gets its own copy of every message — add a subscription to fan a topic out to a new consumer without touching the producer.

Delivery model — know these

Commands (read-only)

gcloud pubsub topics create orders
gcloud pubsub subscriptions create orders-worker --topic orders --ack-deadline 30
gcloud pubsub topics publish orders --message '{"id":123}'
gcloud pubsub subscriptions pull orders-worker --auto-ack --limit 5
# Dead-letter after 5 delivery attempts
gcloud pubsub subscriptions create orders-worker \
  --topic orders \
  --dead-letter-topic orders-dlq \
  --max-delivery-attempts 5

Design for redelivery and out-of-order by default. Make handlers idempotent; add ordering keys only when you truly need order (it limits throughput).

Mark your progress with this note: