如何提升向AWS队列发送消息的性能

huangapple go评论70阅读模式
英文:

How to increase performance for send message to AWS Queue

问题

我正在使用Spring Boot 2.2.0.RELEASE,当我将数据发送到AmazonSqs时,会有300-500毫秒的延迟。也许是我做错了什么。我的代码如下所示:

public class MySender {
    private final QueueMessagingTemplate queueMessagingTemplate;
    private AmazonSQSAsync amazonSqsAsync;

    public MySender(AmazonSQSAsync amazonSqsAsync) {
        this.amazonSqsAsync = amazonSqsAsync;
        this.queueMessagingTemplate = new QueueMessagingTemplate(amazonSqs);
    }

    public void send(String queue, String msg) {
        ...
        Message<String> message = MessageBuilder.withPayload(msg)
                                                .setHeader("MyHeader", "val");
        long startSending = System.currentTimeMillis();
        queueMessagingTemplate.send(queue, message);
        System.out.println("Sending time: " + (System.currentTimeMillis() - startSending));
        // and this I get sending message time 300 - 500ms.
    }
}

如何减少这种延迟?

英文:

I'm using Spring boot 2.2.0.RELEASE and when I send data to AmazonSqs I have delay 300-500ms. Maybe I do something wrong. My code looks like the following:

public class MySender {
    private final QueueMessagingTemplate queueMessagingTemplate;
    private AmazonSQSAsync amazonSqsAsync;

    public MySender(AmazonSQSAsync amazonSqsAsync) {
        this.amazonSqsAsync = amazonSqsAsync;
        this.queueMessagingTemplate = new QueueMessagingTemplate(amazonSqs);
    }

    public void send(String queue, String msg) {
        ...
        Message&lt;String&gt; message = MessageBuilder.withPayload(msg)
                                                .setHeader(&quot;MyHeader&quot;, &quot;val&quot;);
        long startSending = System.currentTimeMillis();
        queueMessagingTemplate.send(queue, message);
        System.out.println(&quot;Sending time: &quot; + (System.currentTimeMillis() - startSending));
        // and this I get sending message time 300 - 500ms.
    }
}

How can I reduce this delay?

答案1

得分: 1

Amazon SQS队列可以提供非常高的吞吐量。如果您不使用FIFO队列,则消息数量没有限制。将消息从您的Spring Boot应用程序发送到SQS队列的延迟取决于诸如区域、网络带宽等其他因素。

如果您在印度的本地计算机上运行Spring Boot应用程序,而您的SQS队列区域位于欧洲,那么会存在一定的延迟。

为了提高性能,请尝试将Spring Boot应用程序部署在与您的SQS队列相同区域的EC2实例中。

您还可以尝试使用VPC终端节点连接到SQS队列,它将使用AWS私有网络发送消息,从而提供良好的性能。

我建议更好的方法是使用Lambda发送消息到SQS队列,这可以为您提供最大的性能。

还要验证您的SQS队列配置。检查设置的默认可见性超时、接收消息等待时间、传递延迟等值。

英文:

Amazon SQS queues can deliver very high throughput. If you are not using FIFO queue, then there is no limit for number of messages. The delay in sending messages to SQS queue from your spring boot application depends on number of other factors like region, network bandwidth

If you are running your spring boot application in your local machine in India and your SQS queue region is in Europe for example, then there will be a latency.

To improve performance, try deploying your spring boot application in EC2 instance in the same region of your SQS queue.

You can also try using vpc endpoint to connect to SQS queue which will use aws private network to send messages and it will give you good performance

I would suggest the better way to use lambda to send message to SQS queue which can give you maximum performance.

Also validate the configuration of your SQS queue.Check the values set for Default visibility timeout, Receive message wait time, Delivery delay

huangapple
  • 本文由 发表于 2020年8月31日 23:02:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63673298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定