Overview of the Pub/Sub service (original) (raw)

Pub/Sub is a publish/subscribe (Pub/Sub) service, a messaging service where the senders of messages are decoupled from the receivers of messages. There are several key concepts in a Pub/Sub service which are explained with the help of the following figure.

Figure showing
  different components of a Pub/Sub service and how they connect to each
  other.

Figure 1 Two publisher clients send two different messages to a common Pub/Sub topic.

The following are the components of a Pub/Sub service:

The following procedure discusses the workflow of the Pub/Sub service:

  1. Two publisher applications, Publisher 1 and Publisher 2, send messages to a single Pub/Sub topic. Publisher 1 sends message A and Publisher 2 sends message B.
  2. The topic itself is attached to two subscriptions. These areSubscription 1 and Subscription 2.
  3. The topic is also attached to a schema.
  4. Each subscription receives a copy of A and B messages from the topic.
  5. Subscription 1 is connected to two subscriber applications,Subscriber 1 and Subscriber 2. The two subscriber applications receive a subset of the messages from the topic. In this example,Subscriber 1 receives message B while Subscriber 2 receivesmessage A from the topic.
  6. Subscription 2 is only connected to a single subscriber application called Subscriber 3. Thus, Subscriber 3 receives all the messages from the topic.

Life of a message

Assume that a single publisher client is connected to a topic. The topic has a single subscription attached to it. A single subscriber is connected to the subscription.

Figure showing
  how a message flows within Pub/Sub.

Figure 2 A message flows from a publisher client to a subscriber client through Pub/Sub.

The following steps describe how a message flows in Pub/Sub:

  1. A publisher application sends a message to a Pub/Sub topic.
  2. The message is written to storage.
  3. Along with writing the message to storage, Pub/Sub delivers the message to all the attached subscriptions of the topic.
    In this example, it's a single subscription.
  4. The subscription sends the message to an attached subscriber application.
  5. The subscriber sends an acknowledgment to Pub/Sub that they have processed the message.
    After at least one subscriber for each subscription has acknowledged the message, Pub/Sub deletes the message from storage.

Status of a message in Pub/Sub

While a message is outstanding to a subscriber, Pub/Sub doesn't try to deliver it to any other subscriber on the same subscription. The subscriber has a configurable, limited amount of time, known as theackDeadline, to acknowledge the outstanding message. After the deadline passes, the message is no longer considered outstanding, and is available for delivery again.

There can be three states for a message in a Pub/Sub service:

Choose a Pub/Sub publish and subscribe pattern

When there are multiple Pub/Sub publisher and subscriber clients, you must also choose the kind of publish and subscribe architecture that you want to set up.

Figure showing
  different publish and subscribe patterns.

Figure 3 Publisher-subscriber relationships can be many-to-one (fan-in), many-to-many (load-balanced), and one-to-many (fan-out).

Some of the supported Pub/Sub publish subscribe patterns include the following:

Choose a Pub/Sub configuration option

You can configure a Pub/Sub environment by using any one of the following options:

Your choice of a Pub/Sub configuration option depends on your use case.

If you're new to Google Cloud console and want to test Pub/Sub, then use the console or the gcloud CLI.

The high-level client library is recommended for cases where you require high throughput and low latency with minimal operational overhead and processing cost. By default, the high-level client library uses theStreamingPull API. The high-level client libraries contain prebuilt functions and classes that handle the underlying API calls for authentication, throughput and latency optimization, message formatting, and other features.

The low-level client library is an auto-generated gRPC library and comes into play when you use the service APIs directly.

The following are some best practices for using the client libraries:

How to set up Pub/Sub

Here are the top-level steps to configure Pub/Sub:

  1. Create or choose a Google Cloud project where you can set up Pub/Sub.
  2. Enable the Pub/Sub API.
  3. Get the required roles and permissions to run Pub/Sub.
  4. Create a topic.
  5. If message structure is critical, define a schema for your messages.
  6. Attach the schema to the topic.
  7. Configure a publisher client that can publish messages to the topic.
  8. If required, configure advanced publishing options such as flow control, batch messaging, and concurrency control.
  9. Choose a subscription type based on how you want to receive messages.
  10. Create a subscription for the chosen topic.
  11. Configure a subscriber client that can receive messages from the subscription.
  12. If required, configure advanced message delivery options such as exactly-once delivery, lease management, ordered delivery, and flow control.
  13. Begin publishing messages from your publisher client to the topic.
  14. Simultaneously, set up your subscriber client to receive and process these messages.

Guidelines to name a topic, subscription, schema, or snapshot

A Pub/Sub resource name uniquely identifies a Pub/Sub resource, such as a topic, subscription, schema or snapshot. The resource name must fit the following format:

projects/project-identifier/collection/ID

What's next