Concepts
To specify a default Time-To-Live (TTL) on a container for a transactional store in Microsoft Azure Cosmos DB, you can leverage the Cosmos DB SDKs or the Azure portal. This article will guide you through the process of implementing the default TTL on a container using both approaches.
Default TTL allows you to define a time duration in seconds for the documents in a container to live. Once this duration elapses, Cosmos DB automatically removes the expired documents from the container. This feature is especially useful for scenarios where you need to store time-sensitive data or implement data retention policies.
Using Cosmos DB SDKs
- Ensure you have the necessary Cosmos DB SDK installed in your application. You can find the SDKs and their installation instructions from the official Azure Cosmos DB SDKs page.
-
Create an instance of the
CosmosClient
class, which represents the Cosmos DB account connection. You’ll need to provide the account endpoint URI and the account key or another appropriate authentication method. For example:
using Microsoft.Azure.Cosmos;
// ...
string endpointUri = "
";
string accountKey = ""; CosmosClient cosmosClient = new CosmosClient(endpointUri, accountKey);
-
Retrieve the container where you want to set the default TTL using the
GetContainer
method, specifying the database ID and the container ID. For example:
string databaseId = "
";
string containerId = ""; Database database = cosmosClient.GetDatabase(databaseId);
Container container = database.GetContainer(containerId);
-
To set the default TTL on the container, you can use the
ReplaceContainerAsync
method and provide the modified container properties, including theDefaultTimeToLive
property. For example:
ContainerProperties containerProperties = await container.ReadContainerAsync();
containerProperties.DefaultTimeToLive = 3600; // TTL set to 1 hourawait container.ReplaceContainerAsync(containerProperties);
By setting the DefaultTimeToLive
property to 3600
(1 hour), all documents that you insert into this container will have a default TTL of 1 hour, unless you specify a different value at the document level.
Using the Azure portal
- Navigate to the Azure portal and open your Cosmos DB account.
- In the left-hand menu, click on “Data Explorer.”
- Select the appropriate database and container from the tree view.
- In the container view, click on the “Settings” tab.
- Under “Time to Live (TTL),” check the “Enable” box to enable the TTL feature.
- Set the desired TTL duration (in seconds) in the “Default time to live (seconds)” input box.
- Click the “Save” button to apply the changes.
Once you have done this, the container will automatically remove documents that have expired based on the time duration you specified in the default TTL.
In this article, we have discussed how to specify a default TTL on a container for a transactional store in Azure Cosmos DB. Whether you prefer using the Cosmos DB SDKs or the Azure portal, you can easily implement default TTL to automatically manage the lifecycle of your data in a container.
Answer the Questions in Comment Section
Which of the following options allow you to specify a default TTL (Time to Live) for a container in Azure Cosmos DB?
a) Azure portal
b) Azure Cosmos DB CLI
c) Azure Cosmos DB SDKs
Correct answer(s): a, b, c
The default TTL for a container in Azure Cosmos DB is set to:
a) 1 day
b) 7 days
c) 30 days
d) No default TTL is set
Correct answer: d
True or False: Specifying a default TTL on a container automatically deletes documents when their TTL expires.
Correct answer: False
When specifying a default TTL on a container, what happens to existing documents that do not have a TTL value?
a) They are automatically assigned the default TTL value.
b) They are not affected and will remain without a TTL value.
c) An error is thrown and the container creation fails.
Correct answer: b
The TTL value for a document in Azure Cosmos DB is specified as:
a) A timestamp indicating the deletion time.
b) A time duration from the document’s creation time.
c) A Boolean value indicating whether the document should expire.
Correct answer: b
How can you override the default TTL for a specific document in Azure Cosmos DB?
a) By setting the TTL value to –
b) By setting the TTL value to
c) By setting a custom TTL value for the document.
Correct answer: c
True or False: The TTL value for a document must always be greater than the default TTL specified on a container.
Correct answer: False
When a document’s TTL expires in Azure Cosmos DB, what happens to the document?
a) The document is immediately deleted.
b) The document becomes read-only and cannot be modified.
c) The document is marked for deletion and scheduled for garbage collection.
Correct answer: c
What happens if a document is modified after its TTL has expired but before it is deleted?
a) The modified document is automatically deleted.
b) The modified document retains its TTL value.
c) The modified document obtains a new TTL value.
Correct answer: b
True or False: The TTL of a specific document can be updated once it has been set.
Correct answer: True
I found this post extremely helpful in understanding how to set a default TTL on containers in Azure Cosmos DB.
Thanks for the blog post! It really cleared up some confusion I had.
Interesting read, but I would love some more examples around setting TTLs with different indexing policies.
Very informative! Appreciate the detailed steps provided.
Can someone explain the impact of TTL settings on read/write performance?
Great post! Loving the simplicity in your explanations.
I’m new to Azure Cosmos DB and this post gave me a head start on managing TTL settings. Thanks a lot!
I think some of the points could be elaborated further, especially with regard to exceptions when TTL reaches zero.