Google Cloud Pub Sub

What is Pub/Sub?

Pub/Sub is an asynchronous messaging service that decouples/separates services that produces events from the services that consumes events.

It means Pub/Sub is a messaging queue which is distributed in nature with many producers and consumers with everything completely managed by google. 

While it is a big benefit with google managing everything, it also provides durable message storage, high availability (of course !! With thousands of nodes due to its massive scale) and also consistent performance at peak traffic(Very much scalable).

 



Core Concepts

We have already introduced what is pub/sub means?  It is short form for publisher/subscriber which any one can guess, genius. It is a messaging queue right in which publishers produce messages and subscribers consume from it.  

But there can be a lot of them. So, how to identify to consume messages produced by a specific publisher. 

Topic - A topic is a named resource agreed upon by both in which the publishers publish the messages.  

Why the above sentence is not complete? Subscribers consume from it right?

Yes, but partially. 

For a consumer to consume a message from a topic, first a subscription needs to be created? 

Subscription - A subscription is also a named resource which represents the stream of messages to be consumed by subscriber. 

But why? 

There might be a case where there are multiple subscribers consume same messages produced by a publisher. 

Next one is which we obviously talking about - the message.

Message -   The combination of data and (optional) attributes that a publisher sends to a topic and is eventually delivered to subscribers. The attributes are nothing but a map of key-value pairs. 


Enough of the this theoretical knowledge. Let's dive in to create a topic and  subscription from google cloud console. 

Basically, everyone gets a $300 worth  free credits to test. Yes, later it is a paid service. But, it is cheap compared to other alternatives For the purpose of this tutorial blog, create a new account and  follow this page to create a topic and a subscription. 




Let's Implement

After the topic and subscription is created, create a service account and download the service account json file from here

Oh, wondering what is a service account means?

According to docs, 

It is a special kind of account used by an application or a virtual machine (VM) instance, not a person. Applications use service accounts to make authorized API calls, authorized as either the service account itself, or as Google Workspace or Cloud Identity users through domain-wide delegation. 

Basically it is used to identify and authorize the services. 

Also, if you are wondering what is a project Id means, refer this page.

Now with all things clear, lets dive into the code.  First of all things, let's create a method to fetch the credentials from service account json file. It is basically to let us use the queue we created. 

Now let us create a method for a create SubsriberStub  object. 

There are two ways in which we can consume the messages namely pull and push. The usage is based on the requirements. 

Let's us implement a method to pull messages from the subscription. We can also mention how many messages we want to pull per try. 

Now we are done with subscriber part, however there is one  more thing. There might be a case where the consumed message might not be processed and we might need to replay that message from the queue. 

There are different settings but we are interested in manual acknowledgement of the messages so that messages are removed from the queue and are not replayed. 


Now let us create a publisher to publish a message. It is very simple. Create a publisher object and a method to publish to a topic with message attributes. 


With everything in place, replace the placeholders and test the code.


This concludes our tutorial, for more details keep the pub/sub docs in handy.



You can also watch the pub/sub series by Priyanka Veragadia, a google advocate explained the concepts in a very clear and concise manner.



Keep Experimenting 🔎 

Keep Learning 🚀


Post a Comment

Previous Post Next Post