google-cloud-python icon indicating copy to clipboard operation
google-cloud-python copied to clipboard

User indicates very high Pub/Sub publish average latency for gcloud-python

Open kir-titievsky opened this issue 10 years ago • 9 comments

Hey, Gcloud-python team,

I have a user who was left disappointed. From an extract: "With Google’s gcloud python library, we are seeing avg latencies around 400ms publishing tiny messages. This is unacceptable so we tried the experimental RPC library in both python and go, and see average latencies around 120ms." Do we have any idea where the discrepancy might be coming from? Glad to offer some more detail -- contact me directly @google.com if possible.

Kir

kir-titievsky avatar Sep 12 '16 18:09 kir-titievsky

We have gRPC support as well, depending on what version they are using. The latency would be from the network, not from the library, but the 400ms might not all be latency.

For example, there are blocking calls that may wait longer, though we don't use any explicit sleeps, so that would indicate any blocking is due to network latency (again, not anything to do with the library).

dhermes avatar Sep 12 '16 18:09 dhermes

@kir-google-cloud-pubsub Just sent an email as well. Cheers.

dhermes avatar Sep 12 '16 18:09 dhermes

If the reported latency is at the point of Topic.publish: note that our GAX API helper maintains API compatibility with the JSON-over-HTTP version, returning the message ID directly to the caller It therefore disables the GAX "bundling" feature. We've discussed adding another method (e.g., Topic.publish_async) which would be available only under GAX, and which would not return message IDs, but rather the GAX event object. Users publishing multiple messages simultaneously would gain lower latency (no need to wait for message delivery) in exchange for not being able to track the message IDs at point-of-call (they would need to call even.wait() at some point to get them).

tseaver avatar Sep 12 '16 18:09 tseaver

Pardon the level of my ignorance here -- don't know what GAX bundling does. You mean that this is because the Topic.publish() call is blocking? That still seems odd because the 130ms the user was able to get with gRPC in python indicates that

  1. they used a blocking implementation because an async call would not take that long to return
  2. the baseline for full response is still 270ms longer in python. That seems like a lot for things that don't happen over network.

kir-titievsky avatar Sep 12 '16 19:09 kir-titievsky

I did some benchmarking and it seems like 200ms is the average, when sending a lot of messages, though the number of messages and the duration is only about 65-70% correlated.

It may be more interesting to run this on 50 different machines instead of re-constructing a connection 50 different times, since the OS may be keeping the socket open and making future calls faster.

dhermes avatar Sep 13 '16 00:09 dhermes

Summarizing some off-line discussion with @dhermes. The user shared their python implementation:

topic = pubsub_client.topic(topic_name)
with topic.batch():
    for timestamp, value in data:
        msg = json.dumps({
            'timestamp': timestamp,
            'value': value
        })
        topic.publish(msg)

This code sends every message in a separate request, explaining the large latency. Proper batch implementation would have been:

with topic.batch() as batch:
     # redacted code
     for t, v in data: 
       batch.publish(json.dumps({'timestamp': timestamp, 'value': v}))

The actual publish call will be executed after the for t,v in data loop finishes.

This explains the 400ms latency in the python sample but not the 130ms latency for the gRPC implementation. Working on that now.

kir-titievsky avatar Sep 13 '16 16:09 kir-titievsky

explaining the large latency...explains the 400ms latency

It doesn't explain latency, but it explains why the block of code might take 400ms. Though if there were a lot of requests (i.e. a lot of entries in the data iterator), it'd take a lot more than 400ms.

dhermes avatar Sep 13 '16 16:09 dhermes

@kir-titievsky Your update, correcting the batch usage, seems fine to me. If hand-marshalling the data using json.dumps is not a hard requirement, you might consider sending timestamp as an attribute, e.g.:

with topic.batch() as batch:
     for timestamp, value in data: 
       batch.publish(value, timestamp=timestamp)

which ought to shave off more cycles.

tseaver avatar Oct 12 '16 14:10 tseaver

Hello! :-) First, a mea culpa; I have not done as good of a job at keeping up with issues as I should have. If you are getting this (I admit it) cut and paste, it is likely because your issue sat for too long.

In this case, I have been in the process of making a radical update to the PubSub library (#3637) to add significant performance improvements and a new surface, which we hope to launch soon. As such, I am clearing out issues on the old library. It is my sincere goal to do a better job of being on top of issues in the future.

As the preceding paragraph implies, I am closing this issue. If the revamped library does not solve your issue, however, please feel free to reopen.

Thanks!

lukesneeringer avatar Aug 08 '17 06:08 lukesneeringer