【问题标题】:SQS Amazon Throttling request between servers服务器之间的 SQS Amazon 限制请求
【发布时间】:2014-03-27 13:31:45
【问题描述】:

我目前正在使用 Amazon SQS 将消息排队等待处理。我有基准,我只能以大约 400 条消息/秒的速度将 lib 放入队列中。我正在使用发送消息的上限到 10。

我的第二个问题是远程使用 SQS(即:我有一台服务器正在创建不在亚马逊或 EC2 实例旁边的消息)。

我的目标是增加这个瓶颈,因为我想至少做 10K 的请求。

这是因为网络延迟而失败的成就吗?或者是否有更好的 SQS 解决方案或代码调整来实现这一点。

SQS 的库也是 PHP。

编辑:添加代码

use Aws\Sqs\SqsClient;
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(Data\DataRepository $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;
        }
    }
}

【问题讨论】:

  • 也许两者兼而有之。你的代码是什么样的?您正在使用适用于 PHP 的 AWS 开发工具包吗?
  • @JeremyLindblom 是的 "aws/aws-sdk-php": "2.4.10"
  • 您是否为每条消息创建一个新队列?
  • @JeremyLindblom - 不,正如您从 getQueue 中看到的那样 - 外部代码确保没有其他实例被调用(它基本上是一个单例)
  • 我看到您的 SQS 类是单例的,但 _putToClient 方法同时调用了 createQueuesendMessage。这看起来很奇怪。

标签: php amazon-web-services amazon-sqs


【解决方案1】:

网络延迟可能会影响您,但您可能还可以采取其他一些措施来提高吞吐量。

您对代码所做的工作应该可以正常工作。但是,它绝对不是最优化的。 _putToClient() 总是进行 2 次 API 调用。只需致电SqsClient::createQueue() 即可;每次发送消息时都打这个电话似乎很奇怪。如果您只这样做一次,并存储/缓存 QueueUrl,则可以消除那里的一些延迟。

您还应该查看SDK guide for doing parallel requests。这将允许您一次发送超过 10 条消息。您可能还想阅读SDK performance guide,看看您是否可以做些什么来加快您对 SDK 的总体使用速度。

我还会发帖到SQS forum,看看 SQS 工程师是否可以指出任何特定于 SQS 的最佳实践。

【讨论】:

  • 在“升级你的 PHP”旁边似乎有很多设置可以关闭,以便消息可以更快地传递。感谢您的意见
猜你喜欢
  • 2013-01-08
  • 1970-01-01
  • 1970-01-01
  • 2013-02-03
  • 2014-06-29
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
相关资源
最近更新 更多