为什么我无法获得全局二级索引的一致读取?
我有以下设置:
表:
tblUsers
(ID 作为哈希值)全局二级索引:
tblUsersEmailIndex
(电子邮件为哈希值,ID 为 属性)全局二级索引:
tblUsersUsernameIndex
(用户名作为哈希值,id 作为属性)
我查询索引以检查给定的电子邮件或用户名是否存在,因此我不会创建重复的用户。
现在的问题是我无法对索引查询进行一致的读取。但为什么不呢?这是我真正需要最新数据的少数情况之一。
根据 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.
但是我该如何处理这种情况呢?如何确保给定的电子邮件或用户名尚未存在于数据库中?
最佳答案
您可能已经经历过这个: http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html
简短的回答是,您无法使用全局二级索引做您想做的事情(即它始终是最终一致性)。
这里的解决方案是拥有一个单独的表,其中包含您感兴趣的属性作为键,并在那里进行一致的读取。您需要确保每当插入新实体时都进行更新,并且您还必须担心插入成功但不在主表中的边缘情况(即您需要确保它们同步)
另一种解决方案是扫描整个表,但如果表很大,这可能会有点过分。
如果有人使用同一电子邮件创建了 2 个帐户,您为什么要关心?您可以仅使用用户名作为主哈希键,而不强制执行电子邮件的唯一性。
关于asp.net - DynamoDB 全局二级索引的一致性读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35414372/