You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cassandra.apache.org by John Stager <jo...@gmail.com> on 2014/02/25 04:00:17 UTC

Cassandra Data Model and Surrogate Keys

Hello,

We are working on our data model for Cassandra and we have a need to use
surrogate keys (timeuuid) and not the natural key for one of our tables but
this causes a possible timing issue when determining if a row already
exists.Are there any best practices or patterns for Cassandra for using
surrogate keys?

This is the classic user id (surrogate key) and username (email address).

Thanks

-- 
John W Stager

Re: Cassandra Data Model and Surrogate Keys

Posted by Daniel Chia <da...@coursera.org>.
If you look at https://github.com/Netflix/astyanax/wiki , they have both
distributed lock, as well as a uniqueness constraint recipe.

Once you get past the thrift/astyanax style code, the underlying principle
is fairly simple which you should be able to port it to CQL3.

Thanks,
Daniel


On Tue, Feb 25, 2014 at 1:26 PM, John Stager <jo...@gmail.com> wrote:

>
> Hello Daniel,
>
> That is correct this is for user creation and collisions should be rare.
> Is the astyanax recipe a distributed lock? I do not understand what you
> mean by a combination of reads and writes?
>
> We are using cql3 with the Datastax java driver
>
> Thanks
>   *From: *Daniel Chia
> *Sent: *Tuesday, February 25, 2014 2:02 PM
> *To: *user@cassandra.apache.org
> *Reply To: *user@cassandra.apache.org
> *Subject: *Re: Cassandra Data Model and Surrogate Keys
>
> Hi John,
>
> Since this is presumably for creation of users, which should have low
> activity per user, you can use a combination of reads / writes to acquire a
> "lock" on the username. I believe astyanax has a recipe with what you want
> that works for C1.2 (although you might have to port it to CQL3 if that's
> what you're using).
>
> Thanks,
> Daniel
>
>
> On Tue, Feb 25, 2014 at 5:41 AM, <jo...@gmail.com> wrote:
>
>> Thanks again Michael. Those are the conclusions that I came to as well.
>> For us the window is small for possible duplicate users so I think we will
>> have to do the read before like you suggested. We will also have to be able
>> to handle the case where the duplicate users exist.
>>
>> And thanks for the example cql.
>>
>> Thanks
>>
>> Sent from my BlackBerry 10 smartphone on the Rogers network.
>>   Original Message
>> From: Michael Shuler
>> Sent: Monday, February 24, 2014 11:53 PM
>> To: user@cassandra.apache.org
>> Reply To: user@cassandra.apache.org
>> Subject: Re: Cassandra Data Model and Surrogate Keys
>>
>> On 02/24/2014 09:24 PM, john.stager@gmail.com wrote:
>> > Thanks Michael, I will take a look at LWT for the future but
>> > unfortunately we are using Cassandra 1.2 ( I should have stated that,
>> > sorry). Are there any recommendations for 1.2, or do you just have to
>> > deal with him the race condition and possible duplicate data.
>>
>> I think you would need to try to perform a best effort read before write
>> in your application. I guess it depends on the amount of traffic for the
>> table. Even then, there is a small race window in that turnaround time.
>> A nasty alternative would be a distributed lock manager, or you could
>> upgrade to 2.0, if that's possible, which would be easier than messing
>> around with locking. Someone may correct me, if there are better
>> alternatives. I simple example attached.
>>
>> Michael
>>
>
>
>
>
>
>
> --
> John W Stager
>

Fwd: Cassandra Data Model and Surrogate Keys

Posted by John Stager <jo...@gmail.com>.
Hello Daniel,

That is correct this is for user creation and collisions should be rare. Is
the astyanax recipe a distributed lock? I do not understand what you mean
by a combination of reads and writes?

We are using cql3 with the Datastax java driver

Thanks
  *From: *Daniel Chia
*Sent: *Tuesday, February 25, 2014 2:02 PM
*To: *user@cassandra.apache.org
*Reply To: *user@cassandra.apache.org
*Subject: *Re: Cassandra Data Model and Surrogate Keys

Hi John,

Since this is presumably for creation of users, which should have low
activity per user, you can use a combination of reads / writes to acquire a
"lock" on the username. I believe astyanax has a recipe with what you want
that works for C1.2 (although you might have to port it to CQL3 if that's
what you're using).

Thanks,
Daniel


On Tue, Feb 25, 2014 at 5:41 AM, <jo...@gmail.com> wrote:

> Thanks again Michael. Those are the conclusions that I came to as well.
> For us the window is small for possible duplicate users so I think we will
> have to do the read before like you suggested. We will also have to be able
> to handle the case where the duplicate users exist.
>
> And thanks for the example cql.
>
> Thanks
>
> Sent from my BlackBerry 10 smartphone on the Rogers network.
>   Original Message
> From: Michael Shuler
> Sent: Monday, February 24, 2014 11:53 PM
> To: user@cassandra.apache.org
> Reply To: user@cassandra.apache.org
> Subject: Re: Cassandra Data Model and Surrogate Keys
>
> On 02/24/2014 09:24 PM, john.stager@gmail.com wrote:
> > Thanks Michael, I will take a look at LWT for the future but
> > unfortunately we are using Cassandra 1.2 ( I should have stated that,
> > sorry). Are there any recommendations for 1.2, or do you just have to
> > deal with him the race condition and possible duplicate data.
>
> I think you would need to try to perform a best effort read before write
> in your application. I guess it depends on the amount of traffic for the
> table. Even then, there is a small race window in that turnaround time.
> A nasty alternative would be a distributed lock manager, or you could
> upgrade to 2.0, if that's possible, which would be easier than messing
> around with locking. Someone may correct me, if there are better
> alternatives. I simple example attached.
>
> Michael
>






-- 
John W Stager

Re: Cassandra Data Model and Surrogate Keys

Posted by Daniel Chia <da...@coursera.org>.
Hi John,

Since this is presumably for creation of users, which should have low
activity per user, you can use a combination of reads / writes to acquire a
"lock" on the username. I believe astyanax has a recipe with what you want
that works for C1.2 (although you might have to port it to CQL3 if that's
what you're using).

Thanks,
Daniel


On Tue, Feb 25, 2014 at 5:41 AM, <jo...@gmail.com> wrote:

> Thanks again Michael. Those are the conclusions that I came to as well.
> For us the window is small for possible duplicate users so I think we will
> have to do the read before like you suggested. We will also have to be able
> to handle the case where the duplicate users exist.
>
> And thanks for the example cql.
>
> Thanks
>
> Sent from my BlackBerry 10 smartphone on the Rogers network.
>   Original Message
> From: Michael Shuler
> Sent: Monday, February 24, 2014 11:53 PM
> To: user@cassandra.apache.org
> Reply To: user@cassandra.apache.org
> Subject: Re: Cassandra Data Model and Surrogate Keys
>
> On 02/24/2014 09:24 PM, john.stager@gmail.com wrote:
> > Thanks Michael, I will take a look at LWT for the future but
> > unfortunately we are using Cassandra 1.2 ( I should have stated that,
> > sorry). Are there any recommendations for 1.2, or do you just have to
> > deal with him the race condition and possible duplicate data.
>
> I think you would need to try to perform a best effort read before write
> in your application. I guess it depends on the amount of traffic for the
> table. Even then, there is a small race window in that turnaround time.
> A nasty alternative would be a distributed lock manager, or you could
> upgrade to 2.0, if that's possible, which would be easier than messing
> around with locking. Someone may correct me, if there are better
> alternatives. I simple example attached.
>
> Michael
>

Re: Cassandra Data Model and Surrogate Keys

Posted by jo...@gmail.com.
Thanks again Michael. Those are the conclusions that I came to as well. For us the window is small for possible duplicate users so I think we will have to do the read before like you suggested. We will also have to be able to handle the case where the duplicate users exist. 

And thanks for the example cql. ‎

Thanks‎

Sent from my BlackBerry 10 smartphone on the Rogers network.
  Original Message  
From: Michael Shuler
Sent: Monday, February 24, 2014 11:53 PM
To: user@cassandra.apache.org
Reply To: user@cassandra.apache.org
Subject: Re: Cassandra Data Model and Surrogate Keys

On 02/24/2014 09:24 PM, john.stager@gmail.com wrote:
> Thanks Michael, I will take a look at LWT for the future but
> unfortunately we are using Cassandra 1.2 ( I should have stated that,
> sorry). Are there any recommendations for 1.2, or do you just have to
> deal with him the race condition and possible duplicate data.

I think you would need to try to perform a best effort read before write 
in your application. I guess it depends on the amount of traffic for the 
table. Even then, there is a small race window in that turnaround time. 
A nasty alternative would be a distributed lock manager, or you could 
upgrade to 2.0, if that's possible, which would be easier than messing 
around with locking. Someone may correct me, if there are better 
alternatives. I simple example attached.

Michael

Re: Cassandra Data Model and Surrogate Keys

Posted by Michael Shuler <mi...@pbandjelly.org>.
On 02/24/2014 09:24 PM, john.stager@gmail.com wrote:
> Thanks Michael, I will take a look at LWT for the future but
> unfortunately we are using Cassandra 1.2 ( I should have stated that,
> sorry). Are there any recommendations for 1.2, or do you just have to
> deal with him the race condition and possible duplicate data.

I think you would need to try to perform a best effort read before write 
in your application. I guess it depends on the amount of traffic for the 
table. Even then, there is a small race window in that turnaround time. 
A nasty alternative would be a distributed lock manager, or you could 
upgrade to 2.0, if that's possible, which would be easier than messing 
around with locking. Someone may correct me, if there are better 
alternatives. I simple example attached.

Michael

Re: Cassandra Data Model and Surrogate Keys

Posted by jo...@gmail.com.
Thanks Michael, I will take a look at LWT for the future but unfortunately we are using Cassandra 1.2 ( I should have stated that, sorry). Are there any recommendations for 1.2, or do you just have to deal with him the race condition and possible duplicate data. 

Thanks again

Sent from my BlackBerry 10 smartphone on the Rogers network.
  Original Message  
From: Michael Shuler
Sent: Monday, February 24, 2014 10:17 PM
To: user@cassandra.apache.org
Reply To: user@cassandra.apache.org
Subject: Re: Cassandra Data Model and Surrogate Keys

On 02/24/2014 09:00 PM, John Stager wrote:
> Hello,
>
> We are working on our data model for Cassandra and we have a need to use
> surrogate keys (timeuuid) and not the natural key for one of our tables
> but this causes a possible timing issue when determining if a row
> already exists.Are there any best practices or patterns for Cassandra
> for using surrogate keys?
>
> This is the classic user id (surrogate key) and username (email address).

This sounds like the perfect use case for lightweight transactions and 
just using timeuuid. LWT are more expensive due to read before write, 
but when needed, as in your case, it's the right fit. Even with 
perfectly time-synced nodes, there is still a race for two users to 
create the same userid, which is why LWT was included.

http://www.datastax.com/dev/blog/lightweight-transactions-in-cassandra-2-0
http://www.datastax.com/documentation/cassandra/2.0/cassandra/dml/dml_ltwt_transaction_c.html

-- 
Kind regards,
Michael

Re: Cassandra Data Model and Surrogate Keys

Posted by Michael Shuler <mi...@pbandjelly.org>.
On 02/24/2014 09:00 PM, John Stager wrote:
> Hello,
>
> We are working on our data model for Cassandra and we have a need to use
> surrogate keys (timeuuid) and not the natural key for one of our tables
> but this causes a possible timing issue when determining if a row
> already exists.Are there any best practices or patterns for Cassandra
> for using surrogate keys?
>
> This is the classic user id (surrogate key) and username (email address).

This sounds like the perfect use case for lightweight transactions and 
just using timeuuid. LWT are more expensive due to read before write, 
but when needed, as in your case, it's the right fit. Even with 
perfectly time-synced nodes, there is still a race for two users to 
create the same userid, which is why LWT was included.

http://www.datastax.com/dev/blog/lightweight-transactions-in-cassandra-2-0
http://www.datastax.com/documentation/cassandra/2.0/cassandra/dml/dml_ltwt_transaction_c.html

-- 
Kind regards,
Michael