• Skip to main content
  • Skip to primary sidebar
BMA

BeMyAficionado

Inspire Affection

What is Google Cloud Pub/Sub and How Does it Work?

March 18, 2019 by varunshrivastava Leave a Comment

Have you ever found yourself in a situation where you have to process a tons of requests, but unfortunately the processing speed of your machine is less than the volume of requests.

This is a dreadful situation because sooner or later, your service is going to get stalled or the requests will start getting time-out. This is exactly the kind of situation where you can make use of something called message queue.

With message queue, you simply take the incoming requests and put them in a queue. Then your service can pull these requests from the queue one-by-one and process it in its own time.

Table of Contents

  • What is Pubsub?
  • How does Pub/Sub work?
  • Where to Use Cloud Pub/Sub?
    • Common use cases
    • Benefits of using Google Cloud Pub/Sub
    • Use as a Service
    • Auto adjust message delivery
    • Trigger on Events
  • Conclusion

What is Pubsub?

Well, Pub/Sub is an implementation of a Publisher/Subscriber design. And Cloud Pub/Sub is a Google product that brings the flexibility and reliability of enterprise message-oriented middleware to the cloud. In short, Cloud Pubsub provides many-to-many, asynchronous messaging that decouples senders and receivers, it allows for secure and highly available communication among independently written applications.

The most important part of the definition is that “Pub/Sub decouples the Senders (Publishers of the messages) from the Receivers (Subscribers of the messages)”. This means that two services can talk to each other using a common language, without having any kind of knowledge about the other.

This decoupling of services makes the communication more secure and requires fewer resources from their end even when dealing with a lot of requests. This makes Cloud Pub/Sub a really good way to create highly available communication between different services.

So, next time if someone asks you what is Pub/Sub, tell them it is just a message queue that decouples senders from receivers.

How does Pub/Sub work?

This is a very simple and elegant way to communicate between two parties. Let’s take a simple example- Suppose you visit an e-commerce store to buy a product. And you found that the product is not yet available and there is an option to subscribe for notification. What this option does it to take your email address and save it in its database under the category of subscribers so that whenever the product you subscribed is launched, you would get the notification.

The pub/sub model works exactly the same way.

There is a publisher and there are multiple subscribers. The publisher sends a message to a particular topic and all the subscribers who have opted to get a notification on that topic will receive the data.

Publisher/Subscriber Model

Simple. And. Elegant. 🙂

I think the Pub/sub model is pretty clear now, so let’s get a little deeper and understand how it simplifies the communication.

Publisher/Subscriber model is part of Message Queue paradigm. Message Queue simplifies the communication because now the sender and receiver need not process synchronously. The receiver can process messages at its own pace and sender can send messages at its own pace.

Message Queue

The sender no longer has to have the knowledge of the receiver. The sender can now send the messages at its own pace to intermediate message queue. And the receiver can process the message one-by-one at its own pace. No real time coupling required.

Once the receiver is done with the processing, it sends the acknowledgement back to the intermediate message queue so that the message queue can delete the processed message from the queue. Here is a short article about the working of the Message Queue: Click to Read.

In short, the Publisher/Subscriber model is an asynchronous decoupled way of communicating between two services.

Where to Use Cloud Pub/Sub?

Let’s list down the common use cases for the Pub/Sub model. These use cases are not restricted to Cloud Pub/Sub. Any publisher/subscriber model is capable of doing below tasks. Here are some of the most common use cases of Pub/Sub model.

Common use cases

  • Balancing workloads in network clusters. For example, a large queue of tasks can be efficiently distributed among multiple workers, such as Google Compute Engine instances.
  • Implementing asynchronous workflows. For example, an order processing application can place an order on a topic, from which it can be processed by one or more workers.
  • Distributing event notifications. For example, a service that accepts user signups can send notifications whenever a new user registers, and downstream services can subscribe to receive notifications of the event.
  • Refreshing distributed caches. For example, an application can publish invalidation events to update the IDs of objects that have changed.
  • Logging to multiple systems. For example, a compute instance can write logs to the monitoring system, to a database for later querying, and so on.
  • Data streaming from various processes or devices. For example, a residential sensor can stream data to backend servers hosted in the cloud.
  • Reliability improvement. For example, a single-zone computing service can operate in additional zones by subscribing to a common topic, to recover from failures in a zone or region.

I’ve personally used it for Implementing asynchronous workflows and Distributing event notifications.

Benefits of using Google Cloud Pub/Sub

