5 min read

Do More With Google Cloud Pub/Sub

Dec 22, 2021 8:00:00 AM

Do More With Google Cloud Pub_SubPicture Credit: Unsplash

As official partners of the Google Cloud Platform, Nisum offers reflections on what we have learned and to consider key takeaways and some basic walk-throughs and guides to help get you started.

One of our rising talents, Nasir Hussain, a Sr. DevOps Intern, will dig into one of the products that he has found to be quite high “on the awesome scale,” called Pub/Sub.

What Is Google Cloud's Pub/Sub?

Pub/Sub (Publish/Subscribe) Messaging is an asynchronous messaging service that decouples services and produces events from services that process events.

​​As the name suggests, Pub/Sub works in a Publisher / Subscriber model and uses event-driven applications. This means you can have applications publishing and subscribing to events that are available as messages. Simply put, a Publisher publishes a message, and the Subscriber acknowledges that message.

Here are some key points according to Google:

  • Pub/Sub allows services to communicate asynchronously, with latencies on the order of 100 milliseconds.
  • Pub/Sub is used for streaming analytics and data integration pipelines to ingest and distribute data.
  • It is equally effective as messaging-oriented middleware for service integration or as a queue to parallelize tasks.

Some salient questions should come to mind. The first was - How do we make sure that the Publisher publishes to the right place and the Subscriber is listening to the right place? We’ll demonstrate how this works below.

New call-to-action

Let's Take a Look Under the Hood at Pub/Sub's Core Constructs:

First here are some key terms explained:

  • Topic: An intermediary channel that maintains a list of subscribers to relay messages to that received from publishers
  • Message: Serialized messages sent to a topic by a publisher which does not know the subscribers
  • Publisher: An application that publishes a message to a topic
  • Subscriber: An application that registers itself with the desired topic to receive the appropriate messages

Let's Walk Through a Pub/Sub Logic Model:

Now that we called out the Core components, let’s run through an example of how the Pub/Sub Model Works:

  • Publisher: Create a topic in Pub/Sub
  • Publisher: Create a subscription for that topic that plays the Subscriber's part
  • Publisher: Publish messages in the corresponding Pub/Sub topic Messages are available within our Pub/Sub topic
  • Subscriber: Consumes messages by acknowledging them

The diagram below demonstrates a pictorial view of the logic model used in this blog.

Do More with Google Cloud PubSub

Cloud Pub/Sub Has Two Benefits: Loose Coupling and Scalability

  1. Loose Coupling: Publishers are unaware of the subscribers' presence, and it provides the ability for the system to function independently of each other.
  2. Scalability: Pub/Sub messaging can scale to volumes beyond the capability of a single traditional data center.

Enough Talk - Let's Get Hands-on

First, you will need two things for this demo:

  • A GCP account
  • gcloud CLI (Google Cloud Shell)

USE CASE: You are the owner/operator of a Coffee Shop. Your customers can push messages or order their beverage to a topic or queue.

At each barista station, subscribed baristas can pull one message (order) at a time and confirm it to start up the espresso machine.

Creating the Topic in Pub/Sub

Step 1

Create the topic in pub/sub using gcloud CLI.
```code```
$ gcloud pubsub topics create coffee-shop

With the topic created as coffee-shop, the barista needs to join as a subscriber.

Creating a Subscription for the Topic

Step 2

```code```
$ gcloud pubsub subscriptions create --topic coffee-shop barista

We're creating a subscription as a barista that subscribes to the topic created in step 1.

And there we have it. You are done with Pub/Sub setup - easy, right? Now let's integrate it with our applications as subscribers and publishers.

Publishing and Consuming Messages:

Let's assume we've got a customer that wants an add-on to their espresso- a dash of cinnamon.

  1. All they need to do is publish the following message in the coffee-shop topic as: {"coffee_type": "espresso", "cinnamon": "true"}
  2. Use the gcloud CLI to publish this message as:
    $ gcloud pubsub topics publish coffee-shop --message '{"coffee_type": "espresso", "cinnamon": "true"}'
  3. gcloud CLI publishes the message on the coffee-shop topic.


For the barista to take coffee orders, they use their subscription to consume the messages.

We'll use gcloud CLI as well.

$ gcloud pubsub subscriptions pull barista --limit=1 --auto-ack

NOTE: We're using the --limit flag to ensure we only receive one message from the topic.

NOTE: For the --auto-ack flag to automatically acknowledge the message.

The notes above make sure consumers receive a message successfully by acknowledging its receipt.

We can also manually acknowledge the messages.

A Pub/Sub Solution Now Ready to Use

See how easy this was? This is a good reason to get excited about Pub/Sub and the Google Cloud Platform in general. For more information on Quickstarts, Samples, and How to Guides, please visit cloud.google.com/pubsub.

While the Pub/Sub concept is easy to understand, keep in mind that every coding and programming language handles things differently, so make sure to check before running into challenging learning curves across other platforms.

New call-to-action

Let Nisum’s Digital Services Help

Nisum utilizes Google Cloud to provide Cloud Development and Integration services to its clients and Cloud migration services. See how Nisum’s Digital Services covers all aspects of digital transformation journeys. From DevOps to cloud development, Nisum is here to help guide you as you start your Google Cloud Platform journey.

Contact us to learn more about how Nisum can develop your GCP practice and implement best practices across teams, programs, and portfolios for your organization.

Nasir Hussain

Nasir Hussain

Nasir Hussain is a Sr. DevOps Intern in Nisum's Automation and Cloud Integration division and is an enthusiast of open-source programs and technologies. He has previously contributed to the development and maintenance of Kubernetes, NixOS, and Fedora Project.

Have feedback on this blog? Leave a comment!

Featured

Blog by Topics

See All