Time-to-Live (TTL) Best Practices in DynamoDB
The most overlooked cost optimization strategy for your DynamoDB database
One of the most cost-effective tips you can leverage in DynamoDB is using TTLs.
Time-to-live (TTL) is a timestamp that you can add to an item in your database and it will define when DynamoDB should automatically delete that item.
There are 2 main benefits of using TTLs:
When DynamoDB deletes items through TTLs, you are not charged for these deletes.
You will have lower storage costs for data that is expired
I’ve seen many companies neglect setting TTLs on their data and they have suffered with massive costs with no simple way of deleting the mass amounts of expired data.
Architecting with TTLs from the start will let you avoid these mistakes and will declutter your database automatically.
Best Practices for TTLs
Some best practices for setting TTLs are:
Choose a Unix timestamp to define the expiration of that item (DynamoDB will delete it within 48 hours, though usually within minutes).
Track TTL deletions: use DynamoDB streams to track expired items (for compliance reasons, like an immutable logs data warehouse)
Use TTL for non-critical data: Since items with TTLs are not deleted instantly at their timestamp, don’t use it on data that needs to be deleted immediately.
Rather, use TTLs on items like: logs, temporary sessions, or cache data.
Test TTLs in a development environment to make sure the behavior matches your application’s needs.
Monitor TTL deletions with DynamoDB metrics to ensure it works for your application’s needs
Common Use Cases for TTLs
Some typical use cases for TTLs include:
Expiring user sessions — TTLs are commonly used with user sessions or auth tokens
Generally temporary data — these can be data like cart items in an e-commerce app, incomplete form submissions, temporary storage for file uploads, old metadata (that is going to an archive)
Log and Event data — you can store application logs or event data on DynamoDB and add TTL to automatically delete these after a retention period.
Rate limiting — for APIs or services that enforce rate limits, you can store user-specific data like API call timestamps and use TTLs to clean up the data automatically.
Adding TTLs Demo
Let’s see a demo of how we can implement TTLs in our DynamoDB.
Start by logging into the AWS console. Head over the the DynamoDB service.
Here, create a new table or use an existing one.
In the table overview page, click on the Actions button and select the option Turn on TTL in the dropdown.
On this new page, specify the attribute name you want to define as the TTL, for instance “TTLTimestamp”.
Once done, click on the button Turn on TTL.
Now all items that contain the attribute name “TTLTimestamp” will be deleted on the timestamp of the value you give them.
Conclusion
DynamoDB’s TTL feature helps us reduce storage costs and maintain a clean and efficient database by automatically expiring irrelevant data (at no cost).
In this article, we looked at best practices for TTLs and some typical use cases of when to use TTLs in DynamoDB.
Using TTLs, you ensure your application stays optimized and scalable in regards to costs, saving both time and resources in the long run.
👋 My name is Uriel Bitton and I’m committed to helping you master Serverless, Cloud Computing, and AWS.
🚀 If you want to learn how to build serverless, scalable, and resilient applications, you can also follow me on Linkedin for valuable daily posts.
Thanks for reading and see you in the next one!