Anil Jadhav
Anil Jadhav

Reputation: 2158

How to update time-to-live(TTL) of existing records in Cassandra DB?

As Cassandra DB is providing a option to set Time-to-live(TTL) value to automatically delete the record on the basis of TTL value set with record. I have a case that user can change the date of data expiry, means user can change the data expiration date any time.

Case:

  1. Suppose default expiration period is 10 days.
  2. Then inserted some records with setting TTL for 10 days.
  3. after 2 days user change expiration period to 30 days.
  4. But the previous data inserted with 10 days TTL period automatically gets deleted after 10 days instead of 30 days.

Requirement: Whenever user changes the expiration period through UI, we wanted to change TTL in database. Is there any option to update the TTL for records in Cassandra DB.

Upvotes: 16

Views: 23087

Answers (3)

Domenico
Domenico

Reputation: 79

This work on Cassandra 2.0.7
You can RE-INSERT the same data changing only TTL.

INSERT INTO Table (col1, col2 ) VALUES ( '001','aaa') USING TTL 200;
INSERT INTO Table (col1, col2 ) VALUES ( '001','aaa') USING TTL 2; Wait 2 sec and the row disappear;

I found also this by DataStax: To change the TTL of a specific column, you must re-insert the data with a new TTL. Apache Cassandra™ upserts the column with the new TTL, replacing the old value with the old TTL, if any exists.

See this link

Upvotes: 1

Anil Jadhav
Anil Jadhav

Reputation: 2158

I did some R&D on this issue and I have concluded following points.

  1. We can increase TTL for any record in Cassandra DB but it needs to reset all fields with update query for individual record.
  2. We can’t reduce TTL for any record at any cost. Even if you update record with less TTL value, the record will remain in database till maximum TTL value which is either old or new. updated value.

Upvotes: 13

Carlo Bertuccini
Carlo Bertuccini

Reputation: 20021

You can perform a "fake update"

INSERT INTO ttl_example (k, v) VALUES ('somekey', 'somevalue') USING TTL 60;
UPDATE ttl_example USING TTL 200 SET v = 'somevalue' WHERE k = 'somekey';

After the execution of second statement TTL will be 200 seconds.

HTH, Carlo

Upvotes: 5

Related Questions