Lifecycle management rules in GCP - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to apply lifecycle management rules changes as we manage more storage objects.
Specifically, how does the number of objects affect the work done by these rules?
Analyze the time complexity of applying lifecycle rules to objects in a Cloud Storage bucket.
// Define lifecycle rule for a bucket
storageClient.buckets().patch(bucketName, {
lifecycle: {
rule: [
{ action: { type: 'Delete' }, condition: { age: 30 } }
]
}
}).execute();
This sequence sets a rule to delete objects older than 30 days in a bucket.
When lifecycle rules run, they check each object in the bucket.
- Primary operation: Checking each object's metadata against the lifecycle conditions.
- How many times: Once per object in the bucket.
As the number of objects grows, the system checks more objects one by one.
| Input Size (n) | Approx. Checks |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of objects.
Time Complexity: O(n)
This means the time to apply lifecycle rules grows in direct proportion to the number of objects.
[X] Wrong: "Lifecycle rules run instantly no matter how many objects exist."
[OK] Correct: Each object must be checked, so more objects mean more work and more time.
Understanding how lifecycle rules scale helps you design storage solutions that stay efficient as data grows.
"What if lifecycle rules included multiple conditions and actions? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand lifecycle rules function
Lifecycle rules automate file management by applying actions like deletion or moving files based on set conditions.Step 2: Compare options with lifecycle purpose
Only To automatically delete or move files based on conditions like age or storage class describes automatic file management based on age or storage class, which matches lifecycle rules.Final Answer:
To automatically delete or move files based on conditions like age or storage class -> Option DQuick Check:
Lifecycle rules automate file management = A [OK]
- Confusing lifecycle rules with manual file upload
- Thinking lifecycle rules create backups
- Assuming lifecycle rules handle encryption
Solution
Step 1: Identify correct key names in lifecycle JSON
The lifecycle configuration uses the key "rules" (plural) for the list of rules, not "rule".Step 2: Check action type and condition keys
Action type must be "Delete" and condition uses "age" in days. {"rules": [{"action": {"type": "Delete"}, "condition": {"age": 30}}]} correctly uses "rules", "Delete", and "age".Final Answer:
{"rules": [{"action": {"type": "Delete"}, "condition": {"age": 30}}]} -> Option AQuick Check:
Correct lifecycle JSON uses "rules" and "age" = C [OK]
- Using singular "rule" instead of "rules"
- Using "Remove" instead of "Delete" for action
- Using "days" instead of "age" in condition
{"rules": [{"action": {"type": "SetStorageClass", "storageClass": "NEARLINE"}, "condition": {"age": 60}}]}What happens to objects older than 60 days?
Solution
Step 1: Understand action type in lifecycle rule
The action type "SetStorageClass" changes the storage class of objects matching the condition.Step 2: Analyze condition and effect
The condition "age": 60 means objects older than 60 days will be moved to the "NEARLINE" storage class.Final Answer:
They are moved to the NEARLINE storage class -> Option CQuick Check:
SetStorageClass action moves objects = A [OK]
- Confusing SetStorageClass with Delete action
- Assuming objects are deleted instead of moved
- Thinking NEARLINE means Coldline storage
{"rule": [{"action": {"type": "Delete"}, "condition": {"age": 90}}]}Why does this rule not work as expected?
Solution
Step 1: Check lifecycle JSON key names
The lifecycle configuration requires the key "rules" (plural) for the list of rules, not "rule".Step 2: Validate action and condition correctness
"Delete" is a valid action type, and "age" is correctly a number. Age can be any positive number.Final Answer:
The key should be "rules" not "rule" -> Option AQuick Check:
Correct key is "rules" = B [OK]
- Using singular "rule" instead of "rules"
- Thinking "Delete" is invalid action
- Believing age must be string or less than 30
Solution
Step 1: Identify correct order of lifecycle actions
Objects should first move to Coldline after 30 days, then be deleted after 365 days. The order matters for correct lifecycle behavior.Step 2: Check storage class and age conditions
{"rules": [{"action": {"type": "SetStorageClass", "storageClass": "COLDLINE"}, "condition": {"age": 30}}, {"action": {"type": "Delete"}, "condition": {"age": 365}}]} correctly sets "SetStorageClass" to "COLDLINE" at age 30, then "Delete" at age 365.Final Answer:
{"rules": [{"action": {"type": "SetStorageClass", "storageClass": "COLDLINE"}, "condition": {"age": 30}}, {"action": {"type": "Delete"}, "condition": {"age": 365}}]} -> Option BQuick Check:
Move to Coldline at 30 days, delete at 365 days = D [OK]
- Reversing delete and move actions order
- Using wrong storage class name
- Setting wrong age values for actions
