Google Cloud Platform (GCP)
Core Compute Services
Storage & Databases
- Google Cloud Storage
- Persistent Disks
- Cloud Filestore
- Cloud SQL
- Cloud Spanner
- Cloud Bigtable
- Cloud Firestore
Data Analytics & AI
Google Cloud Platform
๐งญ Google Cloud Pub/Sub: Event-Driven Messaging for Modern Applications
In todayโs digital world, real-time communication between systems is essential. From IoT sensors streaming data every second to microservices interacting seamlessly โ modern systems rely on event-driven architecture.
Thatโs where Google Cloud Pub/Sub comes in.
Pub/Sub (Publish/Subscribe) is a fully managed messaging system that allows applications to send and receive messages asynchronously. It enables real-time data streaming, event-driven workflows, and loose coupling between components.
In this article, weโll explore:
- What Pub/Sub is and how it works
- Architecture and workflow with a ****
- 3 real-world examples (Python, streaming, analytics)
- Tips for remembering concepts (for interview/exams)
- Why itโs important for data engineers and developers
Letโs begin!
โ๏ธ What Is Google Cloud Pub/Sub?
Pub/Sub stands for Publish/Subscribe โ a messaging pattern where:
- Publishers send messages to a topic.
- Subscribers receive those messages asynchronously through subscriptions.
It acts like a message bus that connects producers and consumers without requiring them to know each other.
๐งฉ Pub/Sub = โProducer publishes โ Broker (Topic) โ Consumer subscribesโ
๐ Key Features
Feature | Description |
---|---|
Serverless Messaging | Fully managed, no servers to maintain |
Scalable | Handles millions of messages per second |
Asynchronous Communication | Decouples producers and consumers |
At-Least-Once Delivery | Ensures every message is processed |
Low Latency | Near real-time message delivery |
Integration | Works with Dataflow, BigQuery, Cloud Functions |
Security | IAM-based access control and encryption |
๐งฉ Pub/Sub Architecture
To understand Pub/Sub, letโs visualize how publishers, topics, and subscribers work together.
๐งญ (Architecture Representation)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ Publisher โ โ (sends messages to topic) โ โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ โ โผ โโโโโโโโโโโโโโโโโโโโ โ Topic โ โ (Message Broker) โ โโโโโโโโโโโโโโโโโโโโ โ โโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโ โผ โผ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โ Subscription A โ โ Subscription B โ โ (Pull Subscriber)โ โ (Push Subscriber)โ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โ โ โผ โผ โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โ Consumer App โ โ Cloud Function โ โโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
๐ก How Pub/Sub Works
- Publisher creates a topic and publishes messages.
- Subscribers attach to the topic through subscriptions.
- When a message is published, Pub/Sub stores it temporarily.
- Subscribers pull messages (or receive via push) asynchronously.
- Once processed, the subscriber acknowledges the message.
- Unacknowledged messages are retried until confirmed.
This architecture ensures reliability, scalability, and decoupling.
โ๏ธ 3 Core Components
1๏ธโฃ Topic
A named resource where publishers send messages.
Example: projects/myproject/topics/user-events
2๏ธโฃ Subscription
A connection to a topic that delivers messages to subscribers.
Example: projects/myproject/subscriptions/user-activity-sub
3๏ธโฃ Message
Data payload sent by the publisher (e.g., JSON, text, binary).
๐งฎ Example Set 1: Basic Pub/Sub (Python SDK)
Letโs start with Python examples demonstrating how Pub/Sub works.
๐งฉ Example 1: Create Topic and Publish a Message
from google.cloud import pubsub_v1
project_id = "my-gcp-project"topic_id = "user-notifications"
publisher = pubsub_v1.PublisherClient()topic_path = publisher.topic_path(project_id, topic_id)
# Create a topic (if not exists)topic = publisher.create_topic(request={"name": topic_path})print(f"Topic created: {topic.name}")
# Publish a messagedata = "New user registered!"future = publisher.publish(topic_path, data.encode("utf-8"))print(f"Message published: {future.result()}")
๐ง Concept: A publisher creates a topic and sends messages to it. Pub/Sub handles the delivery.
๐งฉ Example 2: Create a Subscriber (Pull Mode)
from google.cloud import pubsub_v1
project_id = "my-gcp-project"subscription_id = "user-notifications-sub"
subscriber = pubsub_v1.SubscriberClient()subscription_path = subscriber.subscription_path(project_id, subscription_id)
def callback(message): print(f"Received: {message.data.decode('utf-8')}") message.ack()
subscriber.subscribe(subscription_path, callback=callback)
print("Listening for messages on:", subscription_path)import timewhile True: time.sleep(60)
๐ก Explanation: The subscriber pulls messages from the subscription and acknowledges them after processing.
๐งฉ Example 3: Push Subscription (Webhook Receiver)
# Flask app to receive messagesfrom flask import Flask, request
app = Flask(__name__)
@app.route("/pubsub/push", methods=["POST"])def pubsub_push(): envelope = request.get_json() message = envelope["message"] print(f"Received message: {message['data']}") return "OK", 200
if __name__ == "__main__": app.run(port=8080)
๐ Explanation: In push mode, Pub/Sub delivers messages to a URL endpoint (e.g., Cloud Function or API).
๐ Example Set 2: Event-Driven Architecture
These examples show how Pub/Sub powers real-world event-driven apps.
๐งฉ Example 1: IoT Sensor Data Streaming
Publisher (IoT Device)
import json, randomfrom google.cloud import pubsub_v1
publisher = pubsub_v1.PublisherClient()topic_path = publisher.topic_path("my-project", "iot-sensor-data")
for i in range(5): message = json.dumps({"device_id": "sensor_1", "temperature": random.uniform(25, 35)}) publisher.publish(topic_path, message.encode("utf-8")) print("Published:", message)
Subscriber (Data Processor)
def callback(message): data = json.loads(message.data.decode("utf-8")) print(f"Device: {data['device_id']} Temp: {data['temperature']}") message.ack()
๐ง Use Case: IoT sensors continuously stream temperature data to Pub/Sub for processing or alerting.
๐งฉ Example 2: E-Commerce Order Event
When a customer places an order, a Pub/Sub message triggers multiple downstream services:
- Email notification
- Inventory update
- Analytics tracking
order_event = { "order_id": "A12345", "user_id": "U5678", "status": "Placed", "timestamp": "2025-10-22T10:00:00Z"}
publisher.publish(topic_path, json.dumps(order_event).encode("utf-8"))
Each microservice subscribes to the order-events
topic independently.
๐งฉ Example 3: Real-Time Analytics Pipeline
Pub/Sub โ Dataflow โ BigQuery
# Dataflow reads messages from Pub/Sub, transforms them, and writes to BigQuerypython -m apache_beam.examples.streaming_wordcount \ --input_topic=projects/my-project/topics/stream-data \ --output_table=my_dataset.analytics_results
๐ง Concept: Pub/Sub provides the input stream, while Dataflow performs real-time transformations.
๐งฎ Example Set 3: Advanced Integrations
๐งฉ Example 1: Pub/Sub with Cloud Functions
You can trigger a Cloud Function automatically when a new message arrives.
def process_message(event, context): import base64 message = base64.b64decode(event['data']).decode('utf-8') print(f"Processed event: {message}")
๐งฉ Deployment Command:
gcloud functions deploy process_message \--trigger-topic=my-topic --runtime=python39
Use Case: Automatically process new user registrations, logins, or payments.
๐งฉ Example 2: Pub/Sub + BigQuery + Dataflow
Architecture: Pub/Sub (Ingest) โ Dataflow (Transform) โ BigQuery (Store)
- Pub/Sub collects streaming data
- Dataflow enriches or aggregates it
- BigQuery provides analytics
This trio forms a real-time analytics pipeline used in finance, IoT, and e-commerce.
๐งฉ Example 3: Pub/Sub Dead Letter Topic (Error Handling)
from google.cloud import pubsub_v1
publisher = pubsub_v1.PublisherClient()topic_path = publisher.topic_path("my-project", "main-topic")dl_topic_path = publisher.topic_path("my-project", "dead-letter-topic")
# Create a subscription with dead-letter policysubscription = pubsub_v1.types.Subscription( name="projects/my-project/subscriptions/main-sub", topic=topic_path, dead_letter_policy=pubsub_v1.types.DeadLetterPolicy( dead_letter_topic=dl_topic_path, max_delivery_attempts=5 ))
publisher.create_topic(request={"name": dl_topic_path})subscriber = pubsub_v1.SubscriberClient()subscriber.create_subscription(request={"subscription": subscription})
Purpose: If a message fails processing after multiple attempts, itโs redirected to the dead-letter topic for debugging.
๐ง How to Remember Pub/Sub Concepts (Interview & Exam Tips)
๐ฏ Mnemonic: โTMSโ โ Topic, Message, Subscriber
Concept | Meaning | Easy Trick |
---|---|---|
T โ Topic | Where messages are sent | โTopic = Targetโ |
M โ Message | Data payload | โThe message movesโ |
S โ Subscriber | Who receives messages | โSubscriber = Sinkโ |
๐ Flashcard Q&A
Question | Short Answer |
---|---|
What is Pub/Sub? | Event-driven messaging system |
Whatโs the difference between push and pull? | Push sends messages to endpoints; Pull fetches manually |
What guarantees does Pub/Sub provide? | At-least-once delivery |
Can it integrate with BigQuery or Dataflow? | Yes |
Whatโs a dead-letter topic? | Stores failed messages for retry/debugging |
๐ Why Itโs Important to Learn Pub/Sub
Reason | Explanation |
---|---|
Event-Driven Systems | Enables decoupled, scalable apps |
Real-Time Processing | Powers IoT, analytics, and alerts |
Scalability | Handles millions of messages per second |
Serverless | No cluster management needed |
Integration | Works seamlessly with Dataflow, BigQuery, Cloud Functions |
Reliability | Guaranteed delivery and retries |
Career Relevance | Used in modern microservice and streaming architectures |
๐งฉ Common Mistakes & Best Practices
Mistake | Issue | Best Practice |
---|---|---|
Not acknowledging messages | Causes re-delivery | Always ack() messages |
Using wrong subscription type | Missed events | Choose push/pull wisely |
Ignoring retries | Message loss | Use Dead Letter Topics |
Overloading subscribers | Processing delays | Scale horizontally |
Hardcoding credentials | Security risk | Use IAM roles and service accounts |
๐ Real-World Use Cases
Use Case | Description |
---|---|
IoT Telemetry | Real-time device updates and alerts |
E-Commerce Events | Order and payment tracking |
Financial Systems | Fraud detection in real time |
Gaming Apps | Leaderboards, achievements, notifications |
Microservices | Communication between independent services |
๐งญ Summary
Google Cloud Pub/Sub is a powerful event-driven messaging system that connects producers and consumers asynchronously.
It enables: โ Real-time event processing โ Decoupled architecture โ Seamless integration with Dataflow, BigQuery, and Cloud Functions โ Reliable and scalable message delivery
Pub/Sub is the backbone of event-driven systems on Google Cloud โ essential for IoT, analytics, and microservice-based applications.
๐ง Final Thoughts
Learning Pub/Sub gives you an edge as a data engineer, cloud architect, or backend developer. It teaches you how modern event-driven systems work โ a crucial skill in the era of real-time data.
โIf BigQuery is the brain of analytics, then Pub/Sub is the heartbeat of real-time data flow.โ