TTL with Streams for archival in DynamoDB - Time & Space Complexity
When using TTL with Streams for archival in DynamoDB, it's important to understand how the processing time changes as data grows.
We want to know how the time to handle expired items and archive them scales with the number of expired records.
Analyze the time complexity of the following DynamoDB TTL and Stream processing snippet.
// DynamoDB table with TTL enabled
// Stream triggers a Lambda on item expiration
exports.handler = async (event) => {
for (const record of event.Records) {
if (record.eventName === 'REMOVE') {
await archiveItem(record.dynamodb.OldImage);
}
}
};
async function archiveItem(item) {
// Save expired item to archival storage
}
This code listens to expired items removed by TTL and archives each one individually.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each expired record in the event stream.
- How many times: Once per expired item in the batch received from the stream.
As the number of expired items in the stream batch grows, the processing time grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 archive calls |
| 100 | 100 archive calls |
| 1000 | 1000 archive calls |
Pattern observation: The time grows linearly with the number of expired items processed.
Time Complexity: O(n)
This means the time to archive expired items grows directly in proportion to how many items expire at once.
[X] Wrong: "Processing expired items is constant time regardless of how many expire."
[OK] Correct: Each expired item triggers a separate archival operation, so more expired items mean more work.
Understanding how batch sizes affect processing time helps you design scalable data pipelines and handle real-world data flows confidently.
"What if we batch multiple expired items into a single archival call? How would the time complexity change?"
Practice
Solution
Step 1: Understand TTL function
TTL marks items with an expiration timestamp so DynamoDB can delete them automatically after expiry.Step 2: Identify TTL's main benefit
This automatic deletion helps keep the table clean without manual intervention.Final Answer:
Automatically delete expired items to keep the table clean -> Option DQuick Check:
TTL = automatic deletion [OK]
- Confusing TTL with encryption
- Thinking TTL creates backups
- Assuming TTL increases capacity
Solution
Step 1: Recall AWS CLI TTL command
The correct command is 'update-time-to-live' with parameters for table name and TTL specification.Step 2: Match correct syntax
aws dynamodb update-time-to-live --table-name MyTable --time-to-live-specification Enabled=true,AttributeName=expiryDate uses the exact syntax: --time-to-live-specification Enabled=true,AttributeName=expiryDate.Final Answer:
aws dynamodb update-time-to-live --table-name MyTable --time-to-live-specification Enabled=true,AttributeName=expiryDate -> Option CQuick Check:
Correct CLI command = aws dynamodb update-time-to-live --table-name MyTable --time-to-live-specification Enabled=true,AttributeName=expiryDate [OK]
- Using non-existent commands like enable-ttl
- Wrong parameter names
- Missing 'time-to-live-specification' flag
Solution
Step 1: Understand TTL deletion effect on streams
When TTL deletes an item, DynamoDB Streams capture this as a REMOVE event because the item is removed.Step 2: Identify correct event type
INSERT and MODIFY happen on new or updated items, not deletions. REMOVE is the correct event for deletions.Final Answer:
REMOVE -> Option BQuick Check:
TTL deletion = REMOVE event [OK]
- Choosing INSERT or MODIFY for deletions
- Confusing UPDATE with MODIFY
- Assuming TTL triggers no stream events
Solution
Step 1: Check stream configuration
If the Lambda never receives events, the stream might be disabled or not configured to capture REMOVE events, which TTL deletions generate.Step 2: Verify other causes
TTL must be enabled, but if streams are off or missing REMOVE events, Lambda won't get notifications. Permissions or missing primary key would cause other errors.Final Answer:
The stream is not enabled or not set to capture REMOVE events -> Option AQuick Check:
Missing REMOVE events in stream = The stream is not enabled or not set to capture REMOVE events [OK]
- Ignoring stream settings
- Assuming permissions cause no events
- Overlooking TTL enablement
Solution
Step 1: Understand archival with TTL and streams
TTL deletes expired items, streams capture REMOVE events. Lambda triggered by these events can archive data safely.Step 2: Ensure reliability and no data loss
Processing REMOVE events and writing to S3 with retries ensures data is archived even if Lambda fails temporarily.Step 3: Evaluate other options
Use TTL to delete items, ignore streams, and periodically scan table for expired items ignores streams and risks missing data. Disable TTL and manually delete items after archiving disables TTL, losing automatic cleanup. Process INSERT events in Lambda and archive items before expiration archives before expiration, which is not TTL-based archival.Final Answer:
Process REMOVE events in Lambda and write directly to S3, retrying on failure -> Option AQuick Check:
Reliable archival = Process REMOVE events in Lambda and write directly to S3, retrying on failure [OK]
- Ignoring stream REMOVE events
- Disabling TTL unnecessarily
- Archiving on INSERT instead of REMOVE
Ready to prove it?
Pass the test and this pattern moves unknown
