Implementing PUB/SUB in Google Cloud

Implementing PUB/SUB in Google Cloud

tudip-logo

Tudip

13 April 2020

How PUB/ SUB works?

Pub/Sub stands for publishing and subscribing. As the name suggests, this data streaming procedure is based on mainly three parts: Publisher, topic and subscriber.

Due to the use of publisher and subscriber, this procedure has been considered as durable, reliable and flexible.
Pub/Sub data streaming procedure, mainly decouples the sender from the receiver due to which it has found a secure way to process the data between the applications which are independently created. It has the capability of ingesting a large amount of data which is possible due to scalability of the service.

Here are the core concepts

  • Topic: To which publisher sends the messages.
  • Subscription: In order to get the data published to the topic, we must subscribe to that topic. Subscription represents the streaming message from a unique topic to be delivered to the subscribing application.
  • Message: A data which is going to be published and subscribed.
  • Message attribute: Depending on the receiver’s language, the publisher can define a key-value pair for the message.

pub-sub-in-google-cloud-1

Data flow in Pub/Sub

Data flow in pub/sub service is mainly dependent on the publisher application, topic and a subscribing application.

A publisher application that can be any application which makes a Hypertext Transfer Protocol Secure requests to the web services, app engine, publishes the messages to the topic.

A subscribing application at subscribing end, subscribes to that topic. A subscriber can be a pull subscriber or a push subscriber.

A pull subscriber requests a Hypertext Transfer Protocol Secure request to the google apis and a push subscriber accepts the data which is streaming over HTTPs.

The communication can be in any manner like Many subscribers to one publisher and one publisher to many subscribers.

pub-sub-in-google-cloud-2

Pub/Sub sends out the messages from the topic to all of its subscribers and waits for the acknowledgement. Once a message is received, it is acknowledged by the subscriber, it is deleted from the queue of the subscription message.

Use cases

User case Description
Improving reliability A computer Engine service having a single zone can be operated in additional zones by subscribing to the common topic. In this way, it can be recovered from the zonal failure.
Notifications can be distributed. If we take an example of such an application which sends out the notification whenever a new user signs up with it, a service used as downstream can subscribe to the topic to get the notifications.
To make workflow independent We can make a workflow independent in the applications which processes orders. These applications can place an order on one topic and subscribers to that topic can process these requests.

How data processes in Pub/Sub?

Create a topic:

  1.  In the google cloud console, navigate to the Pub/Sub topic page.
  2. Click create topic and enter a unique name.pub-sub-in-google-cloud-3-1024x293

Subscribe to the Topic:

  1. Once a Pub/Sub topic is created, add a subscriber to this. Click: Create subscriptions.pub-sub-in-google-cloud-4
  2. Name the subscription and select one of the options as push or pull and then click the create button

    pub-sub-in-google-cloud-5-1024x582

Publish a message to the topic:

To publish a message to the topic. For the topic we just created, click on the Publish a message button and enter a suitable message in the message field and finally, publish it with the help of the publishing button.

pub-sub-in-google-cloud-6-1024x520

Pull the data published:

Finally,execute gcloud pubsub subscriptions pull –auto-ack [SUBSCRIPTION_NAME]  command to pull the data from the subscription.

The output should display the message published.

pub-sub-in-google-cloud-7-1024x127

search
Blog Categories
Request a quote