Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modeling account for rest communication cassandra

I need to model account (first name, last name, email as username etc.) in cassandra along with currently active token.

My initial idea was to create account_by_email which will have skinny row partitioned by email with static columns and have clustering by access_token (and maybe TTL) and than you can always find access token based on current email.

But we have requirement that clients will send after login only access_token and based on it current user must be pulled from DB.

I can create one more table where email will be partitioned by access_token but that seams to me as overhead and lot of partitions. Then I could get email from access_token and get user by email always.

Any better ideas and approaches, it seams that this is common use case but I cannot find any modeling details when cassandra is used as storage?

like image 696
Nenad Bozic Avatar asked Jun 03 '15 16:06

Nenad Bozic


1 Answers

I can create one more table where email will be partitioned by access_token but that seams to me as overhead and lot of partitions.

What's wrong with a large number of partitions it a table? It's definitely a correct Cassandra-way of doing things:

create table users (
  email text primary key,
  first_name text,
  last_name text,
  current_token text
);

create table tokens (
  auth_token text primary key,
  valid_until timestamp,
  email text
);

So you have a separate tables for users, and the tokens table which has token as a partition key. With this model you can:

  • set a new token for a user by updating users.current_token with TTL and inserting a new token row to tokens table.
  • get user email by token, even if the token was expired.
  • get current user's active token.
  • have full token history for a user (but no way to run effective queries for that type of information, you can use Spark/SparkSQL/Hive for that).
  • automatically expire tokens by setting TTL for a single current_token column.
like image 67
shutty Avatar answered Oct 22 '22 11:10

shutty