본문 바로가기

빅데이터/cassandra

카산드라 TTL에 따른 데이터 삭제 정리

https://docs.datastax.com/en/cql-oss/3.3/cql/cql_using/useExpire.html

 

Expiring data with time-to-live

Use time-to-live (TTL) to expire data in a column or table. Columns and tables support an optional expiration period called TTL (time-to-live); TTL is not supported on counter columns. Define the TTL value in seconds. Data expires once it exceeds the TTL p

docs.datastax.com

카산드라에서는 컬럼단위 또는 테이블 단위로 TTL(time-tolive)를 설정할 수 있습니다. ttl은 카운터컬럼에서는 지원되지 않는 기능입니다 ttl은 초단위로 설정가능하고 ttl이 도래한 데이터는 tombstone이라고 하는 플래그가 찍힙니다. tombsotone이 설정된 데이터는 gc_grace_seconds에 도래할 때까지 계속해서 읽기가 가능합니다. 일반적인 컴팩션 또는 repair에 따라 tombstone데이터는 삭제됩니다.

 

tombstone : A marker in a row that indicates a column was deleted. During compaction, marked columns are deleted.

gc_grace_seconds : Seconds after data is marked with a tombstone (deletion marker) before it is eligible for garbage-collection. Default value: 864000 (10 days). The default value allows time for Cassandra to maximize consistency prior to deletion.

default_time_to_live : TTL (Time To Live) in seconds, where zero is disabled. The maximum configurable value is 630720000 (20 years). If the value is greater than zero, TTL is enabled for the entire table and an expiration timestamp is added to each column. A new TTL timestamp is calculated each time the data is updated and the row is removed after all the data expires. Default value: 0 (disabled).

 

참고사항:

- TTL은 초단위로 설정되며 코디네이터 노드에 의해 계산됩니다. TTL을 사용하면 모든 노드들이 시간을 싱크합니다.

- TTL이 너무 짧으면 유용하지 않습니다.

- 삭제 대상 데이터는 8바이트를 메모리와 디스크에서 추가로 사용합니다. 이 추가 용량은 TTL과 grace period 계산에 사용됩니다.

 

1) column에 TTL 설정하는 방법

insert 또는 update를 통해 ttl을 설정할 수 있습니다.

> 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 86400;
> UPDATE cycling.calendar USING TTL 259200 
  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 확인 방법

> SELECT TTL (race_name) from cycling.calendar WHERE race_id = 200;
ttl(race_name)
---------------
86371

 

2) table단위로 TTL 설정하는 방법

create table 또는 alter table에서 default_time_to_live 설정이 가능합니다.

> create table cyclist(date text, data text, primary key(date, data)) 
 with default_time_to_live = 3600;

1시간(3600초) TTL이 기본 설정된 테이블

반응형