Yandex.Cloud
  • Services
  • Why Yandex.Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Yandex Database
  • Getting started
    • Overview
    • Create databases
    • Examples of YQL queries
    • Examples of operations in the YDB CLI
    • Launch a test app
    • Document API
    • Developing in NodeJS through the Document API
  • Step-by-step instructions
    • Database management
    • How to connect to a database
    • Table management
    • Reading and writing data
    • Working with secondary indexes
  • Working with the SDK
  • Concepts
    • Overview
    • Data model and schema
    • Serverless and Dedicated operation modes
    • Data types
    • Transactions
    • Secondary indexes
    • Time to Live (TTL)
    • Terms and definitions
    • Quotas and limits
  • Access management
  • Pricing policy
    • Overview
    • Serverless mode
    • Dedicated mode
  • Recommendations
    • Schema design
    • Partitioning tables
    • Secondary indexes
    • Paginated output
    • Loading large data volumes
    • Using timeouts
  • YDB API and API reference
    • Database limits
    • Handling errors in the API
  • Amazon DynamoDB-compatible HTTP API
    • API reference
      • All methods
      • Actions
        • BatchGetItem
        • BatchWriteItem
        • CreateTable
        • DeleteItem
        • DeleteTable
        • DescribeTable
        • GetItem
        • ListTables
        • PutItem
        • Query
        • Scan
        • TransactGetItems
        • TransactWriteItems
        • UpdateItem
      • Common errors
  • YQL reference guide
    • Overview
    • Data types
      • Simple
      • Optional
      • Containers
      • Special
    • Syntax
      • Unsupported statements
      • For text representation of data types
      • Expressions
      • CREATE TABLE
      • DROP TABLE
      • INSERT INTO
      • UPSERT INTO
      • REPLACE INTO
      • UPDATE
      • DELETE
      • SELECT
      • GROUP BY
      • JOIN
      • FLATTEN
      • ACTION
      • DISCARD
      • PRAGMA
      • DECLARE
      • OVER, PARTITION BY, and WINDOW
    • Built-in functions
      • Basic
      • Aggregate
      • Window
      • For lists
      • For dictionaries
      • For JSON
      • For structures
      • For types
    • Preset user-defined functions
      • HyperScan
      • Pcre
      • Pire
      • Re2
      • String
      • Unicode
      • Datetime
      • Url
      • Ip
      • Digest
      • Math
      • Histogram
    • For text representation of data types
  • YQL tutorial
    • Overview
    • Creating a table
    • Adding data to a table
    • Selecting data from all columns
    • Selecting data from specific columns
    • Sorting and filtering
    • Data aggregation
    • Additional selection criteria
    • Joining tables by JOIN
    • Data insert and update by REPLACE
    • Data insert and update by UPSERT
    • Data insert by INSERT
    • Data update by UPDATE
    • Deleting data
    • Adding and deleting columns
    • Deleting a table
  • Maintenance
    • Backups
  • Diagnostics
    • System views
  • Questions and answers
    • General questions
    • Errors
    • YQL
    • All questions on the same page
  • Public materials
  1. Concepts
  2. Time to Live (TTL)

Time to Live (TTL)

  • How it works
  • Guarantees
  • Limits
  • Configuration
    • Enabling TTL for an existing table
    • Enabling TTL for a newly created table
    • Disabling TTL

This section describes how the TTL mechanism works and what its limits are. It also gives examples of commands and code snippets that can be used to enable, configure, and disable TTL.

How it works

YDB lets you specify a table column (TTL column), whose values set the lifetime of items. TTL automatically deletes the item from your table once the specified number of seconds passes after the time set in the TTL column.

Warning

An item with the NULL value in the TTL column is never deleted.

The timestamp for deleting a table item is determined by the formula:

expiration_time = valueof(ttl_column) + expire_after_seconds

Note

TTL doesn't guarantee that the item will be deleted exactly at expiration_time, it might happen later. If it's important to exclude logically obsolete but not yet physically deleted items from the selection, use request-level filtering.

Data is deleted by the Background Removal Operation (BRO), consisting of two stages:

  1. Checking the values in the TTL column.
  2. Deleting expired data.

The BRO has the following properties:

  • The concurrency unit is a table shard.
  • For tables without secondary indexes, blind deletes are performed. This means that if the TTL column value is updated in-between stages 1 and 2 (for example, with an UPDATE request), the updated value is not rechecked.
  • For tables with secondary indexes, the delete stage is a distributed transaction.

Guarantees

  • At every point in time, a BRO is run in no more than 1 instance per table.
  • BROs are run no more than once per hour for the same shard.
  • For tables with secondary indexes, data consistency is guaranteed.

Limits

  • The TTL column must be of one of the following types:
    • Date
    • Datetime
    • Timestamp
  • You can't specify multiple TTL columns.
  • You can't delete the TTL column. However, if this is required, you should first disable TTL for the table.

Configuration

Currently, you can manage TTL settings using:

  • YDB console client.
  • YDB Python SDK.

Enabling TTL for an existing table

In the example below, the items of the mytable table will be deleted an hour after the time set in the created_at column:

CLI
Python
$ ydb -e <endpoint> -d <database> table ttl set --column created_at --expire-after 3600 mytable
session.alter_table('mytable', set_ttl_settings=ydb.TtlSettings().with_date_type_column('created_at', 3600))

Enabling TTL for a newly created table

For a newly created table, you can pass TTL settings along with the table description:

Python
session.create_table(
    'mytable',
    ydb.TableDescription()
    .with_column(ydb.Column('id', ydb.OptionalType(ydb.DataType.Uint64)))
    .with_column(ydb.Column('expire_at', ydb.OptionalType(ydb.DataType.Timestamp)))
    .with_primary_key('id')
    .with_ttl(ydb.TtlSettings().with_date_type_column('expire_at'))
)

Disabling TTL

CLI
Python
$ ydb -e <endpoint> -d <database> table ttl drop mytable
session.alter_table('mytable', drop_ttl_settings=True)
In this article:
  • How it works
  • Guarantees
  • Limits
  • Configuration
  • Enabling TTL for an existing table
  • Enabling TTL for a newly created table
  • Disabling TTL
Language
Careers
Privacy policy
Terms of use
© 2021 Yandex.Cloud LLC