Setting the time-to-live (TTL) for value

To set the TTL for data, use the USING TTL clause. The TTL function may be used to retrieve the TTL information.

The USING TTL clause can be used to insert or update data into a table for a specific duration of time. To determine the current time-to-live for a record, use the TTL function.

The database storage engine can only encode TTL timestamps through January 19 2038 03:14:07 UTC due to the Year 2038 problem. The TTL date overflow policy determines whether requests with expiration timestamps later than the maximum date are rejected or inserted. See -Dcassandra.expiration_date_overflow_policy.

  • Insert data into the table cycling.calendar and use the USING TTL clause to set the expiration period to 200 seconds.

    INSERT INTO cycling.calendar (
      race_id, race_name, race_start_date, race_end_date
    ) VALUES (
      200, 'placeholder', '2015-05-27', '2015-05-27'
    )
    USING TTL 200;
  • Issue a SELECT statement to determine how much longer the data has to live.

    SELECT TTL(race_name) FROM cycling.calendar WHERE race_id = 200;
 ttl(race_name)
----------------
            198

(1 rows)

+ If you repeat this step after some time, the time-to-live value will decrease.

  • The time-to-live value can also be updated with the USING TTL clause in an UPDATE statement.

    UPDATE cycling.calendar USING TTL 300 
    SET race_name = 'Tour de France - Stage 12' 
    WHERE race_id = 200 
      AND race_start_date = '2015-05-27' 
      AND race_end_date = '2015-05-27';
 ttl(race_name)
----------------
            298

(1 rows)

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com