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 attachedsubscriptions
. This enables asynchronous communication between different parts of your system.
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 asubscription
. 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.
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:
Output:
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.
You can customize your subscription
by setting particular options during its creation. Here is an example of a subscription
with an acknowledgment deadline of 45 seconds and a message retention duration of 1 day (86,400 seconds):
Output:
These options adjust various aspects of message handling in the subscription
, allowing you to tailor the resource's behavior to fit your application's needs.
Great job! You've now learned how to create and configure messaging resources with different options for message delivery and retention. Be sure to practice these skills in the upcoming exercises, where you'll be asked to create and configure topics
and subscriptions
on your own.
In the next unit, we'll move on to "Sending and Receiving Messages." Remember, practice makes perfect. Keep coding, and I'll see you in the next lesson!
