DynamoDB TTL

Topics Covered

Overview

With the TTL (Time to Live) option, which is available in modern databases, an object or record is automatically removed when its TTL property has expired. Like every other contemporary database, Amazon DynamoDB offers this incredibly useful feature as well.

What is DynamoDB TTL?

To decide when an item is no longer required, you can define a per-item timestamp using Amazon DynamoDB Time to Live (TTL). DynamoDB removes the item from your database shortly after the time and date of the supplied timestamp without using any write throughput.

The time-to-live mechanism is very helpful for getting rid of transient items like session data. We don't want to keep nullified sessions in the store because they don't matter to us, but they also take up storage space, which costs money (DynamoDB is also charged based on how much data we store).

Common Use-Cases for DynamoDB TTL

  • Delete or discard old or potentially irrelevant data.
  • Only keep sensitive data for a set amount of time to abide by regulatory regulations.
  • Put cold data in an Amazon S3 database archive.

Benefits of DynamoDB TTL

  • Decreases Storage: It is normal for storage consumption to decrease when we eliminate unnecessary data from a database. Removal of outdated data plays a significant role in maintaining low storage, even though this won't be noticeable with smaller datasets or tables that experience heavy traffic loads with thousands of new things being added every minute.
  • Minimum Query Outcomes: It must serve as a major driver for using DynamoDB TTL in AWS. A query or scan result from DynamoDB is limited to 1 MB of data. These queries may contaminate the results and slow down performance if they retrieve data that are of little use.
  • Reduces The Usage of Read Capacity Units: Less read capacity units are used when fewer data are fetched, similar to the lighter query results (RCU). It is directly connected to price. It may cost considerably more if the undesirable elements are also modified as part of your data processes. The cost of writes in DynamoDB is higher than the cost of read operations.
  • Makes Event-Driven Computing Possible: Many services may be triggered by DynamoDB TTL deletion events. It encourages service decoupling and makes event-driven computing among microservices possible.
  • Deletion of Ineffective Data: DynamoDB TTL in AWS eliminates the need for lambda functions to use specific queries and costly table scans to locate and delete things. DynamoDB TTL in AWS shifts accountability to the service provider, resulting in less code to write (codeless), which has further advantages.

How does DynamoDB TTL Work?

You must specify a specific property name that the service will check for when determining whether an item is suitable for expiration when you enable TTL on a DynamoDB table. A per-partition scanner background process automatically and continually assesses the expiry status of objects in the table once TTL has been enabled on the table.

The scanner background process checks the value of the user-defined attribute of an item to the current time, expressed in Unix epoch time format in seconds. The item is set to expire if the attribute is of the Number data type, the attribute's value is a timestamp in Unix epoch time format in seconds, and the timestamp value is five years old or older but not more than that (to prevent an inadvertent deletion owing to a potentially incorrect TTL value).

A separate background process searches for and removes things that have expired. Both actions occur automatically in the background, have no impact on read or write traffic to the table, and are free of charge.

When an item is removed from the table, two background processes run concurrently:

  • Items are deleted in the same manner as a DeleteItem action from any local secondary index and global secondary index. This procedure is free of charge.
  • Each item receives a delete operation that is entered into the DynamoDB Stream but is identified as a system deletion rather than a standard delete.

Enabling DynamoDB TTL

Enable DynamoDB TTL (With Console)

To enable Time to Live using the DynamoDB console, follow these steps:

  • Open the DynamoDB console by logging into the AWS Management Console at https://console.aws.amazon.com/dynamodb/.
  • Select Tables, and then select the table you wish to change.
  • Select Enable under the Time to Live (TTL) option on the Additional settings tab.

step to enable dynamodb ttl

  • Put the TTL attribute name on the Enable Time to Live (TTL) page.

step to enable dynamodb ttl

  • To keep the settings and activate TTL, select Enable TTL.

When you browse objects on the DynamoDB console, the TTL attribute is now marked TTL because TTL has been enabled. Hovering your cursor over the attribute will reveal the item's expiration date and time.

Setting Up TTL

Using CLI

update-time-to-live is a unique command found in the CLI. Together with the --time-to-live-specification flag, use it.

Using CDK

You can set the TTL on creation in CDK. Simply set the attribute of your choosing for timeToLiveAttribute.

Using AWS Console

Select "Manage TTL" in the Table Details section of the AWS Console.

using aws console

Pricing of DynamoDB TTL

DynamoDB TTL is offered at no additional charge as a way to decrease the amount of saved data by keeping just the items that are still relevant to your workflow. TTL is helpful if you keep things that become obsolete after a certain period.

Conclusion

  • In this article we learned about DynamoDB Time to Live(TTL). To decide when an item is no longer required, you can define a per-item timestamp using Amazon DynamoDB Time to Live (TTL).
  • DynamoDB TTL in AWS is offered free of cost as a way to decrease the amount of saved data.
  • The TTL option can be set at the item level even though it is enabled at the table level and given a name for the TTL property. You don't have to set the TTL in all items, nor does the TTL duration have to be the same for all of them.
  • The Unix epoch time in seconds must be used when setting the TTL value. It is significant. The background operation that detects and removes objects ignores any other formats.
  • If you select a DynamoDB TTL in AWS value that is more than five years old, DynamoDB won't let those objects expire.