Pub/Sub & Observability
Pub/Sub is GCP's asynchronous messaging service with at-least-once delivery; Cloud Logging, Cloud Monitoring and Cloud Trace are the three pillars of observability. Learn topics, subscriptions and acks, then how to query logs, build alerts and trace requests across services.
What is this?
Pub/Sub is GCP's asynchronous messaging service: publishers send messages to topics, and subscribers receive them from subscriptions, with at-least-once delivery and independent scaling of producers and consumers. Cloud Logging collects, stores and queries logs; Cloud Monitoring provides metrics, dashboards and alerting; Cloud Trace records the path of a request across services. Together they form the messaging and observability backbone of GCP applications.
Why do you need it?
- Asynchronous messaging decouples producers from consumers so systems tolerate bursts and partial failures.
- Pub/Sub is the default glue for event-driven architectures, connecting services, Cloud Functions, GKE and BigQuery.
- Observability is how you answer 'is it working?' — logs, metrics and traces are the three pillars.
- Alerts and dashboards turn raw telemetry into something an on-call engineer can act on.
- Every DevOps interview includes at-least-once delivery, retries and observability questions.
Mental model
A post office with tracking
Pub/Sub and observability
Pub/Sub is the mail system: senders (publishers) drop letters into a route (topic) without caring who reads them, and the post office delivers copies to every subscribed postbox (subscription). If a reader is slow, letters queue up — nothing is lost, but delivery is at-least-once, so readers must handle duplicates. Cloud Logging is the archive of every package scanned; Cloud Monitoring is the dashboard of post office throughput; Cloud Trace is the tracking number that follows a single package through every sorting station.
How it works
- Publishers send messages to a topic; Pub/Sub stores and replicates them and fans them out to each subscription.
- Subscribers pull or receive pushed messages; Pub/Sub delivers at-least-once, so consumers must be idempotent.
- Subscriptions can be configured with retention, ack deadlines and dead-letter topics to handle failures.
- Cloud Logging ingests logs from services via agents or APIs and stores them queryable by resource and labels.
- Cloud Monitoring collects metrics such as CPU, latency and uptime, builds dashboards and fires alerts; Cloud Trace records distributed request spans.
A message flowing through Pub/Sub with observability alongside.
Key commands
gcloud pubsub topics create ordersCreate a topic that messages are published to.
gcloud pubsub subscriptions create orders-sub --topic=ordersCreate a subscription to receive messages from a topic.
gcloud pubsub topics publish orders --message="order-12345 placed"Publish a message to a topic.
gcloud pubsub subscriptions pull orders-sub --limit=1Pull pending messages from a subscription.
gcloud logging read "resource.type=gce_instance" --limit=10Query recent logs by resource type.
gcloud monitoring dashboards listList the monitoring dashboards in the project.
Examples
Publish and pull a Pub/Sub message
$ gcloud pubsub topics create ordersCreated topic [projects/my-project/topics/orders]. $ gcloud pubsub subscriptions create orders-sub --topic=ordersCreated subscription [projects/my-project/subscriptions/orders-sub]. $ gcloud pubsub topics publish orders --message="order-12345 placed"messageIds: '1203597848487128' $ gcloud pubsub subscriptions pull orders-sub --limit=1DATA MESSAGE_IDorder-12345 placed 1203597848487128The topic is the category, the subscription is the delivery agreement, and publish drops the message into the system. Pull retrieves one pending message; once your code processes it, it sends an ack so Pub/Sub can drop the message from the backlog.
In production
- Decouple web tiers from background processors with Pub/Sub so bursts do not drop work.
- Use Cloud Functions or Cloud Run subscribers to process messages with automatic retries.
- Centralize logs in Cloud Logging with log-based metrics and retention policies.
- Set up uptime checks and alerting policies in Cloud Monitoring for the services that matter.
- Adopt distributed tracing with Cloud Trace on Cloud Run and GKE to find slow paths.
Best practices
- Make subscribers idempotent because Pub/Sub delivers at-least-once and messages may repeat.
- Use dead-letter topics to isolate poison messages instead of blocking the whole subscription.
- Set ack deadlines and use pull with batching to tune throughput and latency.
- Structure logs with JSON fields so queries and log-based metrics are reliable.
- Add alerting thresholds and notification channels for the top metrics only.
Common mistakes
- Building consumers that are not idempotent and processing the same message twice.
- Ignoring the subscription backlog and discovering large lag during an incident.
- Letting a poison message block the subscription while it retries forever.
- Storing everything in logs without retention and paying for years of junk.
- Alerting on everything, then ignoring all alerts because they never stop firing.
Common issues
Messages accumulate on the subscriptionThe subscriber is slow, down, or failing to ack messages.
Check subscriber logs and the ack deadline, then scale the subscriber or fix the crash; the backlog drains once acks succeed.
Duplicate processing observedPub/Sub delivers at-least-once, and messages can arrive more than once.
Make the consumer idempotent using a business key or message ID, and store processed IDs for deduplication.
No logs appearing in Cloud LoggingThe log is disabled, the agent is missing, or the query filters it out.
Verify the log name and resource label, confirm the logging agent is running, and check the retention window.
Debugging tips
- gcloud pubsub subscriptions describe shows backlog and ack state per subscription.
- Check the message flow by publishing test messages and watching the backlog.
- For logs, scope queries by resource.type and timestamp ranges to keep them fast.
- Use the Cloud Monitoring metrics explorer before writing alerting policies.
- For latency, check Cloud Trace spans for the slow service before guessing at the database.
Interview checkpoint
Hands-on lab
- Model a topic and subscription as directories
- Publish and deliver messages like Pub/Sub
- Acknowledge and deduplicate a backlog
- Generate and analyze timestamped log lines
Simulate a Message Queue and Analyze Logs Locally
25 min · intermediate
Quick quiz
Which delivery guarantee does Pub/Sub provide by default?
A Pub/Sub subscription can have a dead-letter topic that catches messages that repeatedly fail to ack.
A subscriber crashed overnight and the backlog grew. When it restarts, what should it do first?
Cheat sheet
TopicNamed category for messagesSubscriptionDelivery agreement with ack statePublisherSends messages to a topicSubscriberReceives and acks messagesDead-letter topicCatch messages that fail repeatedlyCloud LoggingQueryable logs with labelsCloud MonitoringMetrics, dashboards, alertingCloud TraceDistributed request spansUptime checksSynthetic probes of public endpoints