Time to live (TTL) for expiration in AWS - Time & Space Complexity
We want to understand how the time it takes to expire items using TTL changes as we add more items.
How does the system handle expiration when many items have TTL set?
Analyze the time complexity of setting TTL on multiple items in a DynamoDB table.
// Example: Enable TTL and update items with expiration timestamps
aws dynamodb update-time-to-live --table-name MyTable --time-to-live-specification Enabled=true,AttributeName=ttl
for each item in items:
aws dynamodb update-item --table-name MyTable --key '{"id": {"S": "' + item.id + '"}}' --update-expression "SET ttl = :expiry" --expression-attribute-values '{":expiry": {"N": "' + item.expiry + '"}}'
This sequence enables TTL on a table and updates each item with its expiration time.
We look at what repeats as we add more items.
- Primary operation: Updating each item with a TTL attribute.
- How many times: Once per item in the dataset.
Each item requires one update call to set its TTL. More items mean more update calls.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 update calls |
| 100 | 100 update calls |
| 1000 | 1000 update calls |
Pattern observation: The number of update calls grows directly with the number of items.
Time Complexity: O(n)
This means the time to set TTL grows linearly as you add more items.
[X] Wrong: "Setting TTL on one item automatically expires all items."
[OK] Correct: Each item must have its own TTL set; expiration is per item, not global.
Understanding how TTL operations scale helps you design systems that handle data expiration efficiently as data grows.
"What if we batch update items with TTL instead of one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand TTL function
TTL is designed to remove items automatically after a set expiration time.Step 2: Identify the main benefit
This automatic deletion helps save storage space and reduces manual cleanup.Final Answer:
Automatically delete expired items to save storage space -> Option CQuick Check:
TTL purpose = automatic deletion [OK]
- Confusing TTL with encryption
- Thinking TTL backs up data
- Assuming TTL improves query speed
Solution
Step 1: Check TTL attribute requirements
DynamoDB TTL requires a Number attribute representing expiration time as Unix epoch seconds.Step 2: Eliminate other types
String, Boolean, or Binary types are not valid for TTL expiration timestamps.Final Answer:
Number representing Unix epoch time in seconds -> Option AQuick Check:
TTL attribute = Number (epoch seconds) [OK]
- Using string dates instead of numbers
- Setting TTL attribute as Boolean
- Trying to use binary data for TTL
expireAt: 1700000000 (Unix epoch seconds), what happens when current time is 1700000001?Solution
Step 1: Compare current time with TTL value
The current time (1700000001) is greater than the TTL attribute (1700000000), so the item is expired.Step 2: Understand TTL behavior on expiration
DynamoDB TTL automatically deletes expired items shortly after expiration time passes.Final Answer:
Item is immediately deleted by DynamoDB TTL process -> Option DQuick Check:
Current time > TTL = item deleted [OK]
- Thinking TTL deletes instantly at exact second
- Assuming manual deletion is needed
- Believing TTL archives data instead of deleting
expiry but items are not deleting after expiration. What is the most likely cause?Solution
Step 1: Check TTL attribute correctness
If the TTL attribute is missing or not a Number with epoch seconds, TTL won't delete items.Step 2: Verify other options
TTL works regardless of region, does not require manual activation, and does not need to be primary key.Final Answer:
TTL attributeexpiryis missing or has wrong data type -> Option BQuick Check:
Wrong TTL attribute = no deletion [OK]
- Thinking TTL depends on table region
- Believing TTL needs manual daily activation
- Assuming TTL attribute must be primary key
Solution
Step 1: Calculate TTL value correctly
TTL attribute must be a Number in seconds, so add 1800 seconds (30 minutes) to current epoch seconds.Step 2: Eliminate incorrect formats
ISO strings, Booleans, or milliseconds are invalid for TTL attribute in DynamoDB.Final Answer:
Set TTL attribute to current Unix epoch time in seconds plus 1800 seconds -> Option AQuick Check:
TTL = epoch seconds + 1800 [OK]
- Using ISO date strings instead of epoch seconds
- Using milliseconds instead of seconds
- Setting TTL as Boolean instead of number
Ready to prove it?
Pass the test and this pattern moves unknown
