如何防止Laravel中的DynamoDb异常ProvisionedThroughputExceededException

时间:2019-02-12 16:14:40

标签: laravel amazon-dynamodb

我将在很长一段时间内更新一定数量的记录,但是我不确定记录的生成时间。有时,当同时产生许多记录时,我收到一条错误日志条目,说我打了ProvisionedThroughputExceededException

我想防止此异常的发生,或者至少能够捕获该异常(然后重新抛出该异常,以便不改变逻辑),但是我得到的只是以下错误日志:

[2019-02-12 15:50:48] local.ERROR: Error executing "UpdateItem" on "https://dynamodb.eu-central-1.amazonaws.com"; AWS HTTP error: Client error: `POST https://dynamodb.eu-central-1.amazonaws.com` resulted in a `400 Bad Request` response:

日志继续,我们可以找到更多细节:

ProvisionedThroughputExceededException (client): The level of configured provisioned throughput for the table was exceeded. Consider increasing your provisioning level with the UpdateTable API. -
{
    "__type": "com.amazonaws.dynamodb.v20120810#ProvisionedThroughputExceededException",
    "message": "The level of configured provisioned throughput for the table was exceeded. Consider increasing your provisioning level with the UpdateTable API."
}
{"exception":"[object] (Aws\\DynamoDb\\Exception\\DynamoDbException(code: 0): Error executing \"UpdateItem\" on \"https://dynamodb.eu-central-1.amazonaws.com\"; AWS HTTP error: Client error: `POST https://dynamodb.eu-central-1.amazonaws.com` resulted in a `400 Bad Request` response:

因此,引发了异常,但是看起来它已经被捕获了,尽管我很想自己捕获它,甚至只是跟踪它,并且有可能完全避免该异常。

有办法吗?

1 个答案:

答案 0 :(得分:0)

为防止异常,显而易见的答案是“在DynamoDb容量上使用自动缩放”。这就是我所做的,并具有一定的运气:当请求出现高峰时,我仍然有例外,但是平均而言,自动缩放效果很好。以下是可自动缩放的CloudFormation:

MyTableWriteScaling:
  Type: AWS::ApplicationAutoScaling::ScalableTarget
  Properties:
    MaxCapacity: 250
    MinCapacity: 5
    ResourceId: !Join ["/", ["table", !Ref myTable ]]
    ScalableDimension: "dynamodb:table:WriteCapacityUnits"
    ServiceNamespace: "dynamodb"
    RoleARN: {"Fn::GetAtt": ["DynamoDbScalingRole", "Arn"]}

WriteScalingPolicy:
  Type: AWS::ApplicationAutoScaling::ScalingPolicy
  Properties:
    PolicyName: !Join ['-', [!Ref 'AWS::StackName', 'MyTable', 'Write', 'Scaling', 'Policy']]
    PolicyType: TargetTrackingScaling
    ScalingTargetId: !Ref MyTableWriteScaling
    ScalableDimension: dynamodb:table:WriteCapacityUnits
    ServiceNamespace: dynamodb
    TargetTrackingScalingPolicyConfiguration:
      PredefinedMetricSpecification:
        PredefinedMetricType: DynamoDBWriteCapacityUtilization
      ScaleInCooldown: 1
      ScaleOutCooldown: 1
      TargetValue: 60
  DependsOn : MyTableWriteScaling

也就是说,我仍然有例外。我知道受限制的请求最终将被写入,但是我正在寻找一种防止异常的方法,因为我无法捕获它。

was introduced by Amazon on November 28的实现方式,它是按需DynamoDB。

非常有用,在公告中,我们读到:

  

如果您的应用程序流量难以预测和控制,工作负载的持续时间短而峰值大或者平均表利用率远低于峰值,那么按需DynamoDB很有用。

在CloudFormation中按需配置再简单不过了:

HotelStay:
  Type: AWS::DynamoDB::Table
  Properties:
    BillingMode: PAY_PER_REQUEST
    ...

更改BillingMode并删除ProvisionedThroughput可以防止抛出此类异常,因为它们永远消失了。