Type Your Question


How to use Google Cloud Pub/Sub for message queuing?

 Tuesday, 18 March 2025
GOOGLE

In the world of modern application development, asynchronous communication is crucial for building scalable and resilient systems. Google Cloud Pub/Sub provides a powerful and reliable message queuing service that allows different components of your application to communicate without being directly coupled. This decoupling improves performance, fault tolerance, and overall architecture.

What is Google Cloud Pub/Sub?

Google Cloud Pub/Sub (Publisher/Subscriber) is a fully managed, real-time messaging service that enables you to send and receive messages between independent applications and services. It follows a publish-subscribe pattern, where publishers send messages to a topic, and subscribers listen to that topic to receive messages.

Key features of Google Cloud Pub/Sub:

  • Scalability: Automatically scales to handle massive volumes of messages.
  • Reliability: Ensures messages are delivered at least once (at-least-once delivery) or exactly once (exactly-once delivery, with certain limitations).
  • Durability: Stores messages redundantly to prevent data loss.
  • Flexibility: Supports various programming languages and integrations with other Google Cloud services.
  • Global Reach: Global presence for low-latency communication worldwide.

Why Use Pub/Sub for Message Queuing?

Using Pub/Sub for message queuing offers several significant advantages:

  • Decoupling: Independent services can communicate without needing to know about each other's implementation details.
  • Scalability and Performance: Distribute workloads and process messages concurrently. The managed service scales to handle demand spikes.
  • Resilience: Failure of one component doesn't affect other components as messages are buffered in the Pub/Sub system.
  • Event-Driven Architecture: Build systems where components react to events published through Pub/Sub.
  • Integration: Easily integrates with other Google Cloud services like Dataflow, Cloud Functions, and App Engine.

Getting Started with Pub/Sub: A Step-by-Step Guide

Follow these steps to set up and use Pub/Sub for message queuing:

1. Project Setup and Authentication

  1. Create a Google Cloud Project: If you don't have one already, create a new Google Cloud project in the Google Cloud Console.
  2. Enable the Pub/Sub API: Go to the API Library in the Google Cloud Console, search for "Cloud Pub/Sub API," and enable it.
  3. Set Up Authentication: The recommended method for authentication is using a service account.
    • Create a Service Account: In the Google Cloud Console, navigate to "IAM & Admin" -> "Service Accounts." Create a new service account.
    • Grant Permissions: Give the service account the "Pub/Sub Publisher" and "Pub/Sub Subscriber" roles (or the more granular "Pub/Sub Editor" role for full access) depending on what the service will be doing.
    • Download a JSON Key: Download the JSON key file for the service account. This file contains the credentials your application will use to authenticate.

  4. Set the GOOGLE_APPLICATION_CREDENTIALS Environment Variable: Set this environment variable to the path of the downloaded JSON key file. This allows your application to automatically authenticate with Google Cloud.
    export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-key.json"

2. Creating Topics and Subscriptions

Topics are named entities to which publishers send messages. Subscriptions define how subscribers receive messages from a topic.

Using the Google Cloud Console

  1. Navigate to Pub/Sub: In the Google Cloud Console, find the "Pub/Sub" section.
  2. Create a Topic: Click "Create Topic," enter a name for the topic (e.g., my-topic), and click "Create." Topic names must be unique within your Google Cloud Project. You can enable message retention on the topic itself.
  3. Create a Subscription: Select the newly created topic and click "Create Subscription." Enter a name for the subscription (e.g., my-subscription). Choose a delivery type:
    • Pull: Subscribers explicitly request messages from Pub/Sub.
    • Push: Pub/Sub pushes messages to a pre-configured HTTP endpoint.

    Configure other settings, such as acknowledgement deadline (the amount of time a subscriber has to acknowledge a message before Pub/Sub retries delivery) and dead-letter topic (for handling messages that repeatedly fail to be processed). Finally click "Create".

Using the gcloud CLI

 # Create a topic
gcloud pubsub topics create my-topic

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

# Create a push subscription (replace YOUR_ENDPOINT with your actual endpoint URL)
gcloud pubsub subscriptions create my-push-subscription --topic=my-topic --push-endpoint=YOUR_ENDPOINT

3. Publishing Messages

Publishers send messages to topics.

Example using Python:


from google.cloud import pubsub_v1

# Project ID and topic name
project_id = "your-project-id" # Replace with your actual project ID
topic_name = "my-topic"

publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_name)

# Data you want to publish
data = "Hello, Pub/Sub!".encode("utf-8")

# When you publish a message, the client returns a future.
future = publisher.publish(topic_path, data=data)
print(f"Published message ID: {future.result()}")

4. Subscribing to Messages

Subscribers receive messages from subscriptions. The mechanism differs slightly based on the chosen delivery type.

Pull Subscriptions (Python)


from google.cloud import pubsub_v1

project_id = "your-project-id" # Replace with your actual project ID
subscription_name = "my-subscription"
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_name)

def callback(message: pubsub_v1.types.ReceivedMessage) -> None:
print(f"Received message: {message.data.decode()}")
message.ack() # Acknowledge the message to remove it from the queue

streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print(f"Listening for messages on {subscription_path}...\n")

try:
# Calling result() will block indefinitely unless the timeout is set.
streaming_pull_future.result()
except TimeoutError:
streaming_pull_future.cancel() # Trigger the shutdown.
streaming_pull_future.result()

Push Subscriptions

For push subscriptions, Pub/Sub will send POST requests to the endpoint URL configured during subscription creation. The endpoint needs to handle these requests and acknowledge the messages within the configured ack deadline. The request body is typically a JSON payload containing message attributes and the message data itself encoded as a base64 string.

An example of Flask to receive the POST request would be like:


from flask import Flask, request, jsonify
import base64

app = Flask(__name__)

@app.route('/', methods=['POST'])
def index():
envelope = request.get_json()
if not envelope:
msg = 'no Pub/Sub message received'
print(f'error: {msg}')
return f'Bad Request: {msg}', 400

if not isinstance(envelope, dict) or 'message' not in envelope:
msg = 'invalid Pub/Sub message format'
print(f'error: {msg}')
return f'Bad Request: {msg}', 400

pubsub_message = envelope['message']

if isinstance(pubsub_message, dict) and 'data' in pubsub_message:
message_data = base64.b64decode(pubsub_message['data']).decode('utf-8').strip()

print(f'Received message: {message_data}')

#Process message_data here
else:
print(envelope)

return jsonify(), 204
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get("PORT", 8080)))

5. Important Considerations: Acknowledgment and Dead-Letter Queues

When using Pull Subscriptions, its your responsibility to "Ack" messages, telling Google Cloud Pub/Sub it can now safely remove from its persistence stores.
When not, those message(s) are returned again when available. Using a high Acknowledgement Deadling increases your likelihood of successfully completing your processing job.

When errors/exceptions occur it should automatically be retried. Setting a dead letter queue with pubsub would be extremely useful. It enables retrying delivery on those fault messaged with out spamming with processing services repeatedly. You will then need a "clean up" service on the dead-letter queue in place.

Best Practices for Using Pub/Sub

  • Choose the Right Delivery Type: Pull subscriptions are suitable when subscribers can control their processing rate. Push subscriptions are suitable for serverless functions and applications with HTTP endpoints.
  • Batch Publishing and Subscribing: Improve throughput by publishing and subscribing to messages in batches.
  • Use Message Attributes: Include metadata in message attributes to enable filtering and routing.
  • Handle Errors Gracefully: Implement retry mechanisms and use dead-letter topics to handle failed messages.
  • Monitor Performance: Use Google Cloud Monitoring to track key metrics like message throughput and latency.
  • Secure Your Applications: Use proper authentication and authorization to control access to your Pub/Sub resources.
  • Ordering Keys: Use Ordering Keys where you *absolutely* need messages to be processed sequentially based on some logical key. Bear in mind enabling ordering will often reduce scalability. Only enable for specific data streams.
  • Exactly Once Delivery: Consider using Google Pub/Sub with Cloud Dataflow for EXACTLY once Delivery on a Stream when messages should never processed more than once.

Conclusion

Google Cloud Pub/Sub provides a robust, scalable, and reliable message queuing solution. By understanding the core concepts and following best practices, you can leverage Pub/Sub to build powerful, decoupled applications and improve the resilience and scalability of your systems.

This comprehensive guide provides a solid foundation for getting started with Pub/Sub. Refer to the official Google Cloud Pub/Sub documentation for more detailed information and advanced configurations.

Pub/Sub Messaging Queue Asynchronous Architecture 
 View : 62


Related


Translate : English Rusia China Jepang Korean Italia Spanyol Saudi Arabia

Technisty.com is the best website to find answers to all your questions about technology. Get new knowledge and inspiration from every topic you search.