You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 21, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Oct 21, 2026. It is now read-only.
Google Cloud Pub/Sub triggers high latency on low messages throughput #925
api: pubsubIssues related to the googleapis/nodejs-pubsub API.priority: p3Desirable enhancement or fix. May not be included in next release.type: questionRequest for information or clarification. Not an issue.
i'm running project which publishes messages to a PubSub topic and triggers background cloud function.
I read that with high volumes of messages, it performs well, but for lesser amounts like hundreds or even tens of messages per second, Pub/Sub may yield high latencies.
The problem is when using PubSub with low throughput of messages (for example, 1 request per second) it struggles sometimes and shows incredibly high latency (up to 7-9s or more).
Is there a way or workaround to make PubSub perform well each time (50ms or less delay) even with small amount of incoming messages?
Thanks!
Reacted by Vincent Costel, Tommy D. Rossi, Georgi Christov and Piyush Aggarwal
@andryichenko Sorry for the late reply on this, but could it be that the publisher piece you showed above is queueing the messages for batch send? You might try decreasing the max batching timeout:
The default is already low (100), but it's possible something is getting set incorrectly. It's also possible that it's not properly obeying that number in your case, in which case I need to do some more debugging.
The other possibility is that it's getting hung up in the machinery somewhere between your client and your GCF function executing. I can pass it off to the right people in that case, but we should eliminate the client library first.
@feywind Thanks for the reply!
I already tried to decrease the max batching timeout, but it didn't really help. (It seemed to me that messages are going faster but still it struggles).
Also as can be found in this guide, PubSub tries to gather multiple messages before delivering it. In other words, it tries to deliver many messages at once. In this specific case to achieve a more realistic real time scenario, should be specified a batch size of 1, which would cause PubSub to delivery every message separately.
So i also added maxMessages property to batching object, like so:
But it seemed also not really helpful.
After some research i come to conclusion that it may be cloud functions causing delays on cold start. So as a workaround i tried to make a simple node.js server that pinging cloud functions via pub/sub so they remain heat. And it shows much better results now.
I'm still trying to find good solution for this though.
We also face the same problem: when we send 1-2 messages per minute the latency is around 1200 ms but once we push more messages it becomes faster and faster (around 10ms). It looks like there is some cache mechanism for pushing the messages.
I'm not as knowledgable about what happens after it leaves the Pub/Sub library, so it's possible something down in gax/gRPC is doing its own buffering. I can ask the maintainers of those libraries if there's anything possibly there. But as for the Pub/Sub library itself, it should be obeying that batching configuration and not pausing. I will peek at that and see if there's possibly a bug there, that it's not using the right values.
@alexander-fenster Sorry to ping you again :D but could there be something introducing latency and doing its own batching of calls at the gax level? If setting the batch size to 1 for the Pub/Sub library doesn't reduce the latency, it seems like it might be something at a lower level.
We are experiencing a similar issue: high latency (5 ~ 10 seconds inconsistently) on low messages throughput.
I don't think it's related to the message producer, but maybe the pubsub server itself and here is why:
We keep tracking the publish_time on message and from the official google doc we know that this publish_time is the time pubsub server receives the message, so it doesn't matter what you set up from the message producer side. And the time between the publish_time and the time our client/message-consumer receives the message can be 5 ~ 10 seconds when the traffic throughput is low, especially in the scenario that a message sent to pubsub after a long time of no message sent.
I have been looking for some SLA of pubsub to guarantee that "queue time" but could not find it.
@kamalaboulhosn Since there's a question here about the server side... Is it possible that the service is doing some sort of batching or delaying to queue up more messages?
@ChrisWestcottUK The release notes for the server only track significant API/region availability/feature availability changes, not every server change that is made. There is no public tracking for all of these such changes. For tracking issues if they affect your projects, it is best to put in a support request so support engineers can follow up. This particular incident should now be resolved.
Doesn't that show that it's important to choose good idempotency keys and not follow the suggestions in the documentation? Or is there something I'm missing?
I'm seeing this issue as well. I also faced it with Cloud Functions but I took my cloud function code and modified it a bit so I could easily trigger it locally with node testProblem.js. So it is just like your typical node command line app. This still causes a 2-5 second latency for publishing a message to a topic. At the same time a different app involved running in GKE uses the Java version of pubsub library (via Clojure) and that works lightning fast, no delay whatsoever. The same lightning fast performance is retained when running that app locally so it is not related to my personal internet connectivity or similar wonky stuff.
To me this seems like the batching disabling doesn't work properly. Batching configurations are respected, if publishing less messages than maxMessages then the latency is very close to maxMilliseconds as one would expect. If however maxMessages is set to 1 then this weird latency occurs. If sending two messages with maxMessages set to 2 they both finish in identically the same amount of time, which again is to be expected since they are batched. If sending two messages with maxMessages set to 1 they both finish about twice as fast! This does NOT make any sense.
Since Java pubsub doesn't suffer from problems such as this at all I'm leaning towards that the whole batch scheduling logic in this library is somehow broken. I'll look into this for a while still but if I can't find the issue really soon we have to rewrite our Cloud Functions with a different language to get acceptable performance since this issue causes a user facing performance problem.
Going back to the original issue, I think the problem might be in the way the topic is being instantiated. The topic is being recreated for every publish, which means the topic is not being reused. It also means that the connection to the server has to be renegotiated every time. It would be better to do this:
I’m seeing a similar issue over here. My setup is similar to the OP - Firebase cloud function triggers individual PubSub topic messages. I’m seeing latency in minutes rather than seconds.
Interestingly the topic().publish() method seems to be taking a long time based in the logs.
Ive tried setting max messages but that didn’t seem to help.
batching: {
maxMessages: 1
}
Anything else I can try or information to share that would help debug this?
I’m seeing latency in minutes rather than seconds.
Have you tried debugging it the same way I did in one of the comments above? It could be caused by throttling on a function because of an async wrapper, or any async code that you aren't awaiting properly.
The key is to log when the function returns - does it return sooner than the publish() call?
@killthekitten Great tip thanks! Turned out that was the problem in my case - I was not waiting for the topic().publish() to complete before terminating the cloud function. The fix was to add an .await() on the publish Promise and now the messages seem to be delivered much faster 👍
This issue has covered a lot of different causes and investigations that are not entirely related. The original issue looks to have been an inefficiency in the way the user code was written. Other issues included transient server-side problem as well as other miscellaneous issues. Going forward, if anyone is still experiencing issues, please enter a support case. Thanks!
api: pubsubIssues related to the googleapis/nodejs-pubsub API.priority: p3Desirable enhancement or fix. May not be included in next release.type: questionRequest for information or clarification. Not an issue.
Activity
feywind commentedon May 5, 2020
@andryichenko Sorry for the late reply on this, but could it be that the publisher piece you showed above is queueing the messages for batch send? You might try decreasing the max batching timeout:
The default is already low (100), but it's possible something is getting set incorrectly. It's also possible that it's not properly obeying that number in your case, in which case I need to do some more debugging.
The other possibility is that it's getting hung up in the machinery somewhere between your client and your GCF function executing. I can pass it off to the right people in that case, but we should eliminate the client library first.
andryichenko commentedon May 6, 2020
@feywind Thanks for the reply!
I already tried to decrease the max batching timeout, but it didn't really help. (It seemed to me that messages are going faster but still it struggles).
Also as can be found in this guide, PubSub tries to gather multiple messages before delivering it. In other words, it tries to deliver many messages at once. In this specific case to achieve a more realistic real time scenario, should be specified a batch size of 1, which would cause PubSub to delivery every message separately.
So i also added
maxMessagesproperty tobatchingobject, like so:But it seemed also not really helpful.
After some research i come to conclusion that it may be cloud functions causing delays on cold start. So as a workaround i tried to make a simple node.js server that pinging cloud functions via pub/sub so they remain heat. And it shows much better results now.
I'm still trying to find good solution for this though.
vyacheslav-kara commentedon May 13, 2020
We also face the same problem: when we send 1-2 messages per minute the latency is around 1200 ms but once we push more messages it becomes faster and faster (around 10ms). It looks like there is some cache mechanism for pushing the messages.
feywind commentedon May 19, 2020
I'm not as knowledgable about what happens after it leaves the Pub/Sub library, so it's possible something down in gax/gRPC is doing its own buffering. I can ask the maintainers of those libraries if there's anything possibly there. But as for the Pub/Sub library itself, it should be obeying that batching configuration and not pausing. I will peek at that and see if there's possibly a bug there, that it's not using the right values.
andryichenko commentedon May 22, 2020
@feywind did you find any solution?
andresmargalef commentedon Aug 12, 2020
@feywind did you find any solution?
feywind commentedon Aug 25, 2020
I missed that there were two issues potentially related to this: #1087
Do you know if 2.5.0 still has the issue?
dozie commentedon Sep 11, 2020
Seems like this has been merged and hasn't solved the problem of latency.
feywind commentedon Sep 18, 2020
@dozie Thanks for the update!
@alexander-fenster Sorry to ping you again :D but could there be something introducing latency and doing its own batching of calls at the gax level? If setting the batch size to 1 for the Pub/Sub library doesn't reduce the latency, it seems like it might be something at a lower level.
andryichenko commentedon Oct 1, 2020
@feywind I can confirm that batch size settings is set to 1 message, but latency didn't reduce.
xiaowei-routific commentedon Nov 6, 2020
We are experiencing a similar issue: high latency (5 ~ 10 seconds inconsistently) on low messages throughput.
I don't think it's related to the message producer, but maybe the pubsub server itself and here is why:
We keep tracking the
publish_timeon message and from the official google doc we know that thispublish_timeis the time pubsub server receives the message, so it doesn't matter what you set up from the message producer side. And the time between thepublish_timeand the time our client/message-consumer receives the message can be 5 ~ 10 seconds when the traffic throughput is low, especially in the scenario that a message sent to pubsub after a long time of no message sent.I have been looking for some SLA of pubsub to guarantee that "queue time" but could not find it.
feywind commentedon Nov 20, 2020
@kamalaboulhosn Since there's a question here about the server side... Is it possible that the service is doing some sort of batching or delaying to queue up more messages?
10 remaining items
kamalaboulhosn commentedon Feb 15, 2022
@ChrisWestcottUK The release notes for the server only track significant API/region availability/feature availability changes, not every server change that is made. There is no public tracking for all of these such changes. For tracking issues if they affect your projects, it is best to put in a support request so support engineers can follow up. This particular incident should now be resolved.
ChrisWestcottUK commentedon Feb 16, 2022
One thing that appeared to have also occurred at this time was messages published multiple times (we're using an
orderingKey):eventIdas an idempotency key: https://cloud.google.com/blog/products/serverless/cloud-functions-pro-tips-retries-and-idempotency-in-actioneventIdis the message id: https://cloud.google.com/functions/docs/calling/pubsub#event_structureDoesn't that show that it's important to choose good idempotency keys and not follow the suggestions in the documentation? Or is there something I'm missing?
feywind commentedon Aug 17, 2022
Linked to the meta-issue about transport problems: b/242894947
empperi commentedon Oct 6, 2022
I'm seeing this issue as well. I also faced it with Cloud Functions but I took my cloud function code and modified it a bit so I could easily trigger it locally with
node testProblem.js. So it is just like your typical node command line app. This still causes a 2-5 second latency for publishing a message to a topic. At the same time a different app involved running in GKE uses the Java version of pubsub library (via Clojure) and that works lightning fast, no delay whatsoever. The same lightning fast performance is retained when running that app locally so it is not related to my personal internet connectivity or similar wonky stuff.To me this seems like the batching disabling doesn't work properly. Batching configurations are respected, if publishing less messages than
maxMessagesthen the latency is very close tomaxMillisecondsas one would expect. If howevermaxMessagesis set to 1 then this weird latency occurs. If sending two messages withmaxMessagesset to2they both finish in identically the same amount of time, which again is to be expected since they are batched. If sending two messages withmaxMessagesset to1they both finish about twice as fast! This does NOT make any sense.Since Java pubsub doesn't suffer from problems such as this at all I'm leaning towards that the whole batch scheduling logic in this library is somehow broken. I'll look into this for a while still but if I can't find the issue really soon we have to rewrite our Cloud Functions with a different language to get acceptable performance since this issue causes a user facing performance problem.
kamalaboulhosn commentedon Jan 30, 2023
Going back to the original issue, I think the problem might be in the way the topic is being instantiated. The topic is being recreated for every publish, which means the topic is not being reused. It also means that the connection to the server has to be renegotiated every time. It would be better to do this:
If the topic name is not known before the publish and can change, then you should keep a map from topic name to
topicobject and reuse it.gchristov commentedon Feb 22, 2023
I’m seeing a similar issue over here. My setup is similar to the OP - Firebase cloud function triggers individual PubSub topic messages. I’m seeing latency in minutes rather than seconds.
Interestingly the
topic().publish()method seems to be taking a long time based in the logs.Ive tried setting max messages but that didn’t seem to help.
Anything else I can try or information to share that would help debug this?
killthekitten commentedon Feb 22, 2023
Have you tried debugging it the same way I did in one of the comments above? It could be caused by throttling on a function because of an async wrapper, or any async code that you aren't awaiting properly.
The key is to log when the function returns - does it return sooner than the publish() call?
gchristov commentedon Feb 23, 2023
@killthekitten Great tip thanks! Turned out that was the problem in my case - I was not waiting for the
topic().publish()to complete before terminating the cloud function. The fix was to add an.await()on the publishPromiseand now the messages seem to be delivered much faster 👍feywind commentedon Mar 7, 2023
Hi all, I just made some updates to the batch flushing logic for publishing, and that's in 3.4.0. I'd be interested to hear if it helps this.
kamalaboulhosn commentedon Jun 20, 2023
This issue has covered a lot of different causes and investigations that are not entirely related. The original issue looks to have been an inefficiency in the way the user code was written. Other issues included transient server-side problem as well as other miscellaneous issues. Going forward, if anyone is still experiencing issues, please enter a support case. Thanks!
fix(build): migrate to using main branch (googleapis#925)
chore: release 3.6.0 (googleapis#929)