How to reduce connection latency with AWS SQS?

2019-08-10 16:37发布

问题:

When connecting to AWS SQS using AWS SDK, there seems to be a noticeable delay.

It is not so important when starting up a service to consume messages since after a 3-7 second delay on the first connection, the messages start flowing at a good speed -

BUT, when publishing messages it is a big problem. For example a user web request takes a few extra seconds to complete because of the connection to AWS is waiting to publish the message. This defeats the purpose of sending a message out in order to defer the wait time to a background job.

Is this a problem fixed with DNS? Networking? Or is it an AWS SQS setting? My web app is not in AWS network not sure if thats an issue.

Simple publishing a message code:

$aws = \Aws\Common\Aws::factory(/* array with connection settings */);
$client = $aws->get('Sqs');
$queue = $client->getQueueUrl(['QueueName' => $queue]);

// This takes 3 - 5 seconds every time its called.
$res = $this->client->sendMessage([
    'QueueUrl' => $queue['QueueUrl'],
    'MessageBody' => json_encode($request)
 ]);

回答1:

SQS has shown very low latency during our usage. However, our logic runs on EC2 instances.

Most likely, there is significant latency between your servers and SQS. Either use SQS in a region physically closer to your servers or move your application's logic onto EC2 or Lambda.

I'd recommend writing a simple test application before you do a migration to rule out issues in your business logic.



回答2:

One trick that has helped me in reducing the latency in SQS is to use the Queue URL directly instead of building it from the API. Also using http instead of https would significantly bring down the latency. For me it was nearly a 20 ms impact .

Of course you might tradeoff between security and portability but if performance is a concern than this might help.