You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cassandra.apache.org by Brian Burruss <bb...@real.com> on 2009/12/16 00:19:27 UTC

create only - no update

can the cassandra client (java specifically) specify that a particular "put" should be "create only, do not update"?  If the value already exists in the database, i want the put to fail.  for instance, two users want the exact same username, so they both do a "get" to determine if the username already exists, it doesn't, so they create.  the last one to create wins, correct?

thx

Re: create only - no update

Posted by Ville Lautanala <la...@gmail.com>.
On Dec 16, 2009, at 22:31, gabriele renzi wrote:

> On Wed, Dec 16, 2009 at 12:34 AM, Brandon Williams <dr...@gmail.com> wrote:
>> On Tue, Dec 15, 2009 at 5:19 PM, Brian Burruss <bb...@real.com> wrote:
>>> 
>>> can the cassandra client (java specifically) specify that a particular
>>> "put" should be "create only, do not update"?  If the value already exists
>>> in the database, i want the put to fail.  for instance, two users want the
>>> exact same username, so they both do a "get" to determine if the username
>>> already exists, it doesn't, so they create.  the last one to create wins,
>>> correct?
>> 
>> Correct.  You would need to implement a locking mechanism such as Zookeeper
>>  in your application to get around this.
> 
> sorry for my usual stupid question, but I'm still trying to wrap my
> head around cassandra...
> is there a fundamental reason for not being able to provide a
> putIfAbsent-like abstraction in cassandra?
> 
> If I understand correctly this can't be done in the case of network
> partitioning  but it could work while the network is not split with an
> operation requiring ConsistencyLevel.ALL. But this seems like it would
> be still the case with any other system.
> So, for unique login ids  in the case of partition there would  be a
> temporary impossibility to register but in the (normal?) non
> partitioned case Brian would still be able to let users register via
> cassandra.

Using decrementing timestamps you could get "first insert wins" behavior. Then you'd have to check after insert if it really wrote any data using ConsistencyLevel.ALL.

This is a really bad use case for Cassandra and you really should use some external locking mechanism.

-Ville


Re: create only - no update

Posted by gabriele renzi <rf...@gmail.com>.
On Wed, Dec 16, 2009 at 10:16 PM, Jonathan Ellis <jb...@gmail.com> wrote:
> On Wed, Dec 16, 2009 at 2:31 PM, gabriele renzi <rf...@gmail.com> wrote:
>> is there a fundamental reason for not being able to provide a
>> putIfAbsent-like abstraction in cassandra?
>
> Yes: there is no global master to serialize operations from multiple clients.
>

of course, but it's a similar trade off to eventually consistent
operations, imvho.

e.g., nodes A, B , C and clients x, y

if X gets to write on A, B , C before y everything is ok, and viceversa.

If two clients wrote on [A,B,C] in mixed order, there would be a
conflict when writing on tht others and both would fail, later on when
propagating information the nodes with different conditional puts
could remove both keys due to inconsistency.

The OP's original problem would then be solved by using this step in a
similar way to the first part of 2pc: if the first write succeeded
he'd have gained a lock on key K and could write the actual user
entry.

But yes, this is still better solved in other ways, I understand :)


-- 
blog en: http://www.riffraff.info
blog it: http://riffraff.blogsome.com

Re: create only - no update

Posted by Jonathan Ellis <jb...@gmail.com>.
On Wed, Dec 16, 2009 at 2:31 PM, gabriele renzi <rf...@gmail.com> wrote:
> is there a fundamental reason for not being able to provide a
> putIfAbsent-like abstraction in cassandra?

Yes: there is no global master to serialize operations from multiple clients.

Re: create only - no update

Posted by gabriele renzi <rf...@gmail.com>.
On Wed, Dec 16, 2009 at 12:34 AM, Brandon Williams <dr...@gmail.com> wrote:
> On Tue, Dec 15, 2009 at 5:19 PM, Brian Burruss <bb...@real.com> wrote:
>>
>> can the cassandra client (java specifically) specify that a particular
>> "put" should be "create only, do not update"?  If the value already exists
>> in the database, i want the put to fail.  for instance, two users want the
>> exact same username, so they both do a "get" to determine if the username
>> already exists, it doesn't, so they create.  the last one to create wins,
>> correct?
>
> Correct.  You would need to implement a locking mechanism such as Zookeeper
>  in your application to get around this.

sorry for my usual stupid question, but I'm still trying to wrap my
head around cassandra...
is there a fundamental reason for not being able to provide a
putIfAbsent-like abstraction in cassandra?

If I understand correctly this can't be done in the case of network
partitioning  but it could work while the network is not split with an
operation requiring ConsistencyLevel.ALL. But this seems like it would
be still the case with any other system.
So, for unique login ids  in the case of partition there would  be a
temporary impossibility to register but in the (normal?) non
partitioned case Brian would still be able to let users register via
cassandra.

Re: create only - no update

Posted by Brandon Williams <dr...@gmail.com>.
On Tue, Dec 15, 2009 at 5:19 PM, Brian Burruss <bb...@real.com> wrote:

> can the cassandra client (java specifically) specify that a particular
> "put" should be "create only, do not update"?  If the value already exists
> in the database, i want the put to fail.  for instance, two users want the
> exact same username, so they both do a "get" to determine if the username
> already exists, it doesn't, so they create.  the last one to create wins,
> correct?
>

Correct.  You would need to implement a locking mechanism such as Zookeeper
 in your application to get around this.

-Brandon