TTL attribute setup in DynamoDB - Time & Space Complexity
We want to understand how the time it takes to set up a TTL attribute in DynamoDB changes as we add more items.
How does the system handle expiration times when the table grows?
Analyze the time complexity of the following TTL setup code.
// Enable TTL on a DynamoDB table
const params = {
TableName: "MyTable",
TimeToLiveSpecification: {
AttributeName: "expireAt",
Enabled: true
}
};
await dynamodb.updateTimeToLive(params).promise();
This code enables TTL on the "expireAt" attribute for the table, telling DynamoDB to delete items after that time.
Look for repeated actions that affect time.
- Primary operation: DynamoDB scans internally to find expired items.
- How many times: This happens continuously in the background, not per request.
As the number of items grows, the background process checks more items for expiration.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Few checks per minute |
| 100 | More checks, but still fast |
| 1000 | Many checks, takes longer |
Pattern observation: The background expiration work grows roughly in proportion to the number of items.
Time Complexity: O(n)
This means the time to process expired items grows linearly with the number of items in the table.
[X] Wrong: "Enabling TTL instantly deletes expired items regardless of table size."
[OK] Correct: TTL deletion happens in the background and takes longer as the table grows, so it is not instant.
Understanding how background processes scale with data size helps you explain system behavior clearly and shows you think about real-world performance.
"What if we added a filter to only check items with a certain prefix? How would that affect the time complexity?"