How to Use Cloud Pub/Sub for Event-driven Architecture on GCP?
Last Updated :
24 Apr, 2025
Pre-requisites: Google Cloud Services
Cloud Pub/Sub is a fully managed, scalable messaging system offered by Google Cloud Platform (GCP) that enables asynchronous communication between applications. It is a simple and reliable way to exchange data between services and applications in real time. With its event-driven architecture, Cloud Pub/Sub provides a flexible way to build scalable, reliable, and loosely-coupled applications that can handle large-scale data processing.
In this article, we will explore how to use Cloud Pub/Sub for event-driven architecture on GCP. We will go through the steps required to set up a Cloud Pub/Sub topic and subscription, publish and consume messages, and demonstrate how to integrate Cloud Pub/Sub with other GCP services.
Key Terminologies
- Topic: A named resource to which messages can be sent by publishers.
- Subscription: A named resource representing the stream of messages from a single, specific topic, to be delivered to the subscribing application.
- Publisher: An application or service that sends messages to a topic.
- Subscriber: An application or service that receives messages from a subscription.
- Message: The unit of data exchanged between publishers and subscribers.
Let’s say you are building a simple application that sends notifications to users when a new post is added to a forum. You can use Cloud Pub/Sub to build an event-driven architecture where the application publishes a message to a Cloud Pub/Sub topic whenever a new post is added, and subscribers consume these messages to send notifications to users.
Steps to use Cloud Pub/Sub for event-driven architecture:
Step 1: Sign up for a Google Cloud Platform (GCP) account if you don’t already have one. You can sign up for a free trial that includes $300 in credit to use on GCP services.
Step 2: Enable Cloud Pub/Sub API: In the Cloud Console, navigate to APIs & Services > Dashboard, click Enable APIs and Services, search for Cloud Pub/Sub, and enable the API.
Step 3: Create a Topic: In the Cloud Console, navigate to Pub/Sub > Topics.
Step 4: After Clicking on ‘Topics’ , Click on Create Topic.
Step 5: Give a name to your Pub/Sub topic such as “new-post-topic” and keep all the things as default and Click on ‘CREATE’.
Step 6: Create a Subscription: In the Cloud Console, navigate to your topic, click Create Subscription, give it a name such as “new-post-subscription”, and choose the delivery type as “Pull”.
Step 7: Leave all other fields as it is and Click on ‘Create’.
Now you have done with creating Cloud Pub/Sub for event-driven architecture.
Step 8: Publish a Message
In your application code, whenever a new post is added, publish a message to the “new-post-topic” with the post information as the payload.
Here’s an example code snippet in Python:
Python3
from google.cloud import pubsub_v1
publisher = pubsub_v1.PublisherClient()
topic_path = "projects/<project-id>/topics/new-post-topic"
def publish_message(post):
data = post.encode( "utf-8" )
future = publisher.publish(topic_path, data = data)
print (future.result())
|
Step 9: Consume Messages
In your notification service code, set up a subscription to the “new-post-subscription” and consume messages whenever they are available.
Here’s an example code snippet in Python:
Python3
from google.cloud import pubsub_v1
subscriber = pubsub_v1.SubscriberClient()
subscription_path = "projects/<project-id>/subscriptions/new-post-subscription"
def handle_message(message):
print (f "Received message: {message.data}" )
message.ack()
subscriber.subscribe(subscription_path, callback = handle_message)
while True :
time.sleep( 1 )
|
With these steps, your application can now use Cloud Pub/Sub to publish and consume messages and build a scalable and reliable notification system for new posts in the forum.
Similar Reads
How To Use Azure Functions For Event-Driven Architecture ?
Azure Function is a service provided by Microsoft Azure for running serverless workloads. It provides support for building and developing event-driven applications without explicitly provisioning or managing infrastructure. It also provided for the development of applications based on functions in t
4 min read
How to Use Cloud NAT For Outbound Internet Access on GCP?
NAT Gateway is a Network address translation gateway that enables multiple devices to access a public network through a single IP address. It is mainly used to conserve registered IP addresses and use private IP addresses instead. Cloud NAT is used to expose internal private resources to the interne
3 min read
Event-Driven Architecture Patterns in Cloud Native Applications
Event-driven architecture (EDA) transforms cloud-native applications by enabling real-time responsiveness and scalability. This article explores key EDA patterns, their benefits in dynamic cloud environments, and practical strategies for implementing them to optimize performance and resilience. Impo
9 min read
How To Create a Pub/Sub Topic on GCP?
Pub/Sub is a messaging service that allows you to send and receive messages between independent applications. Separating the senders and receivers enables secure and consistent communication between independently developed applications. With Pub/Sub, you can expect quick delivery of messages while e
2 min read
Event-Driven Architecture vs. Microservices Architecture
In system design, choosing the right architecture is crucial for building scalable and efficient systems. Two popular approaches, Event-Driven Architecture (EDA) and Microservices Architecture, each offer unique benefits. This article explores their definitions, differences, use cases, and more. Tab
4 min read
Edge-Cloud Architecture in Distributed System
Edge-Cloud Architecture in Distributed Systems explores a modern approach to managing data and processing power. It discusses how combining edge computing (smaller, local data centers) with cloud computing (larger, centralized data centers) can improve efficiency and response times for applications.
11 min read
Google Cloud Architecture Framework
Pre-requisite: Google Cloud Google Cloud Architecture Framework is a set of best practices and guidelines provided by Google Cloud Platform (GCP) to help users design, build, and operate scalable, secure, and highly-available applications on GCP. The framework is designed to help users make the best
8 min read
How to Use Cloud Datastore For NoSQL Database On GCP?
Developers can store and retrieve data using Cloud Datastore, a powerful NoSQL document database offered by Google Cloud Platform (GCP). This detailed article will examine the major elements of using Cloud Datastore as a NoSQL database on GCP, covering everything from setup to advanced querying and
6 min read
Event-Driven APIs in Microservice Architectures
Event-driven APIs in Microservice Architectures explain how microservices, which are small, independent services in a larger system, can communicate through events. Instead of calling each other directly, services send and receive messages (events) when something happens, like a user action or a sys
12 min read
How to Restore State in an Event-Based, Message-Driven Microservice Architecture on Failure Scenario?
In microservice architectures, ensuring state consistency during failures is crucial. This article explores effective strategies to restore state in event-driven microservices, emphasizing resilience and data integrity. Important Topics to Understand State Restoration in Event-Driven Microservices W
7 min read