使用增量计数器在Couchbase集群中生成唯一密钥 [英] Using an Increment counter for unique key generation in a Couchbase cluster

查看:87
本文介绍了使用增量计数器在Couchbase集群中生成唯一密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Couchbase和其他NoSQL数据库,我听到的最常见的问题是如何为记录生成唯一键,或者更具体地说,是如何复制通用关系数据库的AUTO INCREMENT功能。

The most common question I hear about Couchbase and other NoSQL databases is how to generate unique keys for records - or more specificly - how to replicate the AUTO INCREMENT feature of common Relation Databases.

Couchbase中经常提到的解决方案是增量函数,您可以在其中调用数字键的增量,它将按顺序生成一个新的唯一数字。

The solution in Couchbase often mentioned is the increment function, where you can call increment on a numeric key and it will generate a new unique number in sequence.

对此我的问题是,在复制方面,我无法预见我所预见的巨大问题。

My question on this is I cannot get my head around the massive problem I foresee when it comes to replication.

考虑到您有一个由三个Couchbase节点组成的集群,存储请求日志。您想键入此日志,以便创建一个名为 requestlog_counter的条目。

Consider you have a cluster of three Couchbase nodes and you are storing a request log. You want to key this log so you create an entry called "requestlog_counter".

现在说我们有4个Web节点,每个节点每秒接收20个请求,每个需求记录为 request :: {ID})。每秒有80个请求。

Now say we we have 4 web nodes each receiving 20 requests a second and each of those needs to be recorded as "request::{ID})". That's 80 requests a second.

说节点1和3的网络延迟很小,但是两个节点恰好同时接收到这40个请求中的一个。您的脚本会增加请求计数器(对于本示例来说,当前为1500)并获得ID。现在肯定有可能两个Couchbase实例都可以将1501返回到Web节点1和3,并且两个服务器现在都将尝试将正在处理的请求存储为 request:1501。

Say nodes 1 and 3 are having a tiny bit of network latency but both receive one of these 40 requests at precisely the same time. Your script increments the request counter (lets say for this example it is currently at 1500) and gets an ID. Surely it is now possible that BOTH Couchbase instances could return 1501 to web nodes 1 and 3 and both servers will now attempt to store the request they are dealing with as "request:1501".

现在,复制将处理此问题,实际上,最新的将获胜。但是,您现在已经丢失了一个请求的记录。

Now, replication will deal with this and essentially the latest one will win. But you now have lost the record of one request.

那么,这是否意味着实际上您需要一种更好的键控重要数据的方法,并且需要使用自动增量来绝对值和唯一键生成是在NoSQL集群环境中应该避免的事情吗?

So does that now mean that in reality you need a better way of keying vital data and that using auto increments for absolute values and unique key generation is something that should be avoided in a NoSQL cluster environment?

或者-作为键生成过程的一部分,您可以做些什么吗? 100%可靠。

Or - is there something you can do as part of your key generating procedure that makes this 100% reliable.

也请考虑使用跨数据中心复制的多集群环境。

Please also consider a multiple cluster environment using cross data centre replication too.

谢谢。

迈克

推荐答案

首先根据沙发床文档函数递增减少是集群中的原子。因此,如果使用它们生成自动增量,则所有功能都可以正常工作。

First of all according to couchbase documentation functions increment and decremant are "atomic" within cluster. So if you use them to generate "autoincrement" all should work fine.

但是,如果要确保在将新项目保存到沙发床中时不会覆盖现有项(类似两个Couchbase实例都可能返回1501的情况),您可以对 StoreMode.Add 使用存储操作。因此,如果您同时调用 couchbase.store(StoreMode.Add, request:1501,value),则一个请求将成功完成,其他请求将失败,并且您可以捕获此失败并尝试再次重复该存储操作(并获得新的新密钥的自动递增ID)

But if you want to enshure that while you save new item in the couchbase you will not override existing (situation like "BOTH Couchbase instances could return 1501") you can use store operation with StoreMode.Add. So if you call at the same time couchbase.store(StoreMode.Add, "request:1501",value), one request will finish with success, other will fail and you can catch this "fail" and try to repeat that store operation again (with getting new autoincremented id for new key)

这篇关于使用增量计数器在Couchbase集群中生成唯一密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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