question

rizaldi avatar image
rizaldi asked Erick Ramirez commented

When an order is created, do I need to do multiple inserts to all denormalized tables?

Hii, this is my 2nd day I learn CassandraDB.

I have read this: https://www.datastax.com/learn/data-modeling-by-example/order-management#application

In that article has table:

- oders_by_user

- orders_by_id

- orders_by_user_item

- orders_status_history_by_id

Assuming we receive some "order" from user and this is the part I want to ask:

Do I need inserting all across table when "order" been created?

If so, then the next question is, if My App failed inserting data into table `orders_by_user_item` for whatever reason, how I (as developer) should handle this use case? Considering cassandra doesn't support ACID.

I also have read this article: https://www.datastax.com/blog/lightweight-transactions-cassandra-20, to accomplish with that case, so how the `cql` look like?

Thankyou in advanced.

batch
10 |1000

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 Erick Ramirez commented

You need to use CQL batches to group updates to denormalised tables so they are executed as an atomic operation. It isn't necessary to use lightweight transactions since those are for special cases where you need to do conditional inserts/updates/deletes.

In cqlsh, the batch updates to the tables when an order comes would look like:

BEGIN BATCH
  INSERT INTO orders_by_user (...) VALUES (...);
  INSERT INTO orders_by_id (...) VALUES (...);
  INSERT INTO orders_by_user_item (...) VALUES (...);
  INSERT INTO orders_status_history_by_id (...) VALUES (...);
APPLY BATCH;

I've explained this in a bit more detail in this article -- How to keep data in denormalized tables in sync. Cheers!

3 comments Share
10 |1000

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

rizaldi avatar image rizaldi commented ·

Thankyou Erick! excellent answer

0 Likes 0 ·
rizaldi avatar image rizaldi commented ·
0 Likes 0 ·
Erick Ramirez avatar image Erick Ramirez ♦♦ rizaldi commented ·

My apologies for posting the incorrect link. I've fixed it now. Cheers!

0 Likes 0 ·