Skip to content

You are viewing a free preview of this lesson.

Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.

Pub/Sub Overview

Pub/Sub Overview

Google Cloud Pub/Sub is a fully managed, real-time messaging service that allows you to send and receive messages between independent applications. It decouples services that produce events from services that process events, enabling reliable, scalable, and asynchronous communication across your architecture.


What Is Pub/Sub?

Pub/Sub stands for publish-subscribe. It is a messaging pattern where message producers (publishers) send messages to a named resource called a topic, and message consumers (subscribers) receive messages from subscriptions attached to that topic. The publisher does not know which subscribers exist, and subscribers do not know which publisher sent the message.

Core Characteristics

Characteristic Description
Fully managed No servers to provision, patch, or scale — Google handles everything
Global A single topic can span multiple regions with automatic replication
At-least-once delivery Every message is delivered to every subscription at least once
Real-time Messages are typically delivered within milliseconds
Serverless Pay only for what you use — no idle costs beyond storage
Durable Messages are persisted to multiple zones for durability

Why Use Pub/Sub?

In modern cloud-native architectures, services must communicate without tight coupling. Synchronous REST calls create dependencies that reduce resilience and scalability. Pub/Sub solves this by introducing an intermediary message broker.

Key Benefits

Decoupling — Publishers and subscribers operate independently. You can add new subscribers without modifying the publisher. If a subscriber goes offline, messages accumulate and are delivered when it comes back.

Scalability — Pub/Sub automatically scales to handle millions of messages per second. You do not need to provision capacity in advance or manage throughput units.

Reliability — Messages are stored redundantly across multiple availability zones. Pub/Sub guarantees at-least-once delivery, and with ordering keys, it can guarantee ordered delivery within a key.

Fan-out — A single published message can be delivered to many subscribers simultaneously. This is ideal for event-driven architectures where multiple services need to react to the same event.

Load levelling — When traffic spikes, Pub/Sub absorbs the burst. Subscribers process messages at their own pace, preventing downstream services from being overwhelmed.


Core Concepts

Topics

A topic is a named resource to which publishers send messages. Think of it as a broadcast channel. A topic can have zero or more subscriptions. When a message is published to a topic, Pub/Sub automatically delivers a copy of that message to every subscription.

Subscriptions

A subscription is a named resource that represents the stream of messages from a single topic to a specific subscriber. Each subscription receives a copy of every message published to its topic. A subscription can be pull-based (the subscriber polls for messages) or push-based (Pub/Sub sends messages to an endpoint).

Messages

A message is the unit of data sent through Pub/Sub. It consists of:

Component Description
Data The message body — up to 10 MB of binary or text data
Attributes Key-value pairs of metadata (up to 100 attributes)
Message ID A unique identifier assigned by Pub/Sub
Publish time Timestamp when the message was accepted by Pub/Sub
Ordering key Optional key for ordered delivery within a group

Acknowledgement

When a subscriber receives a message, it must acknowledge (ack) it to tell Pub/Sub that processing was successful. Until a message is acknowledged, Pub/Sub considers it undelivered and will redeliver it after the acknowledgement deadline expires.


How Message Flow Works

  1. A publisher sends a message to a topic using the Pub/Sub API
  2. Pub/Sub durably stores the message across multiple zones
  3. For each subscription on the topic, Pub/Sub delivers a copy of the message
  4. The subscriber receives the message (via pull or push)
  5. The subscriber processes the message and sends an acknowledgement
  6. Pub/Sub removes the acknowledged message from the subscription backlog

If the subscriber does not acknowledge within the deadline, Pub/Sub redelivers the message. This ensures no message is lost, even if a subscriber crashes mid-processing.


Getting Started

Creating a Topic

# Create a topic
gcloud pubsub topics create my-topic

# List all topics in the project
gcloud pubsub topics list

Creating a Subscription

# Create a pull subscription
gcloud pubsub subscriptions create my-sub --topic=my-topic

# Create a push subscription
gcloud pubsub subscriptions create my-push-sub \
  --topic=my-topic \
  --push-endpoint=https://example.com/push-handler

Publishing a Message

# Publish a simple text message
gcloud pubsub topics publish my-topic --message="Hello, Pub/Sub!"

# Publish with attributes
gcloud pubsub topics publish my-topic \
  --message="Order created" \
  --attribute="orderId=12345,priority=high"

Pulling Messages

# Pull messages (auto-acknowledge)
gcloud pubsub subscriptions pull my-sub --auto-ack --limit=10

Pub/Sub in the GCP Ecosystem

Pub/Sub integrates deeply with other Google Cloud services:

Integration Description
Cloud Functions Trigger a function on every message
Cloud Run Push messages to a containerised HTTP service
Dataflow Stream messages into Apache Beam pipelines
BigQuery Write messages directly to BigQuery tables via subscriptions
Cloud Storage Export messages to GCS buckets for archival
Cloud Logging Route log entries through Pub/Sub
Eventarc Unified eventing that uses Pub/Sub as the transport layer

Pricing Model

Pub/Sub pricing is based on the volume of data published and delivered:

Component Cost Basis
Message ingestion Per TiB of data published
Message delivery Per TiB of data delivered to subscriptions
Seek and snapshot Per TiB of retained acknowledged message backlog
Storage Per GiB-month of retained messages beyond 7 days

The first 10 GiB per month is free, making Pub/Sub cost-effective for development and small workloads.


Summary

Google Cloud Pub/Sub is a fully managed, globally distributed messaging service that decouples publishers from subscribers. It provides at-least-once delivery, automatic scaling, and seamless integration with the broader GCP ecosystem. Understanding the core concepts — topics, subscriptions, messages, and acknowledgements — is the foundation for building reliable event-driven architectures on Google Cloud. In the following lessons, we will dive deeper into topics, subscriptions, delivery modes, and advanced features.