Cassandra TTL intricacies and usage by examples

Insert record with ttl on limited number of column and then update the other column without TTL.cqlsh:examples> INSERT INTO examples.chat (id, message_by, time) VALUES (2,'Mary',toTimestamp(now())) USING TTL 60;cqlsh:examples> select id,message,message_by,ttl(message) as m_ttl, ttl(message_by) as mb_ttl, ttl(time) as t_ttl from examples.chat;id | message | message_by | m_ttl | mb_ttl | t_ttl—-+———-+————+——-+——–+——- 1 | Hi There | John | null | null | null 2 | null | Mary | null | 32 | 32(2 rows)cqlsh:examples> UPDATE examples.chat SET message='Hello' WHERE id=2;cqlsh:examples> select id,message,message_by,ttl(message) as m_ttl, ttl(message_by) as mb_ttl, ttl(time) as t_ttl from examples.chat;id | message | message_by | m_ttl | mb_ttl | t_ttl—-+———-+————+——-+——–+——- 1 | Hi There | John | null | null | null 2 | Hello | Mary | null | 7 | 7(2 rows)cqlsh:examples> select id,message,message_by,ttl(message) as m_ttl, ttl(message_by) as mb_ttl, ttl(time) as t_ttl from examples.chat;id | message | message_by | m_ttl | mb_ttl | t_ttl—-+———-+————+——-+——–+——- 1 | Hi There | John | null | null | null 2 | Hello | null | null | null | nullHere the TTL expired and it removed the column data but then message column did had non-null data, which prevented the whole records from getting deleted..Basically it proves that even if there is a single column with non null value the record stays.What if later on we make that rebel column to null?cqlsh:examples> select id,message,message_by,ttl(message) as m_ttl, ttl(message_by) as mb_ttl, ttl(time) as t_ttl from examples.chat;id | message | message_by | m_ttl | mb_ttl | t_ttl—-+———-+————+——-+——–+——- 1 | Hi There | John | null | null | null 2 | Hello | null | null | null | null(2 rows)cqlsh:examples> UPDATE examples.chat SET message=null WHERE id=2;cqlsh:examples> select id,message,message_by,ttl(message) as m_ttl, ttl(message_by) as mb_ttl, ttl(time) as t_ttl from examples.chat;id | message | message_by | m_ttl | mb_ttl | t_ttl—-+———-+————+——-+——–+——- 1 | Hi There | John | null | null | nullWell, well even the TTL has expired, if the value is set to null later on, It still deletes the record..Frankly I didn’t expect this behavior..so even if ttl is expired it does check for all column null rule at later events.In contrast let us try to try to update the value to null for all the columns to the first row with id 1 which never had any TTLcqlsh:examples> select id,message,message_by,ttl(message) as m_ttl, ttl(message_by) as mb_ttl, ttl(time) as t_ttl from examples.chat;id | message | message_by | m_ttl | mb_ttl | t_ttl—-+———-+————+——-+——–+——- 1 | Hi There | John | null | null | null(1 rows)cqlsh:examples> UPDATE examples.chat SET message=null,message_by=null,time=null WHERE id=1;cqlsh:examples> select id,message,message_by,ttl(message) as m_ttl, ttl(message_by) as mb_ttl, ttl(time) as t_ttl from examples.chat;id | message | message_by | m_ttl | mb_ttl | t_ttl—-+———+————+——-+——–+——- 1 | null | null | null | null | null(1 rows)cqlsh:examples> select * from examples.chat;id | message | message_by | time—-+———+————+—— 1 | null | null | nullWe can assume that TTL enables the record for deletion at events whenever it fulfill all null requirementsOne more important thing to consider is when you insert records with ttl and again update it without ttl, the ttls will be gone on the columns you are updating.cqlsh:examples> INSERT INTO examples.chat (id, message, message_by, time) VALUES (2,'Hello','Mary',toTimestamp(now())) USING TTL 60;cqlsh:examples> select id,message,message_by,ttl(message) as m_ttl, ttl(message_by) as mb_ttl, ttl(time) as t_ttl from examples.chat;id | message | message_by | m_ttl | mb_ttl | t_ttl—-+———-+————+——-+——–+——- 1 | Hi There | John | null | null | null 2 | Hello | Mary | 58 | 58 | 58(2 rows)cqlsh:examples> INSERT INTO examples.chat (id,message,time) VALUES (2,'Hola',toTimestamp(now()));cqlsh:examples> select id,message,message_by,ttl(message) as m_ttl, ttl(message_by) as mb_ttl, ttl(time) as t_ttl from examples.chat;id | message | message_by | m_ttl | mb_ttl | t_ttl—-+———-+————+——-+——–+——- 1 | Hi There | John | null | null | null 2 | Hola | Mary | null | 35 | nullIt’s time to summarize the points we noticed and few more.SummaryTTL is used for expiring records or data.Cassandra TTL are applied at column level.If a record is inserted with TTL, separate TTL will be applied on all the columns mentioned in the insert statement.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.TTL is not applicable on primary columns in Cassandra.When columns with ongoing TTL are updated without TTL values, the existing TTL gets removed from those columns..In other words, One cannot update the value of a column with TTL without altering the TTL, either by new TTL or by removing it.TTL is not applicable for Cassandra column type counter.As closing words I would like to put that, In Cassandra, TTL should be used with caution otherwise it can lead to erratic behavior..The way common examples are provided, it looks like in Cassandra TTL is applied at row level even though it clearly mentions that it is actually column level.Compared to MongoDB which have predefined TTL value at collection level, Cassandra provides more granularity with TTL as different records and column can have different TTL..One key difference however is that Mongo compares the TTL with the field values whereas Cassandra calculates TTL from insert or update time.Above examples were run at Cassandra version 3.11. More details

Leave a Reply