0

Below is the query using gocql driver:

    insertQueryString = "INSERT INTO my_table (id_1, id_2, textDetails) " + "VALUES (?, ?, ?) IF NOT EXISTS"
    cassandra.Session.Query(insertQueryString, 
    id1,                            
    id2,        
    myTextDetails).Exec();

Table schema in Cassandra 3.0 is:

CREATE      TABLE  IF    NOT    EXISTS my_table (
  id_1                   UUID,
  id_2                   UUID,
  textDetails            TEXT,
  PRIMARY KEY (id_1, id_2)
);

We have a scenario for above INSERT query initiated on multiple Go-routines(http handler) with same column value combination of (id_1, id_2), then,

Does Cassandra ensures that only first INSERT operation is executed successfully? and errors out the subsequent INSERT requests with existing value combination of (id_1, id_2) in database

Volker
  • 40,468
  • 7
  • 81
  • 87
overexchange
  • 15,768
  • 30
  • 152
  • 347

1 Answers1

1

There's a common saying with regard to unique PRIMARY KEYs and concurrent operations in Cassandra:

"Last write wins."

Does Cassandra ensures that only first INSERT operation is executed successfully? and errors out the subsequent INSERT requests with existing value combination of (id_1, id_2) in database

Usually, no. Cassandra typically ensures that all operations for a particular PRIMARY KEY(s) will be executed successfully.

However, in this case whereby using a lightweight transaction, the answer is "yes." In this case, the first write which makes it in will "win," and the IF NOT EXISTS clause will prevent the other from writes from succeeding.

FYI - You'll take a bit of a hit in performance, as a lightweight transaction makes multiple round trips to the database.

Aaron
  • 55,518
  • 11
  • 116
  • 132