Skip to main content
Welcome back. In this lesson we’ll examine how applications and services connect to Google Cloud Pub/Sub, the managed messaging service for asynchronous communication. You’ll learn where connections occur, how client libraries communicate with the service, recommended authentication patterns, commonly used publish/subscribe designs, code examples in popular languages, and operational best practices.

Architecture overview

Connections to Cloud Pub/Sub happen in two primary places:
  • Publisher side — applications that send messages (for example, microservices, data collection APIs, or logging agents like Fluent Bit).
  • Subscriber side — applications that receive messages from a subscription (for example, other microservices, data pipeline workers, or serverless functions).
Both publishers and subscribers use Google Cloud client libraries. Think of the client library as a language-specific connector that handles authentication, retries, batching, flow control, and network details so your application can focus on message semantics.

How client libraries communicate

  • Protocols: gRPC is used by default for performance and low latency. REST/JSON endpoints are also available for certain use cases and tooling.
  • Endpoint: Client libraries communicate with the Pub/Sub endpoint at pubsub.googleapis.com unless you explicitly configure a different endpoint.
  • Responsibilities handled by libraries: authentication (Application Default Credentials or explicit service account keys), automatic retries, publish batching, and subscription flow control/backpressure.

Authentication and IAM

Application Default Credentials (ADC) are the recommended authentication method:
  • In Google Cloud runtimes (Compute Engine, GKE, Cloud Run, Cloud Functions), ADC is usually available automatically.
  • When running outside Google Cloud (for example, local development), set GOOGLE_APPLICATION_CREDENTIALS to a service-account key file:
Use least-privilege IAM roles. For publishing and subscribing, grant the roles/pubsub.publisher or roles/pubsub.subscriber role respectively to the service account.

Publish vs. subscribe patterns

  • Publishing: Clients publish messages to a topic. Libraries support synchronous and asynchronous (future/promise-based) publish APIs and provide batching options for higher throughput.
  • Pull subscribers: Clients pull messages from a subscription. Modern libraries support streaming pull with callback handlers and automatic lease (ack deadline) management.
  • Push subscribers: Pub/Sub sends HTTP POST requests to a configured HTTPS endpoint when new messages arrive. Push endpoints must acknowledge the request (e.g., return HTTP 200) to avoid redelivery.

Client library packages (common languages)

Installation examples:

Minimal code examples

Python — publish (synchronous)
Python — subscribe (streaming pull with callback)
Node.js — publish (async)

Operational considerations

  • Batching and latency: Publishers batch messages to improve throughput. Tune batch size and delay to balance latency and throughput.
  • Flow control: Configure max outstanding messages/bytes on subscribers to avoid resource exhaustion and to maintain predictable memory/CPU usage.
  • Retries and dead-letter topics: Configure retry policies and consider a dead-letter topic for messages that fail processing repeatedly so they can be inspected and reprocessed.
  • Push endpoints: Ensure push endpoints use HTTPS, validate requests as needed (e.g., authentication or token validation), and respond with the correct status codes to control acknowledgment behavior.
  • Monitoring and alerting: Track metrics such as publish/ack latency, undelivered message backlog, and subscriber errors to detect and respond to issues quickly.

Security warning

Never commit service account keys to source control. Use secret management, environment-level credentials (ADC), or workload identity to securely provision credentials.

Quick reference: patterns and choices

Summary
  • Publishers and subscribers connect to Cloud Pub/Sub via Google Cloud client libraries.
  • Libraries abstract network, auth, retries, batching, and flow control so you can focus on message handling.
  • Choose publish/pull/push based on latency, throughput, and runtime environment.
  • Use Application Default Credentials or secure credential provisioning and follow least-privilege IAM.
That’s it for this lesson—see you in the next one.

Watch Video