1

I am using Cassandra as the DB, I want to insert a serial number for every record in sequential form for every record, such that every record is unique. So that even if the application crashes, after the restart if any record is inserted then the serial number is the latest one. I have looked for it but haven't found any solution for Cassandra.

The solution I thought of is to get the count(*) of the table and then inserting record with incremented value by 1. But getting count does not seem a good approach as overtime the number of records will be far higher.

devCodePro
  • 97
  • 2
  • 4
  • 17
  • this may help https://stackoverflow.com/questions/3935915/how-to-create-auto-increment-ids-in-cassandra – deadshot Jul 15 '20 at 11:31
  • Pointing this question out to my new engineers and interns today, as this brings up _several_ good discussion points about how a distributed system works (and doesn't). – Aaron Jul 15 '20 at 12:26

1 Answers1

3

Trying to create a sequential key like this in Cassandra isn't a good idea as Cassandra is a highly available distributed database that generally sacrifices consistency for availability. 'Read before write' (getting a count(*) and then inserting a record) is considered an anti-pattern in Cassandra due to consistency issues. It's not safe to modify data based on a read, as that data could have been changed by another process during the read.

A viable solution to this problem would be to use a TimeUUID. If generated correctly, the IDs will all be unique and as a bonus can also be ordered by time. Check https://cwiki.apache.org/confluence/display/CASSANDRA2/TimeBaseUUIDNotes for more info. There are also plenty of answers on how to create a TimeUUID out there.

SevvyP
  • 375
  • 1
  • 7
  • Adding to this also that Cassandra inserts and updates are bundled into "upserts," so a potential outcome of trying to create a unique key with a read before write would be data getting completely overwritten if two processes write records with a non unique ID – SevvyP Jul 15 '20 at 13:45