我目前使用Amazon SQS对消息进行队列处理。我有一个基准,我只能以大约400条消息/秒的速度将其放入库的队列中。我正在使用发送消息的上限为10。
我的第二个问题是SQS正在被远程使用(即:我有一个服务器正在创建消息,而不是在amazon或EC2实例旁边)。
我的目标是增加这个瓶颈,因为我想至少做10K的请求。
这是由于网络延迟导致的失败成就吗?或者是否有更好的解决方案SQS或代码调整来实现这一点。
SQS的库是PHP。
EDIT: code added
use AwsSqsSqsClient;
class Sqs implements QueueInterface
{
private static $_instance = null;
private $_client = null;
protected function __construct($setting)
{
if ($this->_client === null) {
try {
$this->_client = SqsClient::factory($setting);
} catch (Exception $e) {
$this->_client = null;
}
}
}
public static function getQueue($setting)
{
if (self::$_instance === null) {
self::$_instance = new Sqs($setting);
}
return self::$_instance;
}
public function put(DataDataRepository $data, $queueName)
{
$attributes=array();
if (!$this->_client) return false;
return self::_putToClient($this->_client, $data->getData(), $queueName, $attributes);
}
/**
* Put data into the queue using a defined client.
*
* @param mixed $client The client to use.
* @param mixed $data The data to insert.
* @param string $queueName The name of the queue in which to insert.
* @param mixed $attributes Some attributes for the client (QueueAttribute::DELAY_SECONDS)
*
* @return string The job id in the queue or false if a problem happened.
*/
private static function _putToClient($client, $data, $queueName, $attributes=array())
{
try {
$result = $client->createQueue(array('QueueName' => $queueName, 'Attributes' => $attributes));
$queue_url = $result->get('QueueUrl');
$response = $client->sendMessage(array('QueueUrl' => $queue_url, 'MessageBody' => $data));
return $response->getPath('MessageId');
} catch (Exception $e) {
return false;
}
}
}
网络延迟可能会影响到您,但是您可以做一些其他的事情来获得更多的吞吐量。
你对你的代码所做的应该工作得很好。然而,它绝对不是最优化的。_putToClient()
总是调用2个API。只需要调用一次SqsClient::createQueue()
;每次发信息都要打那个电话,感觉怪怪的。如果你只这样做一次,并存储/缓存QueueUrl,你可以消除一些延迟。
您还应该查看执行并行请求的SDK指南。这将允许您一次发送10条以上的消息。您可能还需要阅读SDK性能指南,看看是否可以做些什么来加快SDK的使用速度。
我也会在SQS论坛上发帖,看看SQS工程师是否能指出任何针对SQS的最佳实践。