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

  1. 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.
  2. 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);

  3. 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);

  4. To set the default TTL on the container, you can use the ReplaceContainerAsync method and provide the modified container properties, including the DefaultTimeToLive property. For example:
    ContainerProperties containerProperties = await container.ReadContainerAsync();
    containerProperties.DefaultTimeToLive = 3600; // TTL set to 1 hour

    await 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

  1. Navigate to the Azure portal and open your Cosmos DB account.
  2. In the left-hand menu, click on “Data Explorer.”
  3. Select the appropriate database and container from the tree view.
  4. In the container view, click on the “Settings” tab.
  5. Under “Time to Live (TTL),” check the “Enable” box to enable the TTL feature.
  6. Set the desired TTL duration (in seconds) in the “Default time to live (seconds)” input box.
  7. 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

0 0 votes
Article Rating
Subscribe
Notify of
guest
33 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Florent Adam
10 months ago

I found this post extremely helpful in understanding how to set a default TTL on containers in Azure Cosmos DB.

Gabrielle Dufour
1 year ago

Thanks for the blog post! It really cleared up some confusion I had.

Marion Robert
1 year ago

Interesting read, but I would love some more examples around setting TTLs with different indexing policies.

Leonel Martins
8 months ago

Very informative! Appreciate the detailed steps provided.

Oskar Thorsen
1 year ago

Can someone explain the impact of TTL settings on read/write performance?

David Riojas
1 year ago

Great post! Loving the simplicity in your explanations.

Lauren Pearson
1 year ago

I’m new to Azure Cosmos DB and this post gave me a head start on managing TTL settings. Thanks a lot!

Tomas Carrasco
9 months ago

I think some of the points could be elaborated further, especially with regard to exceptions when TTL reaches zero.

33
0
Would love your thoughts, please comment.x
()
x