Implementing PUB/SUB in Google Cloud
Implementing PUB/SUB in Google Cloud
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.
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 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:
- In the google cloud console, navigate to the Pub/Sub topic page.
- Click create topic and enter a unique name.
Subscribe to the Topic:
- Once a Pub/Sub topic is created, add a subscriber to this. Click: Create subscriptions.
- Name the subscription and select one of the options as push or pull and then click the create button
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.
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.