简体   繁体   English

Google Cloud Pub/Sub 在低消息吞吐量时触发高延迟

[英]Google Cloud Pub/Sub triggers high latency on low messages throughput

i'm running project which publishes messages to a PubSub topic and triggers background cloud function.我正在运行将消息发布到 PubSub 主题并触发后台云功能的项目。

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..我读到了大量消息时,它表现良好,但对于较少的消息,例如每秒数百甚至数十条消息,Pub/Sub 可能会产生高延迟。

Code example to publish single message:发布单条消息的代码示例:

 const {PubSub} = require('@google-cloud/pubsub');

 const pubSubClient = new PubSub();

 async function publishMessage() {
    const topicName = 'my-topic';
    const dataBuffer = Buffer.from(data);

    const messageId = await pubSubClient.topic(topicName).publish(dataBuffer);
    console.log(`Message ${messageId} published.`);
 }

 publishMessage().catch(console.error);

Code example function triggered by PubSub: PubSub 触发的代码示例函数:

exports.subscribe = async (message) => {
  const name = message.data
    ? Buffer.from(message.data, 'base64').toString()
    : 'World';

  console.log(`Hello, ${name}!`);
}

Cloud Function Environment Details:云函数环境详情:
Node: 8节点:8
google-cloud/pubsub: 1.6.0谷歌云/发布订阅:1.6.0

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).问题是当使用具有低消息吞吐量(例如,每秒 1 个请求)的 PubSub 时,它有时会遇到困难并显示令人难以置信的高延迟(高达7-9 秒或更多)。

Is there a way or workaround to make PubSub perform well each time ( 50ms or less delay) even with small amount of incoming messages?有没有办法或解决方法使 PubSub 每次都表现良好( 50 毫秒或更短的延迟),即使传入的消息量很少?

If you are always publishing to the same topic, you'd be better off keeping the object returned from pubSubClient.topic(topicName) and reusing it, whether you have a small number or large number of messages.如果你总是发布到同一个主题,你最好保留从pubSubClient.topic(topicName)返回的对象pubSubClient.topic(topicName)它,无论你有少量还是大量的消息。 If you want to minimize latency, you'll also want to set the maxMilliseconds property of the batching setting.如果您想最小化延迟,您还需要设置batching设置的maxMilliseconds属性。 By default, this is 10ms.默认情况下,这是 10 毫秒。 As you have the code now, it means that every publish waits 10ms to send a message in hopes of filling the batch.正如您现在拥有的代码一样,这意味着每个发布都等待 10 毫秒来发送消息以希望填充批次。 Given that you create a new publisher via the topic call on every publish, you are guaranteed to always wait at least 10ms.鉴于您通过每次发布时的topic调用创建一个新发布者,保证您始终等待至少 10 毫秒。 You can set it when you call topic :您可以在调用topic时设置它:

const publisher = pubSubClient.topic(topicName, {
      batching: {
        maxMessages: 100,
        maxMilliseconds: 1,
      },
    });

If after reusing the the object returned from pubSubClient.topic(topicName) and changing maxMilliseconds you are still experiencing such a delay, then you should reach out to Google Cloud Support so they can look at the specific project and topic you are using as that kind of latency is definitely not expected.如果在重用从pubSubClient.topic(topicName)返回的对象并更改maxMilliseconds您仍然遇到这样的延迟,那么您应该联系Google Cloud 支持,以便他们可以查看您正在使用的特定项目和主题延迟绝对不是预期的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM