Was this page helpful?
Caution
You're viewing documentation for an unstable version of ScyllaDB Enterprise. Switch to the latest stable version.
ScyllaDB (as well as Apache Cassandra) provides the functionality to automatically delete expired data according to the Time to Live (or TTL) value. TTL is measured in seconds. If the field is not updated within the TTL it is deleted. The TTL can be set when defining a Table (CREATE), or when using the INSERT and UPDATE queries. The expiration works at the individual column level, which provides a lot of flexibility. By default, the TTL value is null, which means that the data will not expire.
Note
The expiration time is always calculated as now() on the Coordinator + TTL where, now() is the wall clock during the corresponding write operation. In particular, it means that a value given via USING TIMESTAMP is not taken into an account for an expiration calculation.
To set the TTL value using the UPDATE query use the following command:
UPDATE heartrate USING TTL 600 SET heart_rate =
110 WHERE pet_chip_id = 123e4567-e89b-12d3-a456-426655440b23;
In this case, the TTL for the heart_rate column is set 10 minutes (600 seconds).
To check the TTL, use the TTL()
function:
SELECT name, heart_rate, TTL(heart_rate)
FROM heartrate WHERE pet_chip_id = 123e4567-e89b-12d3-a456-426655440b23;
The TTL has a value that is lower than 600 as a few seconds passed between setting the TTL and the SELECT query. If you wait 10 minutes and run this command again, you will get a null value for the heart_rate.
It’s also possible to set the TTL when performing an INSERT. To do this use:
INSERT INTO heartrate(pet_chip_id, name, heart_rate) VALUES (c63e71f0-936e-11ea-bb37-0242ac130002, 'Rocky', 87) USING TTL 30;
In this case, a TTL of 30 seconds is set.
Use the CREATE TABLE or ALTER TABLE commands and set the default_time_to_live value:
CREATE TABLE heartrate_ttl (
pet_chip_id uuid,
name text,
heart_rate int,
PRIMARY KEY (pet_chip_id))
WITH default_time_to_live = 600;
Here a TTL of 10 minutes is applied to all rows, however, keep in mind that TTL is stored on a per column level for non-primary key columns.
It’s also possible to change the default_time_to_live on an existing table using the ALTER command:
ALTER TABLE heartrate_ttl WITH default_time_to_live = 3600;
Note
TTL on records in system.paxos table is set with the paxos_grace_seconds
value.
If this value is not set, the value from gc_grace_seconds
is used.
The default for gc_grace_seconds
and paxos_grace_seconds
are both the same (10 days).
You can have two different settings for paxos_grace_seconds
and gc_grace_seconds
.
You can change the paxos_grace_seconds
value by altering the system.paxos table.
Refer to LWT for more information.
The gc_grace_seconds
parameter is defined here.
You can set the TTL on a per element basis for collections. An example of how to do this, is shown in the Maps CQL Reference.
Notice that setting the TTL on a column using UPDATE or INSERT overrides the default_time_to_live set at the Table level.
The TTL is determined by the coordinator node. When using TTL, make sure that all the nodes in the cluster have synchronized clocks.
When using TTL for a table, consider using the TWCS compaction strategy.
ScyllaDB defines TTL on a per column basis, for non-primary key columns. It’s impossible to set the TTL for the entire row after an initial insert; instead, you can reinsert the row (which is actually an upsert).
TTL can not be defined for counter columns.
To remove the TTL, set it to 0.
To learn more about TTL, and see a hands-on example, check out this lesson on ScyllaDB University.
Was this page helpful?