诊断节流 - Amazon DynamoDB

诊断节流

当应用程序出现节流情况时,DynamoDB 会提供详细的异常信息以及针对性的 CloudWatch 指标,来帮助您诊断这些事件。

本节介绍了理解 DynamoDB 应用程序中节流事件的系统性方法。文中展示了如何解读节流异常,如何将其与 CloudWatch 指标关联起来以获得更深入的见解,以及了解哪些更改可减少 DynamoDB 应用程序中出现的节流情况。

了解节流限制

DynamoDB 在对请求进行节流时,会返回包含详细诊断信息的具体异常情况。例如,在 Java 中,这些信息包括 ProvisionedThroughputExceededExceptionRequestLimitExceededThrottlingException

每个异常都包括 ThrottlingReasons,这是包含两个关键字段的各个 ThrottlingReason 的集合,有助于您识别和了解节流情况:

  • 原因:采用 <ResourceType><OperationType><LimitType> 格式的串联字段

  • 资源 ARN:受影响表或索引的 Amazon 资源名称(ARN)

原因字段采用一致的模式,可帮助您准确了解所发生的情况:

  • ResourceType(受限制的资源):TableIndex

  • OperationType(操作类型):ReadWrite

  • LimitType(出现节流的原因):

    • KeyRangeThroughputExceeded:当支持表或索引的特定分区消耗的读取或写入容量,超过了每个分区的内部吞吐量限额时,就会发生这种情况。

    • ProvisionedThroughputExceeded:当读取或写入消耗速率超过预置数量时,在预置的表或全局二级索引上会发生这种情况。

    • AccountLimitExceeded:当按需表或索引的读取或写入消耗速率,超过了在账户级别设置的表及其索引的最大消耗速率时,就会发生这种情况。您可以请求提高此配额。

    • MaxOnDemandThroughputExceeded:当按需表或索引的读取或写入消耗速率,超过了用户提供的表或索引的最大消耗速率时,就会发生这种情况。您可以自行增加此值(不超过账户限额),也可以将其设置为 -1,表示没有用户提供的限额。

资源 ARN 准确识别哪个表或索引受到限制:

  • 对于表:arn:aws:dynamodb:[region]:[account-id]:table/[table-name]

  • 对于索引:arn:aws:dynamodb:[region]:[account-id]:table/[table-name]/index/[index-name]

完全节流的原因示例:

  • TableReadProvisionedThroughputExceeded

  • IndexWriteAccountLimitExceeded

这有助于准确确定哪些资源受限、导致受限的操作类型以及发生节流的原因。

异常示例

示例 1:某个 GSI 上超出了预置容量

{ "ThrottlingReasons": [ { "reason": "IndexWriteProvisionedThroughputExceeded", "resource": "arn:aws:dynamodb:us-west-2:123456789012:table/CustomerOrders/index/OrderDateIndex" } ], "awsErrorDetails": { "errorCode": "ProvisionedThroughputExceeded", "errorMessage": "The level of configured provisioned throughput for the index was exceeded", "serviceName": "DynamoDB", "sdkHttpResponse": { "statusText": "Bad Request", "statusCode": 400 } } }

在此示例中,应用程序收到了 ProvisionedThroughputExceededException,带有原因 IndexWriteProvisionedThroughputExceeded。由于写入消耗量已超过为 GSI 配置的预置写入容量,因此对 OrderDateIndex 的写入操作受到限制。

示例 2:超出按需最大吞吐量

{ "ThrottlingReasons": [ { "reason": "TableReadMaxOnDemandThroughputExceeded", "resource": "arn:aws:dynamodb:us-east-1:123456789012:table/UserSessions" } ], "awsErrorDetails": { "errorMessage": "Throughput exceeds the maximum OnDemandThroughput configured on table or index", "errorCode": "ThrottlingException", "serviceName": "DynamoDB", "sdkHttpResponse": { "statusText": "Bad Request", "statusCode": 400 } } }

在此示例中,由于读取数超过了在表上配置的最大按需吞吐量限额,因此对 UserSessions 表的读取操作受到限制。

DynamoDB 节流诊断框架

当应用程序遇到节流情况时,请按照以下步骤诊断和解决问题。

第 1 步:分析 ThrottlingReason 详细信息

  1. 检查原因字段来确定造成节流的具体原因。原因字段中详细说明了受限资源的类型(表或索引)、导致节流事件的操作类型(读取或写入)以及超出的限额类型(分区、预置吞吐量、账户限额)。

  2. 检查 resourceArn 字段以确定哪些资源(表或 GSI)受到限制。

  3. 将这些信息结合起来,了解节流问题的完整背景信息。

    例如,考虑这种场景:您收到了异常 ProvisionedThroughputExceededException 并带有节流原因 TableWriteKeyRangeThroughputExceeded。受影响的 resourceARN 是 arn:aws:dynamodb:us-west-2:123456789012:table/CustomerOrders

    这种组合告知您,对 CustomerOrders 表的写入操作正在受到限制。节流发生在分区级别(而不是表级别,后一种情况显示为 TableWriteProvisionedThroughputExceeded)。根本原因是您超过了特定分区键值或范围的最大吞吐能力,这表明存在热分区问题。

    了解异常元素之间的这种关系有助于您实施适当的缓解策略,在本例中,您需要解决热分区问题,而不是增加表的总预置容量。

