Search code examples
phpapacheamazon-web-servicesamazon-sqs

How to reduce connection latency with AWS SQS?


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)
 ]);

Solution

  • 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.