xref: /aosp_15_r20/external/googleapis/google/pubsub/v1beta2/README.md (revision d5c09012810ac0c9f33fe448fb6da8260d444cc9)
1*d5c09012SAndroid Build Coastguard Worker## Overview
2*d5c09012SAndroid Build Coastguard WorkerThis file describes an API for a Pub/Sub (Publish/Subscribe) system. This system
3*d5c09012SAndroid Build Coastguard Workerprovides a reliable many-to-many communication mechanism between independently
4*d5c09012SAndroid Build Coastguard Workerwritten publishers and subscribers where the publisher publishes messages to
5*d5c09012SAndroid Build Coastguard Worker*topics* and each subscriber creates a *subscription* and consumes *messages*
6*d5c09012SAndroid Build Coastguard Workerfrom it.
7*d5c09012SAndroid Build Coastguard Worker
8*d5c09012SAndroid Build Coastguard Worker1.  The Pub/Sub system maintains bindings between topics and subscriptions.
9*d5c09012SAndroid Build Coastguard Worker2.  A publisher publishes messages into a topic.
10*d5c09012SAndroid Build Coastguard Worker3.  The Pub/Sub system delivers messages from topics into attached
11*d5c09012SAndroid Build Coastguard Worker    subscriptions.
12*d5c09012SAndroid Build Coastguard Worker4.  A subscriber receives pending messages from its subscription and
13*d5c09012SAndroid Build Coastguard Worker    acknowledges each one to the Pub/Sub system.
14*d5c09012SAndroid Build Coastguard Worker5.  The Pub/Sub system removes acknowledged messages from that subscription.
15*d5c09012SAndroid Build Coastguard Worker
16*d5c09012SAndroid Build Coastguard Worker## Data Model
17*d5c09012SAndroid Build Coastguard WorkerThe data model consists of the following:
18*d5c09012SAndroid Build Coastguard Worker
19*d5c09012SAndroid Build Coastguard Worker*   **Topic**: A topic is a resource to which messages are published by
20*d5c09012SAndroid Build Coastguard Worker    publishers. Topics are named, and the name of the topic is unique within the
21*d5c09012SAndroid Build Coastguard Worker    Pub/Sub system.
22*d5c09012SAndroid Build Coastguard Worker
23*d5c09012SAndroid Build Coastguard Worker*   **Subscription**: A subscription records the subscriber's interest in a
24*d5c09012SAndroid Build Coastguard Worker    topic. The Pub/Sub system maintains those messages which still need
25*d5c09012SAndroid Build Coastguard Worker    to be delivered and acknowledged so that they can retried as needed.
26*d5c09012SAndroid Build Coastguard Worker    The set of messages that have not been acknowledged is called the
27*d5c09012SAndroid Build Coastguard Worker    subscription backlog.
28*d5c09012SAndroid Build Coastguard Worker
29*d5c09012SAndroid Build Coastguard Worker*   **Message**: A message is a unit of data that flows in the system. It
30*d5c09012SAndroid Build Coastguard Worker    contains opaque data from the publisher along with its *attributes*.
31*d5c09012SAndroid Build Coastguard Worker
32*d5c09012SAndroid Build Coastguard Worker*   **Message Attributes** (optional): A set of opaque key-value pairs assigned
33*d5c09012SAndroid Build Coastguard Worker    by the publisher to a message. Attributes are delivered unmodified to
34*d5c09012SAndroid Build Coastguard Worker    subscribers together with the message data, if there's any.
35*d5c09012SAndroid Build Coastguard Worker
36*d5c09012SAndroid Build Coastguard Worker## Publisher Flow
37*d5c09012SAndroid Build Coastguard WorkerA publisher publishes messages to the topic using the `Publish` call:
38*d5c09012SAndroid Build Coastguard Worker
39*d5c09012SAndroid Build Coastguard Worker```data
40*d5c09012SAndroid Build Coastguard WorkerPubsubMessage message;
41*d5c09012SAndroid Build Coastguard Workermessage.set_data("....");
42*d5c09012SAndroid Build Coastguard Workermessage.attributes.put("key1", "value1");
43*d5c09012SAndroid Build Coastguard WorkerPublishRequest request;
44*d5c09012SAndroid Build Coastguard Workerrequest.set_topic("topicName");
45*d5c09012SAndroid Build Coastguard Workerrequest.add_message(message);
46*d5c09012SAndroid Build Coastguard WorkerPublisher.Publish(request);
47*d5c09012SAndroid Build Coastguard Worker```
48*d5c09012SAndroid Build Coastguard Worker
49*d5c09012SAndroid Build Coastguard Worker## Subscriber Flow
50*d5c09012SAndroid Build Coastguard WorkerThe subscriber part of the API is richer than the publisher part and has a
51*d5c09012SAndroid Build Coastguard Workernumber of concepts for subscription creation and use:
52*d5c09012SAndroid Build Coastguard Worker
53*d5c09012SAndroid Build Coastguard Worker1.  A subscriber (user or process) creates a subscription using the
54*d5c09012SAndroid Build Coastguard Worker    `CreateSubscription` call.
55*d5c09012SAndroid Build Coastguard Worker
56*d5c09012SAndroid Build Coastguard Worker2.  A subscriber receives messages in one of two ways: via pull or push.
57*d5c09012SAndroid Build Coastguard Worker
58*d5c09012SAndroid Build Coastguard Worker  *   To receive messages via pull, a subscriber calls the `Pull` method on the
59*d5c09012SAndroid Build Coastguard Worker      `Subscriber` to get messages from the subscription. For each individual
60*d5c09012SAndroid Build Coastguard Worker      message, the subscriber may use the `ack_id` received in the
61*d5c09012SAndroid Build Coastguard Worker      `PullResponse` to `Acknowledge` the message, or modify the *ack deadline*
62*d5c09012SAndroid Build Coastguard Worker      with `ModifyAckDeadline`. See the `Subscription.ack_deadline_seconds`
63*d5c09012SAndroid Build Coastguard Worker      field documentation for details on the ack deadline behavior. Messages
64*d5c09012SAndroid Build Coastguard Worker      must be acknowledged or they will be redelivered in a future `Pull` call.
65*d5c09012SAndroid Build Coastguard Worker
66*d5c09012SAndroid Build Coastguard Worker      **Note:** Messages may be consumed in parallel by multiple processes
67*d5c09012SAndroid Build Coastguard Worker      making `Pull` calls to the same subscription; this will result in the set
68*d5c09012SAndroid Build Coastguard Worker      of messages from the subscription being split among the processes, each
69*d5c09012SAndroid Build Coastguard Worker      process receiving a subset of the messages.
70*d5c09012SAndroid Build Coastguard Worker
71*d5c09012SAndroid Build Coastguard Worker  *   To receive messages via push, the `PushConfig` field must be specified in
72*d5c09012SAndroid Build Coastguard Worker      the `Subscription` parameter when creating a subscription, or set with
73*d5c09012SAndroid Build Coastguard Worker      `ModifyPushConfig`. The PushConfig specifies an endpoint at which the
74*d5c09012SAndroid Build Coastguard Worker      subscriber exposes the `PushEndpointService` or some other handler,
75*d5c09012SAndroid Build Coastguard Worker      depending on the endpoint. Messages are received via the
76*d5c09012SAndroid Build Coastguard Worker      `ProcessPushMessage` method. The push subscriber responds to the method
77*d5c09012SAndroid Build Coastguard Worker      with a result code that indicates one of three things: `Acknowledge` (the
78*d5c09012SAndroid Build Coastguard Worker      message has been successfully processed and the Pub/Sub system may delete
79*d5c09012SAndroid Build Coastguard Worker      it), `Nack` (the message has been rejected and the Pub/Sub system should
80*d5c09012SAndroid Build Coastguard Worker      resend it at a later time).
81*d5c09012SAndroid Build Coastguard Worker
82*d5c09012SAndroid Build Coastguard Worker      **Note:** The endpoint may be a load balancer for better scalability, so
83*d5c09012SAndroid Build Coastguard Worker      that multiple processes may handle the message processing load.
84*d5c09012SAndroid Build Coastguard Worker
85*d5c09012SAndroid Build Coastguard WorkerSubscription creation:
86*d5c09012SAndroid Build Coastguard Worker
87*d5c09012SAndroid Build Coastguard Worker```data
88*d5c09012SAndroid Build Coastguard WorkerSubscription subscription;
89*d5c09012SAndroid Build Coastguard Workersubscription.set_topic("topicName");
90*d5c09012SAndroid Build Coastguard Workersubscription.set_name("subscriptionName");
91*d5c09012SAndroid Build Coastguard Workersubscription.push_config().set_push_endpoint("machinename:8888");
92*d5c09012SAndroid Build Coastguard WorkerSubscriber.CreateSubscription(subscription);
93*d5c09012SAndroid Build Coastguard Worker```
94*d5c09012SAndroid Build Coastguard Worker
95*d5c09012SAndroid Build Coastguard WorkerConsuming messages via pull:
96*d5c09012SAndroid Build Coastguard Worker
97*d5c09012SAndroid Build Coastguard Worker```data
98*d5c09012SAndroid Build Coastguard Worker// The subscription must be created without setting the push_config field.
99*d5c09012SAndroid Build Coastguard Worker
100*d5c09012SAndroid Build Coastguard WorkerPullRequest pull_request;
101*d5c09012SAndroid Build Coastguard Workerpull_request.set_subscription("subscriptionName");
102*d5c09012SAndroid Build Coastguard Workerpull_request.set_return_immediately(false);
103*d5c09012SAndroid Build Coastguard Workerpull_request.set_max_messages(10);
104*d5c09012SAndroid Build Coastguard Workerwhile (true) {
105*d5c09012SAndroid Build Coastguard Worker  PullResponse pull_response;
106*d5c09012SAndroid Build Coastguard Worker  AcknowledgeRequest ack_request;
107*d5c09012SAndroid Build Coastguard Worker  ackRequest.set_subscription("subscriptionName");
108*d5c09012SAndroid Build Coastguard Worker  if (Subscriber.Pull(pull_request, pull_response) == OK) {
109*d5c09012SAndroid Build Coastguard Worker    for (ReceivedMessage received in pull_response.received_messages()) {
110*d5c09012SAndroid Build Coastguard Worker      Process(received.message().data());
111*d5c09012SAndroid Build Coastguard Worker      ackRequest.add_ack_id(received.ack_id());
112*d5c09012SAndroid Build Coastguard Worker    }
113*d5c09012SAndroid Build Coastguard Worker  }
114*d5c09012SAndroid Build Coastguard Worker  if (ackRequest.ack_ids().size() > 0) {
115*d5c09012SAndroid Build Coastguard Worker    Subscriber.Acknowledge(ack_request);
116*d5c09012SAndroid Build Coastguard Worker  }
117*d5c09012SAndroid Build Coastguard Worker}
118*d5c09012SAndroid Build Coastguard Worker```
119*d5c09012SAndroid Build Coastguard Worker
120*d5c09012SAndroid Build Coastguard Worker## Reliability Semantics
121*d5c09012SAndroid Build Coastguard WorkerWhen a subscriber successfully creates a subscription using
122*d5c09012SAndroid Build Coastguard Worker`Subscriber.CreateSubscription`, it establishes a "subscription point" for
123*d5c09012SAndroid Build Coastguard Workerthat subscription, no later than the time that `Subscriber.CreateSubscription`
124*d5c09012SAndroid Build Coastguard Workerreturns. The subscriber is guaranteed to receive any message published after
125*d5c09012SAndroid Build Coastguard Workerthis subscription point. Note that messages published before the subscription
126*d5c09012SAndroid Build Coastguard Workerpoint may or may not be delivered.
127*d5c09012SAndroid Build Coastguard Worker
128*d5c09012SAndroid Build Coastguard WorkerMessages are not delivered in any particular order by the Pub/Sub system.
129*d5c09012SAndroid Build Coastguard WorkerFurthermore, the system guarantees *at-least-once* delivery of each message
130*d5c09012SAndroid Build Coastguard Workeruntil acknowledged.
131*d5c09012SAndroid Build Coastguard Worker
132*d5c09012SAndroid Build Coastguard Worker## Deletion
133*d5c09012SAndroid Build Coastguard WorkerBoth topics and subscriptions may be deleted.
134*d5c09012SAndroid Build Coastguard Worker
135*d5c09012SAndroid Build Coastguard WorkerWhen a subscription is deleted, all messages are immediately dropped. If it
136*d5c09012SAndroid Build Coastguard Workeris a pull subscriber, future pull requests will return NOT_FOUND.
137