DynamoDB预配置的读/写容量单位超出意外

时间:2019-04-01 07:30:26

标签: amazon-dynamodb

我运行了一个使用api网关和lambda将数据发送到dynamodb的程序。

所有发送到数据库的数据很小,仅从大约200台计算机发送。

我仍在使用免费套餐,有时在本月中旬出乎意料之外,我开始获得更高的预配置读/写容量,然后从这一天开始,我每天支付固定金额,直到月底。

有人可以从下面的图片中了解在03/13中发生了什么,导致图表中的此派克,并使这些拨备从50增加到65?

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

我不能仅凭这些图表就知道发生了什么,但要考虑一些事情:

您可能不知道DynamoDB表的新“ PAY_PER_REQUEST”计费模式选项,该选项使您几乎忘记手动设置吞吐量:https://aws.amazon.com/blogs/aws/amazon-dynamodb-on-demand-no-capacity-planning-and-pay-per-request-pricing/

此外,对于您的用例而言可能没有意义,但是对于自由层项目,我发现通过SQS队列代理对DynamoDB的所有写操作很有用(将队列用作具有保留并发性的Lambda的事件源)与您预配置的吞吐量兼容)。如果您的项目是由事件驱动的,那么这很容易,例如,构建DynamoDB请求对象/参数,写入SQS,然后下一步是从DynamoDB流触发的Lambda(因此您不希望同步响应)从第一个Lambda中的写操作开始)。像这样:

用于SQS触发的Lambda的无服务器配置示例:

dynamodb_proxy:
  description: SQS event function to write to DynamoDB table '${self:custom.dynamodb_table_name}'
  handler: handlers/dynamodb_proxy.handler
  memorySize: 128
  reservedConcurrency: 95 # see custom.dynamodb_active_write_capacity_units
  environment:
    DYNAMODB_TABLE_NAME: ${self:custom.dynamodb_table_name}
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:PutItem
      Resource:
        - Fn::GetAtt: [ DynamoDbTable, Arn ]
    - Effect: Allow
      Action:
        - sqs:ReceiveMessage
        - sqs:DeleteMessage
        - sqs:GetQueueAttributes
      Resource:
        - Fn::GetAtt: [ DynamoDbProxySqsQueue, Arn ]
  events:
    - sqs:
        batchSize: 1
        arn:
          Fn::GetAtt: [ DynamoDbProxySqsQueue, Arn ]

示例写入SQS:

await sqs.sendMessage({
  MessageBody: JSON.stringify({
    method: 'putItem',
    params: {
      TableName: DYNAMODB_TABLE_NAME,
      Item: {
        ...attributes,
        created_at: {
          S: createdAt.toString(),
        },
        created_ts: {
          N: createdAtTs.toString(),
        },
      },
      ...conditionExpression,
    },
  }),
  QueueUrl: SQS_QUEUE_URL_DYNAMODB_PROXY,
}).promise();

SQS触发的Lambda:

import retry from 'async-retry';
import { getEnv } from '../lib/common';
import { dynamodb } from '../lib/aws-clients';

const {
  DYNAMODB_TABLE_NAME
} = process.env;

export const handler = async (event) => {
  const message = JSON.parse(event.Records[0].body);

  if (message.params.TableName !== env.DYNAMODB_TABLE_NAME) {
    console.log(`DynamoDB proxy event table '${message.params.TableName}' does not match current table name '${env.DYNAMODB_TABLE_NAME}', skipping.`);
  } else if (message.method === 'putItem') {
    let attemptsTaken;
    await retry(async (bail, attempt) => {
      attemptsTaken = attempt;
      try {
        await dynamodb.putItem(message.params).promise();
      } catch (err) {
        if (err.code && err.code === 'ConditionalCheckFailedException') {
          // expected exception
          // if (message.params.ConditionExpression) {
          //   const conditionExpression = message.params.ConditionExpression;
          //   console.log(`ConditionalCheckFailed: ${conditionExpression}. Skipping.`);
          // }
        } else if (err.code && err.code === 'ProvisionedThroughputExceededException') {
          // retry
          throw err;
        } else {
          bail(err);
        }
      }
    }, {
      retries: 5,
      randomize: true,
    });

    if (attemptsTaken > 1) {
      console.log(`DynamoDB proxy event succeeded after ${attemptsTaken} attempts`);
    }
  } else {
    console.log(`Unsupported method ${message.method}, skipping.`);
  }
};