我们应该使用 Cassandra NoSQL 计数器还是 LWT 进行自动增量整数密钥生成? [英] Should we use Cassandra NoSQL counter or LWT for auto incremental integer key generation?

查看:19
本文介绍了我们应该使用 Cassandra NoSQL 计数器还是 LWT 进行自动增量整数密钥生成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想在 Cassandra 中生成自动增量整数键.这在传统数据库中是微不足道的任务,但在 Cassandra 中并不复杂.

We want to generated auto incremental integer key in Cassandra. This is trivial task in traditional databases but little complicated in Cassandra.

我已经尝试过可以使用递增的计数器数据类型

I have tried out Counter data type which can be incremented using

value=value+1

并尝试使用 LWT

UPDATE myTable SET value=newValue  IF value=oldValue.
(where newValue=oldValue+1 for auto increment)

我已被强烈警告不要使用计数器变量.我不知道为什么.您能帮我了解一下上述两种方法的优缺点吗?

I have been strongly warned against counter variables. I am not sure why though. Can you please help me understand pros and cons of above two approaches?

推荐答案

从免责声明开始,您很可能不希望在 C* 中使用自动递增的整数键.UUID 或 TimeUUID 更有可能是您想要的.但是,如果您确实确实需要一个自动递增值,请继续阅读.

Starting with a Disclaimer, You most likely do not want an auto-incrementing integer key in C*. More likely a UUID or TimeUUID is what you want. But if you do happen to really need an auto incrementing value read on.

状态和分布式系统不喜欢混合.通常,当您想真正"确定分布式系统的状态时,您必须检查所有副本,从而牺牲可用性/分区容错性.LWT 使用 Paxos 来允许检查和设置操作,但要做到这一点,它们需要一定数量的节点,并且比正常的 Cassandra 操作要慢得多.LWT 应仅用于应用程序中使用的一小部分操作.只要计数器变量的争用很少,并且您不需要每次写入都需要它,就可以了.

State and Distributed Systems do not like to blend. Generally whenever you want to make 'really' sure of the state of your distributed system you have to check all replicas and thus sacrifice availability/partition tolerance. LWT use Paxos to allow check and set operations but to do this they require a quorum of nodes and are significantly slower than normal Cassandra operations. LWT should only be used in a small percentage of the operations used in your applications. As long as there is little contention for the counter variable and you don't need it for every write you should be ok.

C* 中的计数器是一个非常不同的实现.在旧版本的 C* 中,它们因失去对值的跟踪能力而有点臭名昭著.它们的实现已被重写以大大提高稳定性,但需要在应用程序端进行仔细规划以保证独特的操作.你可以想象两个客户同时增加一个计数器,每个都认为他们收到了一个唯一的值.因此,如果您确实需要确保唯一性,我认为您应该使用 LWT.

Counters in C* are a very different implementation. In older versions of C* they were slightly infamous for their ability to lose track of values. Their implementation has been rewritten for vastly improved stability but it would require careful planning on the application side to guarantee unique operations. You can imagine two clients both incrementing a counter simultaneously each thinking that they had received a unique value. Because of this I think you should use LWT if you really need to make sure of uniqueness.

这篇关于我们应该使用 Cassandra NoSQL 计数器还是 LWT 进行自动增量整数密钥生成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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