Expiring data

Data in a column, other than a counter column, can have an optional expiration period called TTL (time to live).

You can set an optional expiration period called TTL (time to live) for data in a column, other than a counter column. The TTL value for a column is a number of seconds. After the number of seconds since the column's creation exceeds the TTL value, TTL data is considered expired and is included in results. Expired data is marked with a tombstone after on the next read on the read path, but it remains for a maximum of gc_grace_seconds. After this amount of time, the tombstoned data is automatically removed during the normal compaction and repair processes.

Setting a TTL for a specific column

Use CQL to set the TTL for data.

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, replacing the old value with the old TTL, if any exists.

Setting a TTL for a table

The CQL table definition supports the default_time_to_live property, which applies a specific TTL to each column in the table. After the default_time_to_live TTL value has been exceed, Cassandra tombstones the entire table. Apply this default TTL to a table in CQL using CREATE TABLE or ALTER TABLE.

Notes about TTLs

  • A TTL has a precision of one second, as calculated on the server. A very small TTL is not very useful.

  • The use of TTL makes it important that the clocks on the servers are kept synchronized. If they are not, reduced precision results, because the expiration time is computed on the primary host that receives the initial insertion but is then interpreted by other hosts on the cluster.
  • You can delete a column's TTL by setting it to zero. For details, see the UPDATE documentation.
  • Expiring data has an additional overhead of 8 bytes in memory and on disk (to record the TTL and expiration time) compared to standard data.

For details and examples, see Expiring data with TTL example.