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
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.
Push vs Pull Subscriptions: When to Choose Each
| Factor | Pull Subscription | Push Subscription |
|---|---|---|
| Latency | Slightly higher, as clients poll for messages | Lower, as messages are delivered immediately |
| Scaling | Client controls scaling by running more workers | Google Cloud handles scaling of delivery attempts |
| Endpoint Mgmt | No public endpoint required; client initiates | Requires a publicly accessible HTTP(S) endpoint |
| Retries | Client manages retries and backoff | Google Cloud retries delivery automatically |
| Use Cases | Backend processing, batch jobs, private networks | Real-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.
