docs/kb/ttl-facts.rst
Topic: TTL, gc_grace_seconds, and Compaction
Learn: How data is removed when using TTL
Audience: All
It is not always clear under which circumstances data is deleted when using Time to Live (TTL) and :ref:gc_grace_seconds <gc_grace_seconds> arguments in table definitions.
This article clarifies what may not be apparent.
It corrects some assumptions you may have that are not exactly true.
This document is about CQL's :doc:per-write TTL feature </cql/time-to-live>,
the per-row TTL <https://docs.scylladb.com/stable/cql/cql-extensions.html#per-row-ttl>_
feature behaves differently.
#. When compaction is running on an SSTable and it scans a piece of data that has expired the following happens:
data time stamp + gc_grace_seconds is less than or equal to the current time (now), the data is thrown away and a tombstone is not created.For example:
In this example there is an expired cell that becomes a tombstone, note how the tombstone's local_deletion_time is the expired cell's timestamp.
.. code-block:: none
"rows" : [
{
"type" : "row",
"position" : 120,
"liveness_info" : { "tstamp" : "2017-04-09T17:07:12.702597Z",
"ttl" : 20, "expires_at" : "2017-04-09T17:07:32Z", "expired" : true },
"cells" : [
{ "name" : "country", "value" : "1" },
]
Is compacted into:
.. code-block:: none
"rows" : [
{
"type" : "row",
"position" : 79,
"cells" : [
{ "name" : "country", "deletion_info" :
{ "local_delete_time" : "2017-04-09T17:07:12Z" },
"tstamp" : "2017-04-09T17:07:12.702597Z"
},
]
#. If you set the TTL to be greater than the gc_grace_seconds, the expired data will never generate a tombstone.
Example: When Time Window Compaction Strategy (TWCS) estimates if a particular SSTable can be deleted it is going to treat expired data similarly as a tombstone:
.. code-block:: none
gc grace seconds: 0
sstable A: token range: [10, 10], timestamp range: [1, 1], ttl: 2, max deletion time: 3 (1+2)
sstable B: token range: [10, 10], timestamp range: [1, 5], ttl: 2, max deletion time: 7 (5+2)
now: 6
From this you can see that:
During Compaction:
#. If you use updates in conjunction with TWCS, it may prevent the "window compacted" SSTables from being deleted when they should have been. However, when the corresponding data expires (in all SSTables), all relevant SSTables are will be deleted during the next compaction.
How to Change gc_grace_seconds for a Table </kb/gc-grace-seconds/>Expiring Data with Time to Live (TTL) </cql/time-to-live/>CQL Reference: Table Options <create-table-general-options>