是否可以原子地增加 table 属性作为 DynamoDB 事务功能中的多个操作之一?

Is it possible to atomically increment a table attribute as one of multiple operations within the DynamoDB Transactions feature?

我们的团队有一个 DDB table,其中包含具有表示各种生命周期状态的 'state' 属性的项目。我们有另一个 table 按状态跟踪这些项目的计数(分区键是容器类型的 ID,一组项目分组在其中,排序键是状态)。目前我们使用 lambda 来保持由包含项目的 table 上的 ddb 流触发的计数同步,但是我们在这方面遇到了一些问题(延迟、幂等性)并且正在研究使用 DynamoDB 事务作为一种方式每当我们更改项目的状态时同步更新计数 table。

我想知道是否有一种方法可以像 in this example but within a DynamoDB Transaction so that the update to item state and count increment will be an ACID, all-or-nothing operation? The approach in the example uses an UpdateItemRequest with AttributeValueUpdate, while it seems like Transactions can only use the Update object 一样使用 AttributeValueUpdate + 'AttributeAction.ADD' 进行原子增量添加,但我一直无法找到将 AttributeValueUpdate 与那个对象。 这里

根据我对事务的理解,独立操作没有顺序,因此任何操作都不能依赖于另一个操作的输出(我无法读取值然后在同一事务中添加到它)并且如果我读取value first 它可能会在事务发生时更改。我还没有找到任何其他在 DynamoDB 事务中递增值的示例,希望有人能告诉我这是否可行。

这绝对是可能的,您只是无法访问 UpdateItemRequest 用来构造更新表达式(通过 AttributeValueUpdate)的流畅 API。相反,您必须手动编写更新表达式。

在您链接的示例中,以下内容:

UpdateItemRequest updateRequest =
    new UpdateItemRequest().withTableName("local-content")
        .withKey(key)
        .addAttributeUpdatesEntry("views", new AttributeValueUpdate()
            .withValue(new AttributeValue().withN("" + delta))
            .withAction(AttributeAction.ADD));

相当于这样的东西:

// Create a map of expression attribute values with the amount to increment by
Map<String, AttributeValue> values = new HashMap<String, AttributeValue>();
values.put(":one", new AttributeValue().withN("1"));

Update update =
    new Update().withTableName("local-content")
        .withKey(key)
        .withExpressionAttributeValues(values)
        .withUpdateExpression("ADD views :one");

话虽如此,请记住 DynamoDB 事务比非事务操作要昂贵得多,并且您尝试做的事情可能会在高吞吐量下崩溃,因为计数器将成为热键。