How to set TTL for map entry in aerospike

1.5k views Asked by At

We are having below aerospike set connfiguration and want that entry from the Map should be auto deleted after 'x' days. How should the same be achieved using Spring data aerospike ?

@Document(collection = "cust")
public class Customer {

   @Id
   @Field(value = "PK")
   private String custId;

   @Field(value = "mobileNumber")
   private String mobileNumber;

   @Field(value = "creationTime")
   private String creationTime;
 
   @Field(value = "corrDetails")
   private HashMap<String, Object> corrDetails;
 
}

Moreover there are around 10 - 12 <K,V> pairs in my map with each having different structure, and I want only 1 Particular <K,V> to expire after x days !!

Dependencies looks like :-

<dependency>
            <groupId>com.aerospike</groupId>
            <artifactId>aerospike-client</artifactId>
            <version>4.1.3</version>
        </dependency>

        <dependency>
            <groupId>com.aerospike</groupId>
            <artifactId>spring-data-aerospike</artifactId>
            <version>${aerospike.data.version}</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/spring-data-aerospike-2.0.0.RELEASE.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.aerospike</groupId>
            <artifactId>aerospike-client</artifactId>
        </dependency>
        <dependency>
            <groupId>com.aerospike</groupId>
            <artifactId>aerospike-helper-java</artifactId>
            <version>1.2.2</version>
        </dependency>

2

There are 2 answers

4
pgupta On

There is no native feature/ability within Aerospike server to assign a TTL to a K:V pair in a map data type. You will have to build it from the application. i.e. store the desired expiration timestamp when you insert the K:V pair, with the K:V data, and then periodically scan the entire database to remove expired KV pairs or remove expired K:V pairs on reads (less intensive but carries expired K:V pairs until read) - no elegant option really. Perhaps revisit the data model.

Aerospike does have the ability to expire and remove entire records i.e. assign TTL to an entire record - and then it has an internal thread to do the expiration and removal for you. So if you can store this K:V pair that you want to keep for x days as a separate record, then that is straightforward. But anything at bin level or at bin data level (like map data type), you have to handle from the application.

0
pgupta On

Again, in Aerospike, set name is just a tag on a record. So data in Aerospike across records for example can be like so:

  1. Rec Key=1, data: bin1=5, bin2="abc" and you can specify (optional) -> belongs to "myset1",
  2. Rec Key=2, data: bin1="cdg", bin2=7 and you can specify -> belongs to "myset1",
  3. Rec Key="abc" data: bin1=[list data type], bin2={map data type}, bin3="xyz", bin4=4 and you specify -> belongs to "myset2"

... hope you get the drift. Data schema is purely at the record level, Set name is just a modeling convenience - if you don't specify set, record goes to default "null" set - set name is attached as a "tag" to that particular record. You can then work on all records whose tag (set name) is "myset1". But within Aerospike, all records belonging to different sets in the same namespace are distributed randomly on the storage space for that namespace.