tags:

views:

45

answers:

3

How can I implement incr/decr on top of a key/value store?

I'm using a key value store that doesn't support incr and decr though which is why I want to create this. I have used Redis and Memcached incr and decr, so as mentioned in some of the answers then this is a perfect example of how I want the incr and decr to behave, so thanks to those who mentioned this.

A: 

Memcache has this functionality built in

edit: it looks like you're not going to get an atomic update without updating the source, as there doesn't appear to be a lock function. If there is (and this is not pretty), you can lock the value, get it, increment it in your application, put it, and unlock it. Suboptimal though.

Andy
Thanks, I have amended the question
Zubair
+1  A: 

The point of having a incr() function is it's all internal to the store. You don't have to pull data out and push it back in.

What you're doing sounds like you want to put some logic in your code that pulls the data out, increments it and pushes it back in... While it's not very hard (I think I've just described how you'd do it), it does defeat the point somewhat.

To get the benefit you'd need to change the source of your key store. Might be easy.

But a lot of caches already have this. If you really need this for speed, perhaps you should find an alternate store like memcached that does support it.

Oli
+1  A: 

it kind of seems like without a compareAndSet then you are out of luck. But it will help to consider the problem from another angle. For example, if you were implementing an atomic counter that shows the number of upvotes for a question, then one way would be to have a "table" per question and to put a +1 for each upvote and -1 for each downvote. Then to "get" you would sum the "table". For this to work I assume "tables" are inexpensive and you don't care how long "get" takes to compute, you only mentioned incr/decr.

Ron
I like this solution, and it seems like the most likely to work
Zubair
although it wouldn't work if it had to retreive a unique counter
Zubair
if you can say what other atomic operations are available we could probably piece it together. is this an eventually consistent nosql key value store?
Ron
Yes, it is Riak
Zubair