|
| 1 | +# TTL |
| 2 | + |
| 3 | +TTL for Mutable Streams works differently from [Append Stream](/append-stream-ttl) because data is row-encoded. |
| 4 | + |
| 5 | +Mutable Streams evaluate the timestamp of each row in the historical store asynchronously during background data merge or compaction of the key space. Expired rows are automatically garbage-collected when their TTL elapses. |
| 6 | + |
| 7 | +:::info |
| 8 | +A Mutable Stream created without a TTL cannot be altered later to support TTL, and similarly, a stream with TTL cannot have TTL removed. |
| 9 | +::: |
| 10 | + |
| 11 | +## Retention Based on Wall Clock |
| 12 | + |
| 13 | +In this mode, you specify the storage setting `ttl_seconds` with a non-zero value. |
| 14 | + |
| 15 | +When a row is inserted, the system automatically assigns a **wall-clock timestamp** to it. |
| 16 | +If a row has not been updated for longer than `ttl_seconds`, it becomes **eligible for garbage collection** during background compaction. |
| 17 | + |
| 18 | +### Syntax |
| 19 | + |
| 20 | +```sql |
| 21 | +CREATE MUTABLE STREAM ... |
| 22 | +SETTINGS |
| 23 | + ttl_seconds=<ttl-seconds>, ... |
| 24 | +``` |
| 25 | + |
| 26 | +### Example |
| 27 | + |
| 28 | +```sql |
| 29 | +CREATE MUTABLE STREAM ttl_cached( |
| 30 | + id string, |
| 31 | + name string, |
| 32 | + price string, |
| 33 | + description string, |
| 34 | + event_time datetime |
| 35 | +) |
| 36 | +PRIMARY key id |
| 37 | +SETTINGS |
| 38 | + ttl_seconds = 3600; |
| 39 | +``` |
| 40 | + |
| 41 | +In this example, each row is assigned a **TTL of 1 hour** (3600 seconds). |
| 42 | +If a row is not updated within that time window, it will be **automatically removed** during the next background compaction. |
| 43 | + |
| 44 | +## Retention Based on Event Timestamp |
| 45 | + |
| 46 | +In this mode, you must specify both the storage settings `ttl_seconds` (a non-zero value) and `ttl_column`. |
| 47 | + |
| 48 | +The specified **TTL column** represents the event timestamp of each row. During background compaction, the system evaluates this column to determine whether the row has expired and should be garbage-collected. |
| 49 | + |
| 50 | +### Syntax |
| 51 | + |
| 52 | +```sql |
| 53 | +CREATE MUTABLE STREAM ... |
| 54 | +SETTINGS |
| 55 | + ttl_seconds=<ttl-seconds>, |
| 56 | + ttl_column=<ttl-column>, ... |
| 57 | +``` |
| 58 | + |
| 59 | +### Example |
| 60 | + |
| 61 | +```sql |
| 62 | +CREATE MUTABLE STREAM event_ttl_cached( |
| 63 | + id string, |
| 64 | + name string, |
| 65 | + price string, |
| 66 | + description string, |
| 67 | + event_time datetime |
| 68 | +) |
| 69 | +PRIMARY key id |
| 70 | +SETTINGS |
| 71 | + ttl_seconds = 3600, |
| 72 | + ttl_column = 'event_time'; |
| 73 | +``` |
| 74 | + |
| 75 | +In this example, the stream is configured with a **TTL of 1 hour**. |
| 76 | +If `now() - event_time >= 3600`, the row is considered **expired** and will be **garbage-collected** during background compaction. |
| 77 | + |
| 78 | +## Controlling TTL Frequency |
| 79 | + |
| 80 | +TTL evaluation occurs during background **merge** or **compaction** operations. |
| 81 | +You can control how frequently these compactions run by setting `periodic_compaction_seconds` in `kvstore_options`. |
| 82 | + |
| 83 | +**Example:** |
| 84 | + |
| 85 | +``` |
| 86 | +CREATE MUTABLE STREAM ... |
| 87 | +SETTINGS |
| 88 | + ttl_seconds = <ttl-seconds>, |
| 89 | + ttl_column = <ttl-column>, |
| 90 | + kvstore_options = 'periodic_compaction_seconds=1800;'; |
| 91 | +``` |
| 92 | + |
| 93 | +In this example, the storage engine is configured to trigger periodic compaction every **30 minutes** (1800 seconds), which indirectly determines how often expired rows are cleaned up. |
| 94 | + |
| 95 | +## Manually Triggering Compaction |
| 96 | + |
| 97 | +Expired data is removed only during background compaction. |
| 98 | +If you want to accelerate TTL cleanup, you can manually trigger compaction using the `OPTIMIZE` command. |
| 99 | + |
| 100 | +```sql |
| 101 | +OPTIMIZE STREAM <db.stream-name>; |
| 102 | +``` |
| 103 | + |
| 104 | +This command initiates an immediate, unscheduled merge or compaction for the specified stream, helping reclaim space and remove expired rows sooner. |
0 commit comments