There are many benefits of using Google Cloud Pub/Sub.

Use as a Service

The main advantage of using the Google Cloud Pub/Sub is that you do not have to worry about anything. From maintaining the queue to sending the data to the subscribed users have been taken care of by Google itself.

You just have to create topics and register subscribers.

The publisher service will send message to the topic and everything beyond that will be handled by the Google Cloud Pub/Sub. That is maintaining the queue, sending the message to the subscriber, getting the acknowledgement etc etc…

Auto adjust message delivery

This is another useful feature that you get out-of-box when you use Google Cloud Pub/Sub. It automatically matches the processing speed of the subscribers and send message accordingly.

Suppose it takes subscriber 2 seconds to process a message, the Cloud Pub/Sub will automatically adjust the delivery speed so that the receiving service is not over loaded with requests.

This feature comes pretty handy when you have to process the bulk messages reliably.

Also, it makes sure to keep sending the message again and again until the subscriber has rightfully acknowledged it. Pub/Sub has an option to retain the messages for as long as 7 days. It will keep on sending the message for 7 days until the message has been acknowledged by the subscriber.

Trigger on Events

The Cloud Pub/Sub can be configured to listen to particular events and trigger the subscribers.

Storage Notification Cloud Pub/Sub

The event could be uploading a file to cloud storage. As soon as you upload the file to Cloud Storage Bucket, a finalize event is fired. Cloud Pub/Sub can be configured to listen for such events on a particular bucket, so that as soon as the file is landed on the bucket, it could be picked up for processing. All of this happens asynchronously in the backend.

There are a lot many benefits of using Google Cloud Pub/Sub. I have listed a few above. And ofcourse, you get the reliability and scalability of Google. As Google uses the same infrastructure that it provides to its consumers. If any problem arises, it would be found and fixed by Google itself before it could reach to you. So you can rely on Google for that.

Conclusion

Google Cloud Pub/Sub is a really good solution for Bulk data processing, distributed events notification, asynchronous workflows and reliability improvement.

Cloud Pub/Sub can be integrated with any architecture with minimum effort. And on top of that, it is pretty low priced.

I would suggest you to take it for a spin and do not forget to comment your opinions belows.

Related

Filed Under: Technology Tagged With: cloud, google, message-queue, publisher, pubsub, subscriber

Primary Sidebar

Subscribe to Blog via Email

Do you enjoy the content? Feel free to leave your email with me to receive new content straight to your inbox. I'm an engineer, you can trust me :)

Join 874 other subscribers

Latest Podcasts

Recent Posts

  • Is The Cosmos a Vast Computation?
  • Building Semantic Search for E-commerce Using Product Embeddings and OpenSearch
  • Leader Election with ZooKeeper: Simplifying Distributed Systems Management
  • AWS Serverless Event Driven Data Ingestion from Multiple and Diverse Sources
  • A Step-by-Step Guide to Deploy a Static Website with CloudFront and S3 Using CDK Behind A Custom Domain

Recent Comments

  • Varun Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Vaibhav Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Varun Shrivastava on Should Girls Wear Short Clothes?
  • D on Should Girls Wear Short Clothes?
  • disqus_X5PikVsRAg on Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java

Categories

  • Blogging
  • Cooking
  • Fashion
  • Finance & Money
  • Programming
  • Reviews
  • Software Quality Assurance
  • Technology
  • Travelling
  • Tutorials
  • Web Hosting
  • Wordpress N SEO

Archives

  • November 2024
  • September 2024
  • July 2024
  • April 2024
  • February 2024
  • November 2023
  • June 2023
  • May 2023
  • April 2023
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • November 2021
  • September 2021
  • August 2021
  • June 2021
  • May 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016

Tags

Affordable Hosting (4) algorithms (4) amazon (3) aoc-2020 (7) believe in yourself (4) best (4) database (4) earn money blogging (5) education (4) elementary sorting algorithms (4) experience (3) fashion (4) finance (6) Financial Freedom (7) food (7) friends (3) goals (5) google (5) india (10) indian cuisine (5) indian education system (4) java (16) life (16) life changing (4) love (4) make money (3) microservices (9) motivation (4) oops (4) podcast (6) poor education system (4) principles of microservices (5) problem-solving (7) programmer (5) programming (28) python (5) reality (3) seo (6) spring (3) success (10) success factor (4) technology (4) top 5 (7) typescript (3) wordpress (7)

Copyright © 2025 · Be My Aficionado · WordPress · Log in

Go to mobile version