Introduction

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.

Understanding Topics and Subscriptions in Google Cloud Messaging

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 topic can handle very high message throughput, supporting millions of messages per second.
  • The maximum message size is 10 MB.
  • Each topic can have multiple subscriptions, and each subscription can 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: Subscriptions can be configured to forward undeliverable messages to a separate topic for 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.

Push vs Pull Subscriptions: When to Choose Each
FactorPull SubscriptionPush Subscription
LatencySlightly higher, as clients poll for messagesLower, as messages are delivered immediately
ScalingClient controls scaling by running more workersGoogle Cloud handles scaling of delivery attempts
Endpoint MgmtNo public endpoint required; client initiatesRequires a publicly accessible HTTP(S) endpoint
RetriesClient manages retries and backoffGoogle Cloud retries delivery automatically
Use CasesBackend processing, batch jobs, private networksReal-time notifications, webhooks, public APIs
  • 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.

Creating Topics in Google Cloud Messaging

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 publisher client is initialized.
  • The fully qualified topic_path is constructed using your project_id and the desired topic_id.
  • The topic is created using the create_topic method.
  • The topic's name is printed to confirm successful creation.
Configuring Topics

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 topic if there are no subscriptions.
  • Access control: Using Identity and Access Management (IAM) policies to control who can publish or subscribe to the topic.

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.

Listing Your Topics

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:

Achieving Fan-Out and Reliable Delivery

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.

Review, Practice, and Next Steps

Let's review what we've learned:

  • The concept of topics and subscriptions in Google Cloud messaging.
  • How to create, configure, and list topics.
  • How to use multiple subscriptions to 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!

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