全球二级索引的 DynamoDB 一致性读取

DynamoDB consistent reads for Global Secondary Index(全球二级索引的 DynamoDB 一致性读取)

本文介绍了全球二级索引的 DynamoDB 一致性读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我无法获得全局二级索引的一致读取?

Why cant I get consistent reads for global-secondary-indexes?

我有以下设置:

表格:tblUsers(id 为哈希)

The table: tblUsers (id as hash)

全局二级索引:tblUsersEmailIndex(电子邮件作为哈希,id 作为属性)

Global Secondary Index: tblUsersEmailIndex (email as hash, id as attribute)

全局二级索引:tblUsersUsernameIndex(用户名作为hash,id作为属性)

Global Secondary Index: tblUsersUsernameIndex (username as hash, id as attribute)

我查询索引以检查给定的电子邮件或用户名是否存在,因此我不会创建重复的用户.

I query the indexes to check if a given email or username is present, so I dont create a duplicate user.

现在,问题是我无法对索引的查询进行一致的读取.但为什么不呢?这是我真正需要最新数据的少数场合之一.

Now, the problem is I cant do consistent reads for queries on the indexes. But why not? This is one of the few occasions I actually need up-to-date data.

根据 AWS 文档:

对全局二级索引的查询仅支持最终一致性.

Queries on global secondary indexes support eventual consistency only.

在正常情况下,对表数据的更改会在几分之一秒内传播到全局二级索引.但是,在一些不太可能发生的故障情况下,可能会出现更长的传播延迟.因此,您的应用程序需要预测和处理对全局二级索引的查询返回的结果不是最新的情况.

Changes to the table data are propagated to the global secondary indexes within a fraction of a second, under normal conditions. However, in some unlikely failure scenarios, longer propagation delays might occur. Because of this, your applications need to anticipate and handle situations where a query on a global secondary index returns results that are not up-to-date.

但是我该如何处理这种情况呢?如何确保给定的电子邮件或用户名不存在于数据库中?

But how do i handle this situation? How can I make sure that a given email or username is not already present in the db?

推荐答案

你可能已经经历过这个:http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html

You probably already went through this: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html

简短的回答是,你不能用全局二级索引做你想做的事情(即它总是最终的一致性).

The short answer is that you cannot do what you want to do with Global Secondary Indexes (ie it's always eventual consistency).

这里的解决方案是有一个单独的表,其中包含您感兴趣的属性作为键,并在那里进行一致的读取.您需要确保在插入新实体时进行更新,并且您还必须担心插入成功但不在主表中的边缘情况(即您需要确保它们同步)

A solution here would be to have a separate table w/ the attribute you're interested in as a key and do consistent reads there. You would need to ensure you are updating that whenever you are inserting new entities, and you would also have to worry about the edge case in which inserts there succeed, but not in the main table (ie you need to ensure they are in sync)

另一种解决方案是扫描整个表,但如果表很大,这可能会过大.

Another solution would be to scan the whole table, but that would probably be overkill if the table is large.

您为什么关心有人使用同一电子邮件创建 2 个帐户?您可以只使用用户名作为主哈希键,而不是强制电子邮件唯一性.

Why do you care if somebody creates 2 accounts with the same email? You could just use the username as the primary hash key and just not enforce the email uniqueness.

这篇关于全球二级索引的 DynamoDB 一致性读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:全球二级索引的 DynamoDB 一致性读取

基础教程推荐