

# 4. 超出按需最大吞吐量
<a name="throttling-ondemand-capacity-exceeded-mitigation"></a>

配置[按需](on-demand-capacity-mode.md)表或 GSI 时，您可以选择在表或索引级别设置最大吞吐量限制（[https://docs.aws.amazon.com//amazondynamodb/latest/APIReference/API_OnDemandThroughput.html#DDB-Type-OnDemandThroughput-MaxReadRequestUnits](https://docs.aws.amazon.com//amazondynamodb/latest/APIReference/API_OnDemandThroughput.html#DDB-Type-OnDemandThroughput-MaxReadRequestUnits) 和 [https://docs.aws.amazon.com//amazondynamodb/latest/APIReference/API_OnDemandThroughput.html#DDB-Type-OnDemandThroughput-MaxWriteRequestUnits](https://docs.aws.amazon.com//amazondynamodb/latest/APIReference/API_OnDemandThroughput.html#DDB-Type-OnDemandThroughput-MaxWriteRequestUnits)），以防止成本失控或者防止下游系统过载。有关最大吞吐量的更多信息，请参阅 [DynamoDB 按需表的最大吞吐量](on-demand-capacity-mode-max-throughput.md)。

 当您的读取或写入消耗量超过这些自行施加的限额时，超出限额的其他请求会立即收到节流响应。DynamoDB 会返回异常以及 `MaxOnDemandThroughputExceeded` 节流原因类型，指示哪个资源已达到其吞吐量边界。

## 超过按需最大吞吐量导致的节流
<a name="throttling-ondemand-maximum-exceeded"></a>

此部分针对超出按需最大吞吐量导致的节流场景提供解决方法指导。使用本指南之前，请确保您根据应用程序的异常处理确定了具体的节流原因，并确定了受影响资源的 Amazon 资源名称（ARN）。有关检索节流原因和识别受限制资源的信息，请参阅[DynamoDB 节流诊断框架](throttling-diagnosing-workflow.md#throttling-diagnosing)。

在深入研究具体的节流场景之前，请首先考虑定是否确实需要采取行动：
+ **评估最大吞吐量设置**：这些限制是专门配置的，目的是控制成本或保护下游系统。如果您收到 `MaxOnDemandThroughputExceeded` 节流事件，则表示您的限额按设计发挥了作用。请考虑提高这些限额是否符合您最初的成本控制或系统保护目标。
+ **评测应用程序影响**：确定节流是否确实给您的应用程序或用户造成了问题。如果您的应用程序能够有效地处理重试操作，并且尽管偶尔会出现节流情况，但仍能满足性能要求，那么保持当前限额可能是合适的做法。
+ **查看流量模式**：分析节流代表的是预期的流量模式还是异常峰值。对于持续超过限额的可预测的重复流量模式，调整最大吞吐量设置可能会保障更好的性能。对于临时峰值，相比提升限额，实施更好的请求分配策略可能更合适。

如果您在考虑之后确定需要调整最大吞吐量设置，请参阅以下特定节流场景，了解针对性的修复选项：
+ [TableReadMaxOnDemandThroughputExceeded](#throttling-diagnostic-table-read-max-ondemand) 
+ [TableWriteMaxOnDemandThroughputExceeded](#throttling-diagnostic-table-write-max-ondemand)
+ [IndexReadMaxOnDemandThroughputExceeded](#throttling-diagnostic-index-read-max-ondemand) 
+ [IndexWriteMaxOnDemandThroughputExceeded](#throttling-diagnostic-index-write-max-ondemand) 

### TableReadMaxOnDemandThroughputExceeded
<a name="throttling-diagnostic-table-read-max-ondemand"></a>

**何时出现**  
按需表超过了所配置的最大读取吞吐能力。您可以在 [常见的诊断和监控方法](#ondemand-capacity-exceeded-diagnosis-monitoring) 中监控 CloudWatch 指标来分析节流事件。

**修复方案**  
请考虑采取以下步骤来解决节流事件：
+ **提高最大吞吐量限额**：使用 [DynamoDB 控制台](https://console.aws.amazon.com/dynamodb)、[AWS CLI](/amazondynamodb/latest/developerguide/AccessingDynamoDB.html#Tools.CLI) 或 DynamoDB `[UpdateTable](https://docs.aws.amazon.com//amazondynamodb/latest/APIReference/API_UpdateTable.html)` API，增加受影响表的 [https://docs.aws.amazon.com//amazondynamodb/latest/APIReference/API_OnDemandThroughput.html#DDB-Type-OnDemandThroughput-MaxReadRequestUnits](https://docs.aws.amazon.com//amazondynamodb/latest/APIReference/API_OnDemandThroughput.html#DDB-Type-OnDemandThroughput-MaxReadRequestUnits) 值，然后进行监控和调整。通过这种方法，在发生节流之前，表能够处理更高的读取吞吐量。
+ **移除最大限额**：将 `MaxReadRequestUnits` 设置为 `-1` 可取消上限，实现按需扩展，直至达到账户级别的吞吐量配额。这会移除您的自定义限额，但仍保持 AWS 账户级别的保护措施。但是，取消此限额后，请务必密切监控支出，因为在达到账户级别配额之前，您的表现在可以消耗更多的容量。

### TableWriteMaxOnDemandThroughputExceeded
<a name="throttling-diagnostic-table-write-max-ondemand"></a>

**何时出现**  
按需表超过了所配置的最大写入吞吐能力。您可以在 [常见的诊断和监控方法](#ondemand-capacity-exceeded-diagnosis-monitoring) 中监控 CloudWatch 指标来分析节流事件。

**修复方案**  
请考虑采取以下步骤来解决节流事件：
+ **提高最大吞吐量限额**：使用 [DynamoDB 控制台](https://console.aws.amazon.com/dynamodb)、[AWS CLI](/amazondynamodb/latest/developerguide/AccessingDynamoDB.html#Tools.CLI) 或 DynamoDB `[UpdateTable](https://docs.aws.amazon.com//amazondynamodb/latest/APIReference/API_UpdateTable.html)` API，增加受影响表的 [https://docs.aws.amazon.com//amazondynamodb/latest/APIReference/API_OnDemandThroughput.html#DDB-Type-OnDemandThroughput-MaxWriteRequestUnits](https://docs.aws.amazon.com//amazondynamodb/latest/APIReference/API_OnDemandThroughput.html#DDB-Type-OnDemandThroughput-MaxWriteRequestUnits) 值，然后进行监控和调整。
+ **移除最大限额**：将 `MaxWriteRequestUnits` 设置为 `-1` 可取消上限，实现按需扩展，直至达到账户级别的吞吐量配额。这会移除您的自定义限额，但仍保持 AWS 账户级别的保护措施。但是，取消此限额后，请务必密切监控支出，因为在达到账户级别配额之前，您的表现在可以消耗更多的容量。

### IndexReadMaxOnDemandThroughputExceeded
<a name="throttling-diagnostic-index-read-max-ondemand"></a>

**何时出现**  
在按需模式下，向某个 GSI 发出的读取请求超过了为 GSI 配置的最大读取吞吐能力。您可以在 [常见的诊断和监控方法](#ondemand-capacity-exceeded-diagnosis-monitoring) 中监控 CloudWatch 指标来分析节流事件。

**修复方案**  
请考虑采取以下步骤来解决节流事件：
+ **提高 GSI 最大吞吐量限额**：使用 [DynamoDB 控制台](https://console.aws.amazon.com/dynamodb)、[AWS CLI](/amazondynamodb/latest/developerguide/AccessingDynamoDB.html#Tools.CLI) 或 DynamoDB `[UpdateTable](https://docs.aws.amazon.com//amazondynamodb/latest/APIReference/API_UpdateTable.html)` API，增加受影响 GSI 的 [https://docs.aws.amazon.com//amazondynamodb/latest/APIReference/API_OnDemandThroughput.html#DDB-Type-OnDemandThroughput-MaxReadRequestUnits](https://docs.aws.amazon.com//amazondynamodb/latest/APIReference/API_OnDemandThroughput.html#DDB-Type-OnDemandThroughput-MaxReadRequestUnits) 值，然后进行监控和调整。
+ **移除 GSI 最大限额**：将 GSI 的 `MaxReadRequestUnits` 设置为 `-1` 可取消上限，实现按需扩展，直至达到账户级别的吞吐量配额。这会移除您的自定义限额，但仍保持 AWS 账户级别的保护措施。但是，取消此限额后请务必密切监控支出。

### IndexWriteMaxOnDemandThroughputExceeded
<a name="throttling-diagnostic-index-write-max-ondemand"></a>

**何时出现**  
按需模式下，更新基表中的项目会触发向某个 GSI 的写入，超过了为 GSI 配置的最大写入吞吐能力，从而导致[背压节流](gsi-throttling.md)。您可以在 [常见的诊断和监控方法](#ondemand-capacity-exceeded-diagnosis-monitoring) 中监控 CloudWatch 指标来分析节流事件。

**修复方案**  
请考虑采取以下步骤来解决节流事件：
+ **提高 GSI 最大吞吐量限额**：使用 [DynamoDB 控制台](https://console.aws.amazon.com/dynamodb)、[AWS CLI](/amazondynamodb/latest/developerguide/AccessingDynamoDB.html#Tools.CLI) 或 DynamoDB `[UpdateTable](https://docs.aws.amazon.com//amazondynamodb/latest/APIReference/API_UpdateTable.html)` API，增加受影响 GSI 的 [https://docs.aws.amazon.com//amazondynamodb/latest/APIReference/API_OnDemandThroughput.html#DDB-Type-OnDemandThroughput-MaxWriteRequestUnits](https://docs.aws.amazon.com//amazondynamodb/latest/APIReference/API_OnDemandThroughput.html#DDB-Type-OnDemandThroughput-MaxWriteRequestUnits) 值，然后进行监控和调整。
+ **移除 GSI 最大限额**：将 GSI 的 `MaxWriteRequestUnits` 设置为 `-1` 可取消上限，实现按需扩展，直至达到账户级别的吞吐量配额。这会移除您的自定义限额，但仍保持 AWS 账户级别的保护措施。但是，取消此限额后请务必密切监控支出。

## 常见的诊断和监控方法
<a name="ondemand-capacity-exceeded-diagnosis-monitoring"></a>

在对超出按需最大吞吐量导致的节流事件进行故障排除时，您可以利用多种 CloudWatch 指标来确定根本原因和扩展模式。

**关键 CloudWatch 指标**  
监控以下关键指标，以便诊断超出最大吞吐量导致的节流：
+ **最大吞吐量节流事件**：[https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/metrics-dimensions.html#ReadMaxOnDemandThroughputThrottleEvents](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/metrics-dimensions.html#ReadMaxOnDemandThroughputThrottleEvents) 和 [https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/metrics-dimensions.html#WriteMaxOnDemandThroughputThrottleEvents](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/metrics-dimensions.html#WriteMaxOnDemandThroughputThrottleEvents) 跟踪什么时候由于超出最大限额导致请求受限。[https://docs.aws.amazon.com//amazondynamodb/latest/developerguide/metrics-dimensions.html#ReadThrottleEvents](https://docs.aws.amazon.com//amazondynamodb/latest/developerguide/metrics-dimensions.html#ReadThrottleEvents) 和 [https://docs.aws.amazon.com//amazondynamodb/latest/developerguide/metrics-dimensions.html#WriteThrottleEvents](https://docs.aws.amazon.com//amazondynamodb/latest/developerguide/metrics-dimensions.html#WriteThrottleEvents) 跟踪什么时候读取或写入请求超出了预置容量。
+ **当前为表或全局二级索引配置的最大吞吐量**：[https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/metrics-dimensions.html#OnDemandMaxReadRequestUnits](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/metrics-dimensions.html#OnDemandMaxReadRequestUnits) 和 [https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/metrics-dimensions.html#OnDemandMaxWriteRequestUnits](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/metrics-dimensions.html#OnDemandMaxWriteRequestUnits) 显示当前的最大容量限额。
+ **实际容量消耗**：[https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/metrics-dimensions.html#ConsumedReadCapacityUnits](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/metrics-dimensions.html#ConsumedReadCapacityUnits) 和 [https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/metrics-dimensions.html#ConsumedWriteCapacityUnits](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/metrics-dimensions.html#ConsumedWriteCapacityUnits) 显示实际使用模式。

**分析方法**  
按照以下步骤，对超出按需最大吞吐量的情况进行诊断：

1. 将已消耗的容量与最大容量限额进行比较，检查消耗量是否持续接近或超过最大限额。

1. 查看节流事件的频率和时间以确定模式。查找与节流事件相吻合的消耗容量突然增加的情况。

1. 使用 [CloudWatch Contributor Insights](contributorinsights_HowItWorks.md) 来确定哪些项目或分区键消耗了最多的容量。