Cassandra - Shall I have to do so many writes?

Viewed 86

I have 5 Tables:

users_by_id
users_by_username
users_by_email
users_by_likes
users_by_followers

I have to write 5 Statements every time if a user registered. Is that not expensive or bad ?

INSERT INTO users_by_id (...) values (..)
INSERT INTO users_by_email (...) values (..)
INSERT INTO users_by_username (...) values (..)
INSERT INTO users_by_likes (...) values (..)
INSERT INTO users_by_followers (...) values (..)

The second question: Maybe I update users_by_id I have to write 5 Update statments. Is there another solution? Or is that not this bad ?

2 Answers

Cassandra advocates denormalization of your data and creating data model according to your queries. You will have to write your data model such that it satisfies all the queries with good performance. For performance (due to its architecture and design) Cassandra asks for writing and reading using partition key.

It is not expensive to write 5 insertions for same set of data in 5 different tables. Your reads will perform better and as data size increases to web scale, you will thank your decision of creating 5 tables and writing to them.

You can explore materialized views (Materialized View and Datastax Link for Materialized View but remember it is an experimental feature. So you have to understand it properly and also identify open issues with materialized views.

I would recommend you study Cassandra data model that will make things easier to grasp.

Cassandra is designed to be write intensive database so do not hesitate to duplicate your data. One should always design tables for the read queries. If one table satisfies one query, it is a fine design.

Answer to your second question, you should design your tables such a way that you do not have to update table. Always think about inserting new values.

For example, below table design

CREATE TABLE user_by_email (
email text,
timestamp timestamp,
name text,
fullname text,
userId text,
PRIMARY KEY (email,timestamp)
) WITH CLUSTERING ORDER BY (timestamp DESC);

INSERT INTO user_by_email (email, DateTime.Now ........)

In this design, you should get the latest inserted value. Additionally , this design keeps change history for that key. Think about, how many times we have to update values like user id, email, username? rarely.

Related