Overview

Google Cloud Pub/Sub is a global, serverless messaging service provided by Google Cloud. It enables the creation of event-driven architectures by allowing independent applications to communicate asynchronously through a publish-subscribe model. Publishers send messages to topics, and subscribers receive messages from those topics, without direct knowledge of each other. This decoupling facilitates the development of scalable and resilient systems, particularly in microservices environments and data streaming pipelines.

Pub/Sub is designed to handle message ingestion and delivery at scale, supporting millions of messages per second globally. It offers features such as automatic scaling, low-latency message delivery, and configurable message retention. The service guarantees at-least-once message delivery and provides options for ordered message delivery within a topic partition. It integrates with other Google Cloud services, including Dataflow for stream processing, Cloud Functions for event-driven compute, and BigQuery for data warehousing, allowing developers to build comprehensive data processing solutions.

There are two primary Pub/Sub offerings: standard Pub/Sub and Pub/Sub Lite. Standard Pub/Sub provides a fully managed, global service with automatic scaling and high availability, suitable for most enterprise workloads requiring high throughput and low latency without operational overhead. Pub/Sub Lite offers a more cost-effective option for scenarios with specific regional and zonal availability requirements, where users can manage partition capacity more directly, as detailed in the Google Cloud Pub/Sub documentation on choosing between offerings. Use cases range from real-time analytics and IoT data ingestion to event-driven updates for distributed databases, as might be seen in patterns discussed on Martin Fowler's articles on microservices architecture.

Key features

  • Publish-Subscribe Model: Decouples message producers (publishers) from message consumers (subscribers) through topics.
  • Global Infrastructure: Leverages Google's global network for low-latency message delivery across regions.
  • Automatic Scaling: Scales automatically to handle fluctuating message volumes without manual intervention.
  • At-Least-Once Delivery: Guarantees that each message is delivered to a subscriber at least once.
  • Message Ordering: Supports ordered message delivery within a topic partition.
  • Message Retention: Configurable message retention from 7 days up to 31 days, and up to 7 days for Pub/Sub Lite.
  • Dead-Letter Queues (DLQ): Routes undeliverable messages to a designated topic for reprocessing or analysis.
  • Push and Pull Delivery: Offers both push delivery (Pub/Sub pushes messages to endpoints) and pull delivery (subscribers pull messages when ready).
  • Client Libraries: Provides client libraries for multiple programming languages including Java, Python, Node.js, Go, C#, Ruby, PHP, and C++.
  • Security and Compliance: Integrates with Google Cloud IAM for access control and meets various compliance standards including ISO 27001, SOC 2 Type II, and GDPR.

Pricing

Google Cloud Pub/Sub pricing is primarily based on the volume of data processed, measured in gigabytes (GB). There are separate pricing structures for standard Pub/Sub and Pub/Sub Lite, with additional charges for features such as message retention and snapshot storage. The free tier includes 10 GB of messages per month.

Pricing as of 2026-05-06:

Service Component Price (per GB) Notes
Standard Pub/Sub Message Throughput Starting at $0.06 First 10 GB/month free; pricing varies by volume tiers
Pub/Sub Lite Message Throughput Starting at $0.006 Lower cost option, pricing varies by volume tiers
Pub/Sub Lite Storage (per GB-month) Starting at $0.015 For message retention in Lite topics
Message Retention (Standard Pub/Sub, per GB-month) $0.27 For messages retained beyond 7 days, up to 31 days

For detailed and up-to-date pricing information, refer to the Google Cloud Pub/Sub pricing page.

Common integrations

Alternatives

  • Amazon SQS: A fully managed message queuing service by AWS, offering standard and FIFO queues for decoupling and scaling microservices, batch processing, and serverless applications. Amazon SQS homepage.
  • Azure Service Bus: Microsoft's fully managed enterprise messaging service, providing reliable and secure message delivery for hybrid cloud solutions and microservices architectures, with queues and topics. Azure Service Bus product page.
  • Apache Kafka: An open-source distributed streaming platform capable of handling high-throughput, fault-tolerant real-time data feeds, often deployed on-premises or managed services like Confluent Cloud. Apache Kafka project homepage.
  • RabbitMQ: An open-source message broker that implements the Advanced Message Queuing Protocol (AMQP), offering flexible routing options and supporting various messaging patterns.
  • Redis Streams: A data structure within Redis that provides an append-only log, enabling producers to add items and consumers to read from the stream, suitable for real-time event logging and processing.

Getting started

This Python example demonstrates how to publish a message to a Google Cloud Pub/Sub topic and subscribe to receive messages from a subscription. Before running, ensure you have authenticated your Google Cloud environment and replaced YOUR_PROJECT_ID, YOUR_TOPIC_ID, and YOUR_SUBSCRIPTION_ID with your actual project, topic, and subscription identifiers.


from google.cloud import pubsub_v1
import time

# --- Publisher Code ---
def publish_message(project_id, topic_id, message_data):
    publisher = pubsub_v1.PublisherClient()
    topic_path = publisher.topic_path(project_id, topic_id)
    
    data = message_data.encode("utf-8")
    future = publisher.publish(topic_path, data)
    print(f"Published message ID: {future.result()}")

# --- Subscriber Code ---
def subscribe_messages(project_id, subscription_id):
    subscriber = pubsub_v1.SubscriberClient()
    subscription_path = subscriber.subscription_path(project_id, subscription_id)

    def callback(message):
        print(f"Received message: {message.data.decode('utf-8')}")
        message.ack()

    streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
    print(f"Listening for messages on {subscription_path}. Press Ctrl+C to exit.")

    try:
        streaming_pull_future.result()  # Blocks until an exception is raised or the pull future is cancelled.
    except TimeoutError:
        streaming_pull_future.cancel()
        streaming_pull_future.result()

# Configuration
PROJECT_ID = "YOUR_PROJECT_ID"  # Replace with your Google Cloud Project ID
TOPIC_ID = "YOUR_TOPIC_ID"      # Replace with your Pub/Sub Topic ID
SUBSCRIPTION_ID = "YOUR_SUBSCRIPTION_ID" # Replace with your Pub/Sub Subscription ID
MESSAGE_TO_SEND = "Hello from cloudpicker!"

if __name__ == "__main__":
    # Publish a message
    print("\n--- Publishing Message ---")
    publish_message(PROJECT_ID, TOPIC_ID, MESSAGE_TO_SEND)
    time.sleep(2) # Give a moment for message to propagate

    # Subscribe to messages
    print("\n--- Subscribing to Messages ---")
    # This will run indefinitely, waiting for messages.
    # You might want to run the publisher and subscriber in separate processes or terminals.
    subscribe_messages(PROJECT_ID, SUBSCRIPTION_ID)

To set up your environment:

  1. Install the Google Cloud Pub/Sub client library: pip install google-cloud-pubsub
  2. Ensure your Google Cloud project is configured with the necessary APIs enabled and your credentials are set up. Refer to the Google Cloud SDK Quickstarts for authentication details.
  3. Create a Pub/Sub topic and a subscription to that topic in your Google Cloud project.