What is the Pub/Sub model in Google Cloud?

Pub/Sub is a messaging middleware which globally manages event ingestion and messaging.

Why should we use messaging middleware?

Let’s consider a scenario where we might face failures due to tight dependency.

In the case of tight dependency, every component is dependent on some other element for its smooth functioning. The system relies on things to happen in a specific order. When some component fails to perform its function, the functioning of other components is also affected, and that may cause a temporary system failure.

A system without middleware

Here, the backend receives data from two sources:

  1. The user (user data is processed and then sent to the backend)
  2. Marketing

Once the data is received in the backend, it is moved to the frontend.

Let’s suppose that the backend fails at a certain point, maybe due to some hardware issues. In that case, all the messages from these sources are lost, and the frontend stops working because it doesn’t have any data to work with (backend).

A system with backend failure

To avoid such failures, we use a message bus. There are different types of message buses, but we will only learn about a publish/subscribe model (Pub/Sub) in this shot.

Using the Pub/Sub model

Pub/Sub is a layer between the components where we pass the message directly.

In this model, we can split our message bus into different groups of messages. In Pub/Sub, we call these topics. A message can either be sent to or received from a topic. Let’s look at the system using Pub/Sub:

A system with middleware

By introducing Pub/Sub, we have loosely coupled our services and presented resilience in component failure. Pub/Sub in Google Cloud is a fully managed service, that is, messages are queued, and made ready to be consumed again when a component is restored. In this way, we can think of Pub/Sub as a shock absorber for the system.

Pub/Sub can also act on triggers or trigger some actions. Here are a few essential points to know about Pub/Sub:

  • It is Google’s global messaging and event ingestion service.

  • It is a server-less and fully-managed service.

  • We can create multiple publisher/subscriber patterns—for example, one to one, many to one, and so on.

  • It guarantees at least one delivery of every message.

  • We can process messages in real-time or in batches.