第 2 步:识别和分析相关的 CloudWatch 指标

  1. 确定指标:DynamoDB 中的每个节流原因都直接对应于特定 CloudWatch 指标,您可以监控这些指标来跟踪和分析节流事件。您可以根据节流原因,系统性地得出相应的 CloudWatch 指标名称。

  2. 使用以下参考表,将节流原因与相应的 CloudWatch 指标匹配起来:

    完整节流原因和 CloudWatch 指标参考
    类别 节流原因 主要 CloudWatch 指标
    超过预置容量 TableReadProvisionedThroughputExceeded ReadProvisionedThroughputThrottleEvents
    TableWriteProvisionedThroughputExceeded WriteProvisionedThroughputThrottleEvents
    IndexReadProvisionedThroughputExceeded ReadProvisionedThroughputThrottleEvents(GSI)
    IndexWriteProvisionedThroughputExceeded WriteProvisionedThroughputThrottleEvents(GSI)
    超出分区限额 TableReadKeyRangeThroughputExceeded ReadKeyRangeThroughputThrottleEvents
    TableWriteKeyRangeThroughputExceeded WriteKeyRangeThroughputThrottleEvents
    IndexReadKeyRangeThroughputExceeded ReadKeyRangeThroughputThrottleEvents(GSI)
    IndexWriteKeyRangeThroughputExceeded WriteKeyRangeThroughputThrottleEvents(GSI)
    超出按需最大值 TableReadMaxOnDemandThroughputExceeded ReadMaxOnDemandThroughputThrottleEvents
    TableWriteMaxOnDemandThroughputExceeded WriteMaxOnDemandThroughputThrottleEvents
    IndexReadMaxOnDemandThroughputExceeded ReadMaxOnDemandThroughputThrottleEvents(GSI)
    IndexWriteMaxOnDemandThroughputExceeded WriteMaxOnDemandThroughputThrottleEvents(GSI)
    超过账户限额 TableReadAccountLimitExceeded ReadAccountLimitThrottleEvents
    TableWriteAccountLimitExceeded WriteAccountLimitThrottleEvents
    IndexReadAccountLimitExceeded ReadAccountLimitThrottleEvents(GSI)
    IndexWriteAccountLimitExceeded WriteAccountLimitThrottleEvents(GSI)

    例如,如果您收到了 IndexWriteProvisionedThroughputExceeded,则对于在 ResourceArn 中标识的具体索引,至少应监控 WriteProvisionedThroughputThrottleEvents CloudWatch 指标。

  3. 在 CloudWatch 中监控这些指标,了解节流事件的频率和时间,区分读取与写入节流,确定节流情况增加时的时间模式,并跟踪您的容量利用率趋势。

    DynamoDB 为每个表和全局二级索引发布详细指标。指标(ReadThrottleEventsWriteThrottleEventsThrottledRequests)汇总了您的表及其索引中的所有节流事件。

第 3 步:使用 CloudWatch Contributor Insights 确定受限的键和高访问率(针对与分区相关的节流)

如果您在步骤 1 中发现了与分区相关的问题(例如 KeyRangeThroughputExceeded 错误),那么 CloudWatch Contributor Insights for DynamoDB 可以帮助您诊断哪些特定的键造成了流量增长,并在表或索引中产生了节流事件。

  1. 根据 ResourceARN,为受限的表或索引启用 CloudWatch Contributor Insights。

    您可以选择受限的键模式,将注意力集中在最受限的键上。此模式非常适合持续监控,因为此模式仅在出现节流时才处理事件。或者,访问的键和受限的键模式可帮助您查找最常访问的键中的模式。

  2. 分析报告以找出有问题的模式。寻找访问速率或节流速率高得不成比例的键,将节流问题与流量模式关联起来。您可以创建集成控制面板,将 Contributor Insights 图表和 DynamoDB CloudWatch 指标结合起来。

有关如何启用和使用 CloudWatch Contributor Insights 的详细信息,请参阅使用 CloudWatch Contributor Insights for DynamoDB

第 4 步:确定合适的解决方案

在诊断出造成节流的具体原因后,根据您的具体情况实施推荐的解决方案。合适的解决方案取决于多种因素,包括您的节流场景、表的容量模式、表和键设计决策、访问模式和查询效率、全局和二级索引配置以及整体系统架构和集成点。

有关解决您的特定节流场景的详细解决方案,请参阅 DynamoDB 节流解决方法指导一节。此资源根据您的具体节流原因和容量模式配置,提供定制的针对性补救策略。

第 5 步:监控进展情况

  1. 跟踪与您的节流场景相对应的 CloudWatch 指标。

  2. 观察节流事件是否减少,来验证您的缓解策略是否有效。