0

I am new to Cassandra. I am looking at many examples online. Here is one from JHipster Cassandra examples on GitHub:

https://gist.github.com/jdubois/c3d3bedb869466731316

enter image description here

The repository save(user) method does a read (to look for existence) then a delete and re-insert of the existing user across all the denormalized tables whenever the user data changed.

Is this best practice? Is this only because of how the data model for this sample is designed? Is this sample's design a result of twisting a POJO framework into a NoSQL database design?

When would I want to just do a update in Cassandra? It supports updates at the field-level, so it seems like that would be preferred.

Jason
  • 2,006
  • 3
  • 21
  • 36

2 Answers2

0

First of all, the delete operations should be part of the batch for more robust error handling. But it looks like there are also some concurrency issues with the code. It will update the user based on the current user value read before. It's not save to assume this will still be the latest value while save() is actually executed. It will also just overwrite any keys in the lookup table that might be in use for a different user at that point. E.g. the login could already exist for another user while executing insertByLoginStmt.

Stefan Podkowinski
  • 5,206
  • 1
  • 20
  • 25
  • Thanks for the overview of some flaws evident in this online example. I will remember them when I move forward with my own persistence layer... That said. Do you have any comments on the real question of "editing: delete & insert vs. update" ? – Jason May 22 '15 at 16:19
  • Deletes should be avoided whenever possible and you should try to [model around it](https://lostechies.com/ryansvihla/2014/10/20/domain-modeling-around-deletes-or-using-cassandra-as-a-queue-even-when-you-know-better/). As for inserts vs. updates there's only [subtle differences](https://stackoverflow.com/questions/16532227/difference-between-update-and-insert-in-cassandra) between them anyway, so they basically work the same. – Stefan Podkowinski May 22 '15 at 17:54
0

It is not necessary to delete a row before inserting a new one. But if you are replacing rows and new columns are different from existing columns then you need to delete all existing columns and insert new columns. Or insert new and delete old, does not matter if happens in batch.

relgames
  • 1,356
  • 1
  • 16
  • 34