Search code examples
javadatabaseaerospike

How to auto generate an Aerospike primary key?


I have a class Student

class Student{
   @Id
   private int id;
}

I want to have autogenerated Id for my key id when I am saving this into Aerospike using Spring Data Aerospike.


Solution

  • Due to it's performant and distributed nature, Aerospike doesn't have an equivalent of an AUTO_INCREMENT integer primary keys which are common in relational databases.

    If your data doesn't have a natural primary key, then a common approach is that your app should generate GUIDs as the key for your data. If you are still worried about collisions, then you can apply a Write Policy with CREATE_ONLY of true which will cause a write to fail if there is a collision, which will allow you to regenerate a new GUID and retry.