Working with Messaging Topics

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.

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