Understanding Azure Cosmos DB TTL (Time to Live)
Hello and welcome! 😊Today, Let’s dive into exciting content on How we use TTL concept in Azure cosmos DB. But before that let’s see what TTL is Cosmos DB.
TTL in Azure Cosmos DB
This is a feature that automatically removes items from a container after a specified time.It helps in managing data retention and optimizing storage costs by automatically removing the outdated data.
TTL can be enabled at two levels:
- Container Level: Applies TTL settings to all documents within a container.
- Item Level: Overrides the container TTL for specific documents.
Prerequisites
- Azure standard Logic App
- Azure Cosmos DB
Process :
1.First, I created the Cosmos DB container with ‘EmployeeName’ as the partition key. This database is designed to store tasks assigned to each employee.

We need to set the container’s TTL to -1 to enable TTL management at the document level, allowing each document to have its own TTL configuration.

2. In the Logic App, I am designing a workflow that first fetches the task details of a particular employee from Cosmos DB. If the data is not present, it will create or update the required data.
Here , I am first fecthing the Tasks details of the perticular employee.

If the data is not available , then it will create the task. If the data is available then it will simply update the task.


Output :
Now, my document item 1 will remain active for 1 hour (3,600 seconds). The data will automatically expire after this period.

Now, my document item 1 will remain active for (50 seconds). The data will automatically expire after this period.

Conclusion
Azure Cosmos DB TTL(Time to Live) is a powerful feature for automating data cleaning and optimizing storage costs. Whether using it at the container or item level, it ensures that expired data is automatically removed without manual intervention.
By implementing TTL effectively, you can maintain a lean and efficient database while reducing operational overhead. In real-world scenarios, this approach can be used for generating an HTTP OAuth token cache store in Cosmos DB and storing incident tickets in Cosmos DB whenever an error occurs.
Thanks for stopping by! Your visit means a lot. Please Follow me😊 Stay tuned for more content. Exciting stuff coming your way soon! 🚀 #StayTuned. Also, visit on RioTech.
🔗 Follow Me for More Updates:
🌐 Blog: https://riovtech.com/
📱 Instagram : https://www.instagram.com/_riovtech_/
🐦 Twitter: https://x.com/riotech576
Post Comment
You must be logged in to post a comment.