Creating Messaging Resources

Introduction

Hey there, welcome back! In our introductory discussion, we briefly touched on the core messaging services available — fully managed solutions perfect for decoupling and scaling microservices, distributed systems, and serverless applications. While we haven't delved into specifics yet, it's important to note that these services offer different types of messaging resources, each with its own features and behaviors. Let's explore these in more detail now:

  • Topics and Subscriptions in Pub/Sub allow you to publish messages to a topic, which are then delivered to all attached subscriptions. This enables asynchronous communication between different parts of your system.

Key Configuration Options for Messaging Resources

Let's delve into some more technical aspects. Messaging resources have a set of characteristics, known as configuration options, that control their behavior. Here are some important ones you should be aware of:

  • Acknowledgment Deadline (Pub/Sub Subscriptions): This setting determines how long a message remains "invisible" to other subscribers after it is delivered. If the message is not acknowledged within this period, it becomes available for redelivery. The default is 10 seconds, but it can be configured up to 600 seconds (10 minutes).

  • Message Retention Duration (Pub/Sub Topics): This specifies how long messages are retained in the topic if they are not yet acknowledged by a subscription. The default is 7 days, but it can be set from 10 minutes to 7 days.

  • Dead Letter Topic (Pub/Sub Subscriptions): This allows you to specify a separate topic where messages that cannot be delivered successfully after multiple attempts will be sent, enabling you to handle failed messages separately.

  • Filter Expression (Pub/Sub Subscriptions): This allows you to filter messages based on their attributes, so that subscriptions only receive messages that match specific criteria.

By adjusting these options when creating your messaging resources, you can optimize their behavior for various use cases. For instance, setting a short acknowledgment deadline reduces the time for a stalled message to become available to other subscribers, whereas a longer message retention duration ensures important messages persist longer if unprocessed.

Creating a Pub/Sub Topic and Subscription

Let's begin with some practical coding! Our first task is creating a simple topic and subscription. The Google Cloud Python client libraries make this process straightforward.

Let's now examine the code:

from google.cloud import pubsub_v1

project_id = "your-project-id"
topic_id = "simple-topic"
subscription_id = "simple-subscription"

publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_id)
topic = publisher.create_topic(request={"name": topic_path})
print("Topic created:", topic.name)

subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)
subscription = subscriber.create_subscription(
    request={
        "name": subscription_path,
        "topic": topic_path,
    }
)
print("Subscription created:", subscription.name)

Output:

Topic created: projects/your-project-id/topics/simple-topic
Subscription created: projects/your-project-id/subscriptions/simple-subscription

In this script, we're importing the necessary modules, initializing the publisher and subscriber clients, and creating a topic and a subscription. The create_topic and create_subscription methods return the created resources, and we print out their names.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal