Google Cloud Pub/Sub: A Complete Guide to Real-Time Messaging

Arindam Das
4 min readOct 27, 2024

--

Google Cloud Pub/Sub is a managed messaging service designed for event-driven applications and asynchronous message transmission. It supports real-time message delivery, helping systems decouple microservices and manage high-throughput data pipelines. Google Cloud Pub/Sub’s architecture supports horizontal scaling and global distribution, making it suitable for enterprises with complex workloads.

Image from Google Cloud

Key Features:

  • Global Scalability: Pub/Sub is designed to handle billions of messages per second across the globe.
  • Reliability: Ensures at-least-once message delivery, with options to avoid duplicates.
  • Low Latency: Supports quick message transmission, which is critical for data streams and real-time analytics.
  • Flexible Delivery Options: Allows both push and pull message delivery, making it adaptable to various use cases.

Core Concepts in Google Cloud Pub/Sub

  1. Topics: A logical channel to which publishers send messages.
  2. Messages: Data sent by the publisher to a topic.
  3. Subscriptions: Define how messages are delivered to subscribers. Subscriptions can either be pull-based (subscriber fetches) or push-based (Pub/Sub pushes to an HTTP endpoint, like a Google Cloud Function).
  4. Message Acknowledgement: Subscribers confirm message receipt, ensuring that Pub/Sub doesn’t re-deliver the message.
  5. Dead Letter Topic (DLT): A special topic where undeliverable messages can be redirected for reprocessing or debugging.

Use Cases for Google Cloud Pub/Sub

  1. Real-Time Analytics: Event processing for real-time applications like financial services and IoT.
  2. Data Ingestion Pipelines: Pub/Sub can gather data from multiple sources and push it to data warehouses like BigQuery.
  3. Microservices Communication: Allows asynchronous communication between microservices without direct dependency.
  4. Log Aggregation: Centralizes logs from various systems for analysis and monitoring.

Step-by-Step Guide: Setting Up Google Cloud Pub/Sub

Step 1: Setting Up a Google Cloud Project

  1. Go to the Google Cloud Console.
  2. Create a new project or select an existing one.
  3. Enable the Pub/Sub API in the APIs & Services section.

Step 2: Create a Topic

A topic is where publishers send messages. Follow these steps:

  1. In the Pub/Sub section of the console, click Create Topic.
  2. Enter a name for the topic, for example, my-topic.
  3. Click Create to save the topic.

Step 3: Create a Subscription

A subscription ties a topic to an endpoint where messages are received. You can create subscriptions to pull messages or push them to an HTTP endpoint like a Google Cloud Function.

  1. Go to the Pub/Sub section, select your my-topic, and click Create Subscription.
  2. Name the subscription, for example, my-subscription.
  3. Choose Push as the delivery type.
  4. For Push Endpoint, use the URL of your Google Cloud Function endpoint.
  5. Click Create to finish setting up the subscription.

Step 4: Publish Messages

Publishers send messages to the topic, which are then routed to subscriptions. Messages can be sent using client libraries or directly from the console.

Python Example: Publishing Messages

from google.cloud import pubsub_v1

# Initialize Publisher
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path('YOUR_PROJECT_ID', 'my-topic')

# Publish a message
message = "Hello, World!"
future = publisher.publish(topic_path, message.encode('utf-8'))
print(f'Message ID: {future.result()}')

Example: Subscribing to Messages Using Google Cloud Function

Google Cloud Functions can directly subscribe to a Pub/Sub topic as a push endpoint. This setup allows the function to execute automatically whenever a new message is published.

Step 1: Create a Google Cloud Function

  1. In the Cloud Functions section of the Google Cloud Console, click Create Function.
  2. Name your function, for example, my-function.
  3. Choose HTTP Trigger as the trigger type, and add the trigger topic, my-topic.
  4. Select the Runtime (e.g., Python 3.9).

Step 2: Add the Cloud Function Code

In this example, we’ll create a function that logs the message data to Google Cloud Logging.

Python Code for Cloud Function:

import base64

def pubsub_listener(event, context):
# Decode the Pub/Sub message data
if 'data' in event:
message_data = base64.b64decode(event['data']).decode('utf-8')
print(f'Received message: {message_data}')
else:
print('No message data found')

Configuration:

  • The event parameter contains the Pub/Sub message. The data is base64-encoded, so we decode it to get the original message content.
  • The function automatically acknowledges the message upon successful execution.

5. Deploy the function. Google Cloud Functions will automatically generate a unique URL, which serves as the push endpoint for Pub/Sub.

Step 3: Test the Setup

  1. Go back to your Pub/Sub topic in the Google Cloud Console.
  2. Use the Publish Message button to send a test message to my-topic.
  3. Check Cloud Logging or the Cloud Functions log section to see if the message was received and logged by the function.

Advanced Pub/Sub Configurations

Dead Letter Topics

If a subscriber consistently fails to acknowledge messages, they can be redirected to a Dead Letter Topic (DLT) after a specific retry limit is reached.

  1. In the Pub/Sub Console, go to Subscriptions.
  2. Select the subscription and configure a Dead Letter Policy.
  3. Specify a new topic where undeliverable messages will be sent.

Message Ordering

If message order matters, configure Message Ordering by enabling it on both the publisher and subscriber sides.

Monitoring and Managing Google Cloud Pub/Sub

Use Google Cloud Monitoring and Cloud Logging to observe system performance and troubleshoot issues. Key metrics include:

  • Ack Deadlines: Fine-tune this for slower subscribers to avoid unnecessary retries.
  • Message Lag: Monitor if messages are being delayed, which could indicate high processing times.

Best Practices for Google Cloud Pub/Sub

  1. Use Dead Letter Queues: Ensure critical messages have a secondary path if the main subscription fails.
  2. Enable Message Ordering Only If Needed: It can impact performance, so use it sparingly.
  3. Adjust Acknowledgment Deadline: Tune the acknowledgement settings to avoid unnecessary re-deliveries.
  4. Plan for Idempotency: Build message processing logic that can handle duplicate messages.

Conclusion

Google Cloud Pub/Sub provides a powerful and flexible messaging platform for connecting microservices and managing high-volume event streams. Its support for Cloud Functions as a push endpoint allows easy integration with serverless applications, helping you build robust event-driven systems. By following best practices and leveraging advanced configurations, you can optimize Pub/Sub for reliable, real-time messaging across distributed systems.

--

--

Arindam Das
Arindam Das

No responses yet