Sub topic 16
Lifecycle Rules, Orphan Cleanup, and Egress Cost
This chapter assumes you know how presigned uploads, a quarantine prefix, and a CDN fit together from earlier chapters. It covers three ways media systems quietly waste money: files that nobody uses, unfinished uploads, and bytes that leave your network more often than they should.
Where the waste comes from
Direct uploads create objects that your database may never learn about. A user can request a presigned URL, upload the file, and close the tab before the client confirms. Multipart uploads are worse. Parts of an unfinished upload stay in the bucket and are billed, but they don't appear in a normal object listing.
Costs fall into three groups:
- Storage for orphans, old variants, and old object versions.
- Requests, such as the many small parts of a multipart upload.
- Egress, which is data transferred out to the internet.
Lifecycle rules
A lifecycle rule is a bucket-level policy that the storage service applies on a schedule, so you don't need to run your own cleanup job for simple age-based cases. Scope each rule with a key prefix. This is why earlier chapters separated incoming/ from the final keys.
import boto3
s3 = boto3.client("s3")
s3.put_bucket_lifecycle_configuration(
Bucket="example-uploads",
LifecycleConfiguration={
"Rules": [
{
"ID": "expire-unconfirmed-uploads",
"Status": "Enabled",
"Filter": {"Prefix": "incoming/"},
"Expiration": {"Days": 1},
"AbortIncompleteMultipartUpload": {"DaysAfterInitiation": 2},
},
{
"ID": "cool-old-originals",
"Status": "Enabled",
"Filter": {"Prefix": "originals/"},
"Transitions": [{"Days": 30, "StorageClass": "STANDARD_IA"}],
},
]
},
)Note that this call replaces the whole lifecycle configuration. Include every rule you want to keep.
Orphan cleanup beyond age rules
Age rules can't tell whether a database row still references an object. For objects that were promoted but later detached, such as an avatar the user replaced, use a periodic reconciliation job:
- Mark the database row as
deletedwhen the user removes the file. Don't delete the object yet. - After a grace period, delete the object and then the row.
- Occasionally list a prefix and compare it with known keys. Report the differences before you delete anything.
Reducing egress cost
Egress usually costs more than storage for popular media. The main levers are:
- Raise the CDN cache hit ratio with immutable, content-hashed URLs. Every hit avoids an origin fetch.
- Serve right-sized variants, such as a 400 px WebP instead of a 4000 px JPEG.
- Compress text-based assets, and use adaptive bitrate for video so viewers on slow links don't download the top rendition.
- Check your provider's pricing for origin-to-CDN transfer. Some providers don't charge for it, and others do.
Common errors
- Putting a short expiry on a prefix that also holds confirmed files. Scope rules tightly.
- Forgetting versioned buckets. Deleted objects leave noncurrent versions that still cost money, so add a noncurrent-version expiration rule.
- Moving small files to infrequent-access classes. Minimum billable sizes and per-request fees can make this more expensive.
- Expecting rules to run at an exact moment. Expiry is applied asynchronously, usually within a day.
Exercise
Users report that your bucket bill keeps growing, yet listing objects shows few files under incoming/. Which lifecycle setting is most likely missing, and why?
Show answer
A rule that aborts incomplete multipart uploads. Parts of abandoned uploads are billed but don't appear in normal object listings. An AbortIncompleteMultipartUpload rule (for example, DaysAfterInitiation of 2) removes them automatically.