Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra ttl on a row

Tags:

cassandra

row

ttl

I know that there are TTLs on columns in Cassandra. But is it also possible to set a TTL on a row? Setting a TTL on each column doesn't solve my problem as can be seen in the following usecase:

At some point a process wants to delete a complete row with a TTL (let's say row "A" with TTL 1 week). It could do this by replacing all existing columns with the same content but with a TTL of 1 week.

But there may be another process running concurrently on that row "A" which inserts new columns or replaces existing ones without a TTL because that process can't know that the row is to be deleted (it runs concurrently!). So after 1 week all columns of row "A" will be deleted because of the TTL except for these newly inserted ones. And I also want them to be deleted.

So is there or will there be Cassandra support for this use case or do I have to implement something on my own?

Kind Regards
Stefan

like image 610
snd Avatar asked May 14 '13 13:05

snd


People also ask

How does TTL work in Cassandra?

In Cassandra Both the INSERT and UPDATE commands support setting a time for data in a column to expire. It is used to set the time limit for a specific period of time. By USING TTL clause we can set the TTL value at the time of insertion. We can use TTL function to get the time remaining for a specific selected query.

How do I change TTL in Cassandra?

Use CQL to set the TTL. To change the TTL of a specific column, you must re-insert the data with a new TTL. Cassandra upserts the column with the new TTL. To remove TTL from a column, set TTL to zero.

What happens when TTL expires in Cassandra?

Whenever a TTL is expired in Cassandra for a column it checks for all the non primary column values in the record, if all values are null, record gets deleted. Even after TTL is expired in Cassandra and at later point all the non primary column value turns null, the record gets deleted.


3 Answers

There is no way of setting a TTL on a row in Cassandra currently. TTLs are designed for deleting individual columns when their lifetime is known when they are written.

You could achieve what you want by delaying your process - instead of wanting to insert a TTL of 1 week, run it a week later and delete the row. Row deletes have the following semantics: any column inserted just before will get deleted but columns inserted just after won't be.

If columns that are inserted in the future still need to be deleted you could insert a row delete with a timestamp in the future to ensure this but be very careful: if you later wanted to insert into that row you couldn't, columns would just disappear when written to that row (until the tombstone is garbage collected).

like image 195
Richard Avatar answered Nov 13 '22 09:11

Richard


You can set ttl for a row in Cassandra 3 using

INSERT INTO Counter(key,eventTime,value) VALUES ('1001',dateof(now()),100) USING ttl 10;
like image 21
Mahesh Reddy Avatar answered Nov 13 '22 09:11

Mahesh Reddy


Although I do not recommend such, there is a Cassandra way to fix the problem:

SELECT TTL(value) FROM table WHERE ...;

Get the current TTL of a value first, then use the result to set the TTL in an INSERT or UPDATE:

INSERT ... USING TTL ttl-of-value;

So... I think that the SELECT TTL() is slow (from experience with TTL() and WRITETIME() in some of my CQL commands). Not only that, the TTL is correct at the time the select results are generated on the Cassandra node, but by the time the insert happens, it will be off. Cassandra should have offered a time to delete rather than a time to live...

So as mentioned by Richard, having your own process to delete data after 1 week is probably safer. You should have one column to save the date of creation or the date when the data becomes obsolete. Then a background process can read that date and if the data is viewed as obsolete, drop the entire row.

Other processes can also use that date to know whether that row is considered valid or not! (so even if it was not yet deleted, you can still view the row as invalid if the date is passed.)

like image 31
Alexis Wilke Avatar answered Nov 13 '22 09:11

Alexis Wilke