Hello, learners! In today's lesson, we'll delve deeper into messaging concepts on Google Cloud. One of the core building blocks in Google Cloud messaging is the topic. Topics act as communication channels where messages can be published, and subscribers to these topics receive those messages. Our focus today will be on understanding topics and how they enable reliable and scalable communication between different parts of your application.
In Google Cloud messaging, topics are central to the publish/subscribe (Pub/Sub) model. A topic is a named resource to which messages are sent by publishers. Subscriptions are entities that represent the stream of messages from a single, specific topic, delivered to the subscriber.
Here are some important details to note:
- You can create up to 10,000 topics per project by default (this limit can be increased).
- Each topiccan handle very high message throughput, supporting millions of messages per second.
- The maximum message size is 10 MB.
- Each topiccan have multiplesubscriptions, and eachsubscriptioncan deliver messages to different types of endpoints.
Types of subscriptions/endpoints include:
- Pull subscriptions: Applications explicitly pull messages from the subscription.
- Push subscriptions: Messages are automatically sent to a specified HTTP or HTTPS endpoint.
- Dead-letter topics: Subscriptionscan be configured to forward undeliverable messages to a separatetopicfor further analysis or reprocessing.
Each subscription type has its own delivery and retry policies, and you can configure message retention and acknowledgment deadlines to suit your application's needs.
- Choose pull subscriptions when you want more control over message processing, need to operate behind firewalls, or want to manage scaling and retries yourself.
- Choose push subscriptions when you need low-latency, real-time delivery to a web service, and can expose a secure HTTP(S) endpoint.
For example, in an eCommerce application, a topic could be used to broadcast order updates. Multiple services — such as notification systems, analytics, and inventory management — can each subscribe to the topic and receive updates in real time.
Now, let's see how to create a topic in Google Cloud messaging. The following example demonstrates how to create a new topic using the available tools:
Output:
Here's what happens:
- The publisherclient is initialized.
- The fully qualified topic_pathis constructed using yourproject_idand the desiredtopic_id.
- The topicis created using thecreate_topicmethod.
- The topic's name is printed to confirm successful creation.
After creating a topic, you may want to configure its attributes for optimal performance and security. Some key configuration options include:
- Labels: Key-value pairs that help you organize and manage your topics.
- Message retention duration: How long messages are retained in the topicif there are nosubscriptions.
- Access control: Using Identity and Access Management (IAM) policies to control who can publish or subscribe to thetopic.
To set labels and manage access control, you can use the following example:
Output:
To manage access control, you would typically use the Google Cloud Console or the IAM API to grant publish or subscribe permissions to specific users or service accounts.
After creating one or more topics, you may want to view all existing topics in your project. Here's how you can list all topics:
Output:
This will print the names of all topics in your project, allowing you to manage and organize your messaging resources effectively.
Let's also see how to get more detailed information about each topic:
Output:
Google Cloud messaging enables the fan-out pattern by allowing a single topic to have multiple subscriptions. When a message is published to a topic, it is delivered to all attached subscriptions. Each subscription can be processed independently, and messages are retained until they are acknowledged by each subscriber.
For example, you can have one subscription that pushes messages to a web service, another that allows a backend service to pull messages for processing, and a third that forwards undelivered messages to a dead-letter topic. This ensures reliable delivery and allows different parts of your application to process messages in parallel, increasing resilience and scalability.
Let's review what we've learned:
- The concept of topicsandsubscriptionsin Google Cloud messaging.
- How to create, configure, and list topics.
- How to use multiple subscriptionsto achieve fan-out and reliable message delivery.
In the upcoming exercises, you'll practice creating topics, setting labels, and listing all topics in your project. This foundational knowledge will prepare you for more advanced messaging patterns and configurations in future lessons. Happy learning!
