5

It appears you cant do transactions in riak. How does one ensure data is correct?

Lets say we want to insert a comments. In redis i'd do

commentId=incr commentCount
multi
SET comment_post:commentId postId //for later use when we flag comments. We'll need to know where in the db it is
RPUSH post_comment:postId binaryValue //contains commentId in it + comment body
exec

In sql i'd insert a new row in a comment table with the text and post id. Both use more then one statement. How do i both insert the comment body and associate the post to the comment in riak as it has no transactions?

Another problem is if i modify a post. How do i update the post and update the tag list of post using that tag

1 Answers1

6

Riak is an eventually consistent system designed to be write available and has no concept of atomicity; there are no atomic counters or transactions.

Edit: We released counters using CRDTs in Riak 1.4. For details see Counters in Riak 1.4

The approach used by Riak is based on the Amazon Dynamo paper and uses vector clocks for conflict resolution; it's a read/modify/write cycle with any conflict resolution performed during a subsequent read. At any given time you can have multiple values for a given key that will all be returned when doing a fetch.

it sounds like you want to look at our secondary indexes (if I'm understanding your scenario; the fact that you need a new commentId for a 'like' is somewhat confusing without knowing more). You would create a secondary index that pointed at your comment body that gets sent along with the body itself when you do a store. That solves at least one half of the problem.

How you get your commentId is a little trickier. You can't use a global atomic counter because we don't have them. Your design would need to either not rely on sequential IDs (perhaps generate UUIDs?) and perhaps use our links ?

If you're interested and would like to discuss possible design approaches, please ping us on the riak-users mailing list - all of our engineers read it and are always happy to answer questions.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • I don't know much about riak as i took a brief look at it. When I know the basics or more i'll post there but not right now. I want to get a general idea what its suitable for as i found out redis is bad at being a db since it has to be able to fit everything in memory. I don't know a thing about `vector clocks` but you just said i can have multiple values for a key which sounds weird but i'll pretend riak is pretty efficient. So riak is designed to have all consistency logic done by the app? Thats interesting –  Sep 09 '12 at 03:47
  • I did a bit of reading. I'm a bit confused bc the example showed setting allow_mult being set to true. So what happens if i have something that has a write conflict and is false... So it sounds like everything has a vclock in it? It appears you making write available high. I can see the use and tradeoff there. I can imagine how to resolve and correct things for my needs but i cant imagine/think of what happens if i didnt set multis and it is false? Will one overwrite the other? Will it fail? I can see how to handle things if it fails (but i'll have to undo other writes) –  Sep 09 '12 at 04:18
  • but i cant see what happens if it doesnt fail and one job finished while the other job started and finished a bit after it and overwrote data. –  Sep 09 '12 at 04:22