📬 How to Create a New Pub/Sub Topic on GCP
Learn the step-by-step process to create a Pub/Sub Topic in Google Cloud and leverage its messaging capabilities for asynchronous communication.
🌟 What is Google Cloud Pub/Sub?
Google Cloud Pub/Sub is a fully-managed messaging service that facilitates asynchronous communication between applications and services. It enables low-latency, durable messaging for workflows like event notifications, real-time data streaming, and asynchronous task execution.
Pub/Sub operates on a **publisher-subscriber model**:
- Publisher: Sends messages to a Topic.
- Subscriber: Receives messages from the Topic.
👉 Learn more about Google Cloud Pub/Sub on the official documentation.
Key Features of GCP Pub/Sub
- Publisher-Subscriber Model:
- Publisher: Sends messages to a topic.
- Subscriber: Subscribes to a topic and receives messages via a subscription.
- Asynchronous Messaging:
- Enables systems to operate independently without waiting for immediate responses.
- Durable and Reliable:
- Messages are stored until they are delivered to subscribers or their retention duration expires.
- Global Scalability:
- Handles millions of messages per second with low latency.
- Scales automatically to accommodate varying workloads.
- Message Ordering:
- Ensures that messages published to a topic with message ordering are delivered in the same order.
- Integration:
- Works seamlessly with other Google Cloud services like Dataflow, BigQuery, and Cloud Functions.
- Schema Support:
- Ensures that published messages conform to a predefined schema, reducing errors.
- Dead Letter Queue (DLQ):
- Unacknowledged messages can be forwarded to a dead letter topic for debugging or reprocessing.
- Security:
- Integrated with IAM (Identity and Access Management) for role-based access control.
- Supports encryption for data at rest and in transit.
Core Components of GCP Pub/Sub
- Topics:
- A topic is a named resource to which publishers send messages.
- It acts as the entry point for messages.
- Messages:
- Messages are data objects sent by publishers.
- Each message can contain a payload (data) and attributes (key-value metadata).
- Subscriptions:
- A subscription is the mechanism that delivers messages from a topic to a subscriber.
- Types of subscriptions:
- Pull Subscription: Subscribers pull messages from Pub/Sub.
- Push Subscription: Pub/Sub pushes messages to an endpoint specified by the subscriber.
- Dead Letter Topics:
- A destination for messages that cannot be delivered successfully.
- Acknowledgment:
- Subscribers must acknowledge (ack) receipt of a message to indicate successful processing.
How GCP Pub/Sub Works
- Publish Messages:
- A publisher sends a message to a topic.
- Messages are retained in Pub/Sub until they are acknowledged by all subscriptions or the retention period expires.
- Deliver Messages:
- Pub/Sub delivers messages to all active subscriptions for a topic.
- Messages can be delivered via pull or push methods.
- Acknowledge Messages:
- Subscribers process the messages and send an acknowledgment to Pub/Sub.
- Unacknowledged messages are retried until acknowledged or until they expire.
Use Cases of GCP Pub/Sub
- Real-Time Event Streaming:
- Stream and process data in real-time, such as sensor data, logs, or clickstream data.
- Asynchronous Workflows:
- Decouple services in a microservices architecture to allow asynchronous communication.
- Event Notifications:
- Send notifications for events like user sign-ups, payment processing, or system alerts.
- Data Ingestion:
- Ingest large volumes of data into data lakes or streaming pipelines.
- IoT Applications:
- Connect IoT devices to cloud applications for data streaming and processing.
- Distributed Systems:
- Use Pub/Sub to synchronize events between distributed applications.
Types of Subscriptions in Pub/Sub
- Pull Subscription:
- Subscribers actively pull messages from the subscription.
- Suitable for applications that control when they receive messages.
- Push Subscription:
- Pub/Sub pushes messages to a subscriber’s endpoint (HTTP/HTTPS).
- Ideal for applications that can process messages in real-time.
- Streaming Subscription:
- Stream messages using Pub/Sub client libraries for high-performance message processing.
🔧 Step-by-Step Guide to Create a Pub/Sub Topic
Follow these steps to create a Pub/Sub Topic in Google Cloud:
1. Log in to Google Cloud Console
- Go to the Google Cloud Console and sign in with your credentials.
- Ensure you have the required permissions (e.g., Pub/Sub Admin role) to create a Topic.
2. Navigate to the Pub/Sub Section
- Open the Pub/Sub Topics Page.
- Click the Create Topic button.
3. Create a New Topic
In the “Create a Topic” pop-up:
- Enter a Topic ID (e.g., `my-topic`). This will be the unique identifier for your Topic.
- Optional: Configure additional settings such as:
- Default Subscription: Automatically creates a subscription for this Topic.
- Use a Schema: Enforce a specific message format.
- Message Retention Duration: Retain messages for up to 31 days (a paid feature).
- Click Create Topic.
✨ Additional Options When Creating a Pub/Sub Topic
1. Add a Default Subscription
By selecting this option, a subscription will be automatically created with the following default settings:
- Delivery type: Pull
- Message retention: 7 days
- Expiration: 31 days of inactivity
- Acknowledgment deadline: 10 seconds
- Retry policy: Immediate
If you want a customized subscription, follow these steps:
- Navigate to the Subscriptions Page.
- Click Create Subscription.
- Enter a Subscription Name and select the Topic ID.
- Configure additional settings like Dead-Lettering, message expiration, or retry policies.
- Click Create.
2. Use a Schema
A Schema enforces a specific format for the messages published to a Topic. You can either:
- Create a new schema for the Topic.
- Assign an existing schema to the Topic.
Select the schema type (e.g., Protocol Buffers or Avro) and define the encoding format.
3. Set Message Retention Duration
By default, Pub/Sub Topics retain messages for 7 days. Enabling **Message Retention Duration** lets you retain messages for up to 31 days. This feature is useful for data recovery or reprocessing but incurs additional costs.
📋 Use Cases for Google Cloud Pub/Sub
- 🚀 Event Notifications: Deliver real-time notifications across distributed applications.
- 📊 Streaming Data: Process real-time data from IoT devices or logs.
- 🔄 Asynchronous Workflows: Decouple microservices by implementing asynchronous communication.
- 📦 Queueing Systems: Use Pub/Sub to create durable and scalable message queues.
