Skip to content

Commit 71df27f

Browse files
authored
docs(pubsub/v2): revamp package docs (#14317)
This PR includes changes to some of our recommendations for how to optimize Pub/Sub performance. Also use links rather than plaint-text URLs. Fixes #13759
1 parent a6efdbf commit 71df27f

1 file changed

Lines changed: 48 additions & 45 deletions

File tree

pubsub/v2/doc.go

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ messages, hiding the details of the underlying server RPCs.
1818
Pub/Sub is a many-to-many, asynchronous messaging system that decouples senders
1919
and receivers.
2020
21-
If you are migrating from the v1 library, please read over the migration guide:
22-
https://github.com/googleapis/google-cloud-go/blob/main/pubsub/MIGRATING.md
21+
If you are migrating from the v1 library, please read over the [migration guide].
2322
24-
More information about Pub/Sub is available at
25-
https://cloud.google.com/pubsub/docs.
23+
More information about Pub/Sub is available at the [product documentation page].
2624
27-
See https://godoc.org/cloud.google.com/go for authentication, timeouts,
25+
See the [main Google Cloud Go package] for authentication, timeouts,
2826
connection pooling and similar aspects of this package.
2927
3028
# Publishing
@@ -107,8 +105,8 @@ been called unless exactly once delivery is enabled. Applications should be awar
107105
of these deliveries.
108106
109107
Note: This uses pubsub's streaming pull feature. This feature has properties that
110-
may be surprising. Please refer to https://cloud.google.com/pubsub/docs/pull#streamingpull
111-
for more details on how streaming pull behaves.
108+
may be surprising. Please refer to the [Streaming Pull API] for more details on
109+
how streaming pull behaves.
112110
113111
# Emulator
114112
@@ -167,32 +165,16 @@ AckDeadline for the MaxExtension value.
167165
168166
# Fine Tuning PubSub Receive Performance
169167
170-
As the PubSub client receives messages from the PubSub server, it puts them into
171-
the callback function passed to Receive. The user must Ack or Nack a message
172-
in this function. Each invocation by the client of the passed-in callback occurs
173-
in a goroutine; that is, messages are processed concurrently.
168+
This section describes how to adjust [ReceiveSettings] for best performance.
174169
175-
The buffer holds a maximum of MaxOutstandingMessages messages or MaxOutstandingBytes
176-
bytes, and the client stops requesting more messages from the server whenever the buffer
177-
is full. Messages in the buffer have an ack deadline; that is, the server keeps a
178-
deadline for each outstanding message. When that deadline expires, the server considers
179-
the message lost and redelivers the message. Each message in the buffer automatically has
180-
its deadline periodically extended. If a message is held beyond its deadline,
181-
for example if your program hangs, the message will be redelivered.
182-
183-
This medium post describes tuning Pub/Sub performance in more detail
184-
https://medium.com/google-cloud/pub-sub-flow-control-batching-9ba9a75bce3b
185-
186-
- Subscription.ReceiveSettings.MaxExtension
170+
Subscriber.ReceiveSettings.MaxExtension
187171
188172
This is the maximum amount of time that the client will extend a message's deadline.
189173
This value should be set to the maximum expected processing time, plus some
190-
buffer. It is fairly safe to set it quite high; the only downside is that it will take
191-
longer to recover from hanging programs. The higher the extension allowed, the longer
192-
it takes before the server considers messages lost and re-sends them to some
193-
other, healthy instance of your application.
174+
buffer. The higher the extension allowed, the longer it takes before the server considers
175+
messages lost and re-sends them to some other, healthy instance of your application.
194176
195-
- Subscription.ReceiveSettings.MaxDurationPerAckExtension
177+
Subscriber.ReceiveSettings.MaxDurationPerAckExtension
196178
197179
This is the maximum amount of time to extend each message's deadline per
198180
ModifyAckDeadline RPC. Normally, the deadline is determined by the 99th percentile
@@ -203,7 +185,7 @@ of time to extend a message's deadline on a per-RPC basis, you can decrease the
203185
of time before message redelivery when errors occur. However, the downside is that more
204186
ModifyAckDeadline RPCs will be sent.
205187
206-
- Subscription.ReceiveSettings.MinDurationPerAckExtension
188+
Subscriber.ReceiveSettings.MinDurationPerAckExtension
207189
208190
This is the minimum amount of time to extend each message's deadline per
209191
ModifyAckDeadline RPC. This is the complement setting of MaxDurationPerAckExtension and
@@ -213,7 +195,7 @@ low, it may be better to issue fewer ModifyAckDeadline RPCs rather than every
213195
effectively removes the automatic derivation of deadlines and fixes it to the value
214196
you wish to extend your messages' deadlines by each time.
215197
216-
- Subscription.ReceiveSettings.MaxOutstandingMessages
198+
Subscriber.ReceiveSettings.MaxOutstandingMessages
217199
218200
This is the maximum number of messages that are to be processed by the callback
219201
function at a time. Once this limit is reached, the client waits for messages
@@ -223,7 +205,7 @@ This value is set by default to a fairly conservatively low number. We strongly
223205
encourage setting this number as high as memory allows, since a low setting will
224206
artificially rate limit reception. Setting this value to -1 causes it to be unbounded.
225207
226-
- Subscription.ReceiveSettings.MaxOutstandingBytes
208+
Subscriber.ReceiveSettings.MaxOutstandingBytes
227209
228210
This is the maximum amount of bytes (message size) that are to be processed by
229211
the callback function at a time. Once this limit is reached, the client waits
@@ -237,30 +219,51 @@ For example, if the client sets MaxOutstandingBytes to 50 KiB, but receives
237219
a batch of messages totaling 100 KiB, there will be a temporary overflow of
238220
message byte size until messages are acked.
239221
240-
Similar to MaxOutstandingMessages, we recommend setting this higher to maximize
222+
Similar to MaxOutstandingMessages, you can set this higher to maximize
241223
processing throughput. Setting this value to -1 causes it to be unbounded.
242224
243-
- Subscription.ReceiveSettings.NumGoroutines
225+
Subscriber.ReceiveSettings.NumGoroutines
226+
227+
This is the number of goroutines spawned to receive messages from the Pubsub server.
228+
Each goroutine opens a StreamingPull stream, so this also directly sets the number of
229+
open StreamingPull streams.
230+
231+
According to the [Resource Limits] page, each stream can handle about 10 MB/s of messages.
232+
Leaving this value as the default would be good for most use cases.
244233
245-
This is the number of goroutines spawned to receive messages from the Pubsub server,
246-
where each goroutine opens a StreamingPull stream. This setting affects the rate of
247-
message intake from server to local buffer.
234+
If increasing this value to be greater than 1, please set `EnablePerStreamFlowControl` to true.
235+
This helps align the server-side flow control with what is available locally. For more
236+
explanation of this issue, see [this issue].
248237
249-
Setting this value to 1 is sufficient for many workloads. Each stream can handle about
250-
10 MB/s of messages, so if your throughput is under this, set NumGoroutines=1.
251-
Reducing the number of streams can improve the performance by decreasing overhead.
252-
Currently, there is an issue where setting NumGoroutines greater than 1 results in poor
253-
behavior interacting with flow control. Since each StreamingPull stream has its own flow
254-
control, the server-side flow control will not match what is available locally.
238+
Going above 100 streams can lead to poor behavior, such as acks/modacks not succeeding in a reasonable
239+
amount of time and resulting in high message expiration rates. In these cases,
240+
increase the number of subscriber client applications rather than increasing this value.
255241
256-
Going above 100 streams can lead to increasingly poor behavior, such as acks/modacks not
257-
succeeding in a reasonable amount of time, leading to message expiration. In these cases,
258-
we recommend horizontally scaling by increasing the number of subscriber client applications.
242+
By default, the number of connections in the gRPC conn pool is min(4,GOMAXPROCS). Each connection supports
243+
up to 100 streams. Thus, if you have 4 or more CPU cores, the default setting allows a maximum of 400 streams
244+
which is already excessive for most use cases.
245+
If you want to change the limits on the number of streams, you can change the number of connections
246+
in the gRPC connection pool as shown below:
247+
248+
opts := []option.ClientOption{
249+
option.WithGRPCConnectionPool(2),
250+
}
251+
client, err := pubsub.NewClient(ctx, projID, opts...)
252+
253+
This [medium post] describes tuning Pub/Sub performance in more detail.
259254
260255
# General tips
261256
262257
Each application should use a single PubSub client instead of creating many.
263258
In addition, when publishing to a single topic, a publisher should be instantiated
264259
once and reused to take advantage of flow control and batching capabilities.
260+
261+
[product documentation page]: https://cloud.google.com/pubsub/docs.
262+
[Streaming Pull API]: https://docs.cloud.google.com/pubsub/docs/pull#streamingpull-api
263+
[main Google Cloud Go package]: https://pkg.go.dev/cloud.google.com/go
264+
[migration guide]: https://github.com/googleapis/google-cloud-go/blob/main/pubsub/MIGRATING.md
265+
[medium post]: https://medium.com/google-cloud/pub-sub-flow-control-batching-9ba9a75bce3b
266+
[this issue]: https://issuetracker.google.com/352592079
267+
[Resource Limits]: https://docs.cloud.google.com/pubsub/quotas#resource_limits
265268
*/
266269
package pubsub // import "cloud.google.com/go/pubsub/v2"

0 commit comments

Comments
 (0)