如何避免从两个 lambda 表达式覆盖 dynamodb?

How can I avoid overwriting dynamodb from two lambdas?

我正在从 lambda 函数更新 dynamodb table。在高吞吐量的情况下,可能有多个 lambda 实例 运行 同时更新同一个 table。有什么办法可以保护table?能不能锁定table?

没有内置机制 来锁定 DynamoDB table 以防止并发覆盖。但是您可以自己实现一些设计模式,或者根据您的编程语言,找到可以使用的现有实现,甚至由 AWS 提供。

AWS docs 中,您可以找到有关如何实施 Optimistic locking:

的信息

Optimistic locking is a strategy to ensure that the client-side item that you are updating (or deleting) is the same as the item in Amazon DynamoDB. If you use this strategy, your database writes are protected from being overwritten by the writes of others, and vice versa.

AWS 还为 java 提供 Amazon DynamoDB Lock Client:

The DynamoDB Lock Client implements a protocol allowing similar applications to take advisory locks on any part of your problem domain, big or small. This protocol ensures your players “stay in possession of the ball” for a certain period of time.

归根结底,由您设计一个满足您需求的“保护 table”解决方案。