DynamoDB 中的并发更新,是否有任何保证? [英] Concurrent updates in DynamoDB, are there any guarantees?

查看:22
本文介绍了DynamoDB 中的并发更新,是否有任何保证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般来说,如果我想确定当多个线程对 DynamoDB 中的同一个项目进行并发更新时会发生什么,我应该使用条件更新(即乐观锁定").我知道.但我想知道是否还有其他情况可以确保对同一项目的并发更新仍然存在.

In general, if I want to be sure what happens when several threads make concurrent updates to the same item in DynamoDB, I should use conditional updates (i.e.,"optimistic locking"). I know that. But I was wondering if there is any other case when I can be sure that concurrent updates to the same item survive.

例如,在 Cassandra 中,对同一项目的不同属性进行并发更新是可以的,并且这两个更新最终都可以读取.在 DynamoDB 中也是如此吗?还是这些更新中只有一个存在?

For example, in Cassandra, making concurrent updates to different attributes of the same item is fine, and both updates will eventually be available to read. Is the same true in DynamoDB? Or is it possible that only one of these updates survive?

一个非常相似的问题是,如果我同时将两个不同的值添加到同一项目的集合或列表中,会发生什么情况.我是否保证在阅读此集合或列表时最终会看到这两个值,或者是否有可能在某种 DynamoDB冲突解决"协议期间,其中一个添加会掩盖另一个?

A very similar question is what happens if I add, concurrently, two different values to a set or list in the same item. Am I guaranteed that I'll eventually see both values when I read this set or list, or is it possible that one of the additions will mask out the other during some sort of DynamoDB "conflict resolution" protocol?

我看到我的第二个问题的一个版本过去已经在这里提出过 Are DynamoDB "设置"值 CDRT?,但答案是指一个不太清楚的常见问题解答条目,该条目不再存在.作为我的问题的答案,我最希望看到的是官方 DynamoDB 文档,其中说明了 DynamoDB 在既不涉及条件更新"也不涉及事务"时如何处理并发更新,尤其是在上述两个示例中发生的情况.没有这样的官方文档,有没有人对这样的并发更新有任何实际经验?

I see a version of my second question was already asked here in the past Are DynamoDB "set" values CDRTs?, but the answer refered to a not-very-clear FAQ entry which doesn't exist any more. What's I would most like to see as an answer to my question is an official DynamoDB documentation that says how DynamoDB handles concurrent updates when neither "conditional updates" nor "transactions" are involved, and in particular what happens in the above two examples. Absent such official documentation, does anyone have any real-world experience with such concurrent updates?

推荐答案

我刚有同样的问题,遇到了这个帖子.鉴于没有答案,我决定自己测试一下.

I just had the same question and came across this thread. Given that there was no answer I decided to test it myself.

据我观察,答案是只要你更新不同的属性,它最终会成功.我推送到项目的更新越多,它确实需要更长的时间,因此它们看起来是按顺序而不是并行写入的.

The answer, as far as I can observe is that as long as you are updating different attributes it will eventually succeed. It does take a little bit longer the more updates I push to the item so they appear to be written in sequence rather than in parallel.

我还尝试并行更新单个 List 属性,但预计会失败,所有查询完成后生成的列表被破坏,只有一些条目被推送到它.

I also tried updating a single List attribute in parallel and this expectedly fail, the resulting list once all queries had completed was broken and only had some of the entries pushed to it.

我进行的测试非常初级,我可能遗漏了一些东西,但我相信结论是正确的.

The test I ran was pretty rudimentary and I might be missing something but I believe the conclusion to be correct.

为了完整起见,这里是我使用的脚本,nodejs.

For completeness, here is the script I used, nodejs.

const aws = require('aws-sdk');
const ddb = new aws.DynamoDB.DocumentClient();

const key = process.argv[2];
const num = process.argv[3];


run().then(() => {
    console.log('Done');
});

async function run() {
    const p = [];
    for (let i = 0; i < num; i++) {
        p.push(ddb.update({
            TableName: 'concurrency-test',
            Key: {x: key},
            UpdateExpression: 'SET #k = :v',
            ExpressionAttributeValues: {
                ':v': `test-${i}`
            },
            ExpressionAttributeNames: {
                '#k': `k${i}`
            }
        }).promise());
    }

    await Promise.all(p);

    const response = await ddb.get({TableName: 'concurrency-test', Key: {x: key}}).promise();
    const item = response.Item;

    console.log('keys', Object.keys(item).length);
}

像这样运行:

node index.js {key} {number}
node index.js myKey 10

时间安排:

  • 10 次更新:~1.5 秒
  • 100 次更新:~2 秒
  • 1000 次更新:~10-20 秒(波动很大)

值得注意的是,指标显示了许多受限制的事件,但这些事件是由 nodejs sdk 使用指数退避在内部处理的,因此一旦尘埃落定,一切都按预期编写.

Worth noting is that the metrics show a lot of throttled events but these are handled internally by the nodejs sdk using exponential backoff so once the dust settled everything was written as expected.

这篇关于DynamoDB 中的并发更新,是否有任何保证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