Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

376752150_179413 avatar image
376752150_179413 asked ·

How to model account logic? How to check if an email has already been used

Say, I want to create an account system. there is a user id, user name, user email and other fields.

I have the following "find_by" scenarios, so I will also have some "find_by" tables such as account_by_name, account_by_email.

When create an account, I need to check whether the name or email has already been used or not.

Considering the restrictions of batch and lwt, how to accomplish the logic above

data modelling
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Erick Ramirez avatar image
Erick Ramirez answered ·

@376752150_179413 Since your app query is based on the email address, you would include a conditional insert in your batch like this:

BEGIN BATCH
  INSERT INTO account_by_email (email, name, ...)
    VALUES (...) IF NOT EXISTS;
  INSERT INTO account_by_name (...) VALUES (...);
  ...
APPLY BATCH;

If the email account has not been used already (IF NOT EXISTS), then a new partition is inserted to the account_by_email table and the rest of the insert statements in the batch will get applied.

When the conditional insert (LWT) returns false, the entire batch fails meaning none of the statements will be executed.

For more info, see the BATCH CQL page and Batching data insertion and updates. Cheers!

1 comment Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

thanks Erick for your reply. However, as the doc says,

"a batch containing conditional updates can only operate within a single partition", so this way seems not work.

0 Likes 0 · ·