Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

DynamoDB TTL, Soft Deletes, Archives and Storage Hygiene

TTL is a janitor, not a transaction. Nairobi lead Amina Otieno and Pune founder Vikram Patil said: We set expiresAt, then expected instant delete — items lingered for hours. Soft-delete for undo; TTL for eventual expiry; S3 for cold keep. Short TTL note stays in CRUD. Modeling: single-table.

In this chapter

  • Explain TTL with an everyday story
  • Choose soft-delete vs TTL vs S3 archive
  • Enable TTL and verify with CLI
  • Map hygiene across industries
  • Complete the lab and cleanup

1. Everyday example: fridge sticky notes

A sticky note with a date is TTL. Soft-delete = move to maybe-later tray. Archive = photo to a box (S3). Meera Kulkarni (Pune clinic) soft-deletes appointments 14 days; TTL then clears; invoices export to S3.

2. Vertical steps

Why -> What -> How -> Lab -> CleanupWhyWhatHowLabCleanup
Always cleanup lab resources

3. Purpose

Storage and hot partitions grow when abandoned carts, OTPs, and sessions never die. Manual cleanup jobs fail. TTL is built-in eventual delete; soft-delete keeps undo; archives keep compliance cheap.

4. Overview

TTL: number attribute (epoch seconds). DynamoDB usually deletes expired items within a few days. There is no exact deletion deadline. If the item is a session or OTP, check its expiry in the app before accepting it, even if the item still exists. Soft-delete: deleted=true still Queryable. Archive: export or app copy to S3 then delete.

5. Steps

  1. Pick attribute name (expiresAt) and document it.
  2. Write epoch seconds (not ms) on items that must die.
  3. Enable TTL on the table for that attribute.
  4. For undo windows: soft-delete first; set TTL for hard expiry.
  5. For compliance: export/archive before hard delete.
  6. Alarm on storage growth and Streams noise from deletes.

6. CartLite abandoned cart

PK=USER#u100 SK=CART#draft expiresAt=now+72h. Soft-delete sets status=ABANDONED; TTL still clears. Checkout clears TTL so paid carts do not vanish.

7. Commands with output and meaning

aws dynamodb update-time-to-live --table-name lab-ddg-ttl \
  --time-to-live-specification "Enabled=true, AttributeName=expiresAt"
# Output: TimeToLiveStatus ENABLED or ENABLING
aws dynamodb describe-time-to-live --table-name lab-ddg-ttl
# Output: AttributeName expiresAt, Status ENABLED
aws dynamodb put-item --table-name lab-ddg-ttl --item '{"PK":{"S":"USER#u1"},"SK":{"S":"CART#draft"},"expiresAt":{"N":"1767225600"},"status":{"S":"OPEN"}}'
# Meaning: item deleted after that epoch (eventual).

update-time-to-live turns the janitor on. describe confirms. put-item sets epoch seconds. Deletes are eventual — design UX accordingly.

8. Industry stories

  • Retail (Diego Alvarez): abandoned carts TTL 72h; soft-delete for 24h undo.
  • OTP/auth (Fatima Zahra): session keys TTL minutes.
  • IoT (Mei Lin): raw readings TTL 14d; rollups archived.
  • Media (Priya Nair): watch progress soft-delete; TTL 90d.
  • Bank (Lucas Meyer): soft-delete only; legal archive to S3.
  • SaaS (Omar Haddad): trial tenants TTL after churn + export.

9. Lab

  1. Create lab-ddg-ttl on-demand; enable TTL on expiresAt.
  2. Put two items: one with TTL, one soft-deleted.
  3. describe-time-to-live; delete table when done.

Cleanup (stop billing)

  1. Delete lab resources you created.
  2. Confirm with describe that names are gone.

10. Ops checklist before you ship

  1. Confirm TTL attribute name and units (epoch seconds).
  2. Decide soft-delete vs hard-delete vs archive-to-S3.
  3. Enable PITR on any table you cannot rebuild from source.
  4. Document RPO/RTO and who runs restore drills.
  5. If multi-region: pick write stickiness and conflict rules.
  6. If DAX: prove cache-miss path and warm-up plan.
  7. Tag Owner + Expiry on every lab resource.
  8. Budget alert in the account before load tests.

11. Copy-paste DynamoDB CLI starters (batch 2)

export AWS_DEFAULT_REGION=ap-south-1

# TTL status
aws dynamodb describe-time-to-live --table-name ShopApp

# PITR / continuous backups
aws dynamodb describe-continuous-backups --table-name ShopApp

# On-demand backup
aws dynamodb create-backup --table-name ShopApp --backup-name lab-shop-$(date +%Y%m%d)

# List backups
aws dynamodb list-backups --table-name ShopApp

# Global table description (v2)
aws dynamodb describe-table --table-name ShopApp --query 'Table.Replicas'

12. More industry stories

  • Retail (Aisha Khan, Dubai): TTL clears abandoned carts; soft-delete keeps 30-day undo.
  • Bank (Lucas Meyer, Berlin): PITR on ledger table; restore drill every quarter.
  • Travel (Omar Haddad, Amman): Global Tables for catalog reads; inventory writes sticky.
  • Gaming (Carlos Rivera, Mexico City): DAX for hot profile Gets; never cache money ledgers.
  • Social (Priya Nair, Kochi): adjacency edges for follows; sparse GSI for unread inbox.
  • IoT (Mei Lin, Shenzhen): device#day PK + TTL 14 days; rollups to S3.

13. Glossary (batch 2)

TermMeaning
TTLTime to Live — attribute with epoch seconds; DynamoDB deletes later
Soft deleteFlag deleted=true; item remains until TTL/archive
PITRPoint-in-time recovery — continuous backups window
Global TableMulti-region replicated DynamoDB table
LWWLast-writer-wins conflict resolution on replicas
DAXDynamoDB Accelerator — in-memory cache cluster
Adjacency listEdge items modeling relationships in one table
Fan-outWrite once; push copies to many feeds/inboxes

14. Practice: explain out loud

  1. 60s: TTL vs soft-delete vs S3 archive.
  2. 60s: PITR vs on-demand backup.
  3. 60s: when Global Tables help and hurt.
  4. 60s: DAX consistency trade-off.
  5. 60s: CartLite checkout keys in one breath.
  6. 60s: follow edge vs feed item.
  7. Record once; cut filler; record again.

15. Safe lab hygiene

  • Use ap-south-1 labs with a budget alert.
  • Prefix lab tables with lab- and your initials.
  • Delete replicas, DAX clusters, and backups you created for labs.
  • Never enable Global Tables on production without a write plan.
  • Tag every resource Owner + Expiry.
  • Export/import labs can create large S3 bills — delete prefixes.

17. Delete / archive decision pad

NeedPreferWhy
Undo 7-30 daysSoft-delete flagUser restore
Hard expiry, no readTTL attributeAuto delete later
Compliance keep coldExport/archive to S3Cheap long store
Mistake recoveryPITR / backupTable-level restore

18. Ten oral drill questions

  1. What units does DynamoDB TTL use?
  2. Does TTL delete instantly?
  3. PITR window typical length?
  4. Global Tables conflict default?
  5. When sticky writes matter?
  6. DAX vs API Gateway cache?
  7. Cart PK for a shopper?
  8. Order lines under which PK?
  9. Follow edge SK example?
  10. Unread inbox sparse GSI idea?

19. RPO / RTO mini workshop

  1. Write the worst data loss you can accept (RPO) in minutes.
  2. Write how long restore may take (RTO) before business pain.
  3. Map: on-demand backup vs PITR vs multi-region replica.
  4. Schedule a restore drill on a lab table this month.
  5. Name the person who runs the drill.

20. Cost watch list (batch 2)

  • PITR storage and on-demand backup storage.
  • Global Tables: replicated WCUs in every region.
  • DAX node hours even at idle.
  • Export to S3 + S3 storage + Athena later.
  • Streams + Lambda still bill during TTL deletes.

21. Project Definition of Done

  1. Access patterns written and peer-reviewed.
  2. Sample items drawn for happy path + one failure.
  3. Lab table created, exercised, deleted (or tagged Expiry).
  4. At least one conditional write or transaction exercised.
  5. Cleanup commands pasted in your notes.
  6. One interview answer recorded out loud.
  • TTL hygiene → Backup/PITR for “oops” recovery.
  • Global Tables → sticky writes from capacity/hot lessons.
  • DAX → only after Query shapes are correct.
  • P1 CartLite reuses modeling + transactions.
  • P2 Social reuses adjacency + Streams ideas.

23. Quick self-check (yes/no)

  1. I can explain TTL delay vs instant DeleteItem.
  2. I know when soft-delete beats hard delete.
  3. I can start PITR and say what restore costs in time.
  4. I can name one reason NOT to use Global Tables.
  5. I know DAX does not fix a bad partition key.
  6. I can sketch CartLite USER# and ORD# keys.
  7. I can sketch a follow edge item.
  8. I delete lab replicas/DAX/backups the same day.

24. Common errors and fixes

Each row is a real mistake students and juniors hit. Read the fix before you panic.

Error / symptomFix
TTL in millisecondsUse seconds.
Expect instant deleteDesign for delay.
TTL on money ledgerArchive+soft only.
No soft-delete undoFlag first.
Forget Streams deletesFilter or accept noise.
expiresAt wrong attrMatch enable name.
Paid cart still has TTLRemove attr on pay.
Archive after deleteExport first.
No storage alarmAlarm growth.

25. Troubleshooting checklist

  1. TTL not Enabled: describe-time-to-live.
  2. Items remain: wait; check epoch seconds.
  3. Wrong deletes: verify attribute name.
  4. Huge Streams: TTL deletes emit events.
  5. Bill still high: check GSIs and backups too.

26. FAQ

Q. Should I always use a single table?

Often yes for related access patterns; not a religion. Separate tables are fine for unrelated domains or different capacity/security needs.

Q. Do I still need RDS?

Yes for heavy joins, multi-row SQL reports, and strong relational constraints. See the intro DynamoDB vs RDS chapter.

Q. Is Scan evil?

Scan is a tool for rare admin jobs and small tables. Design so hot paths use Query/GetItem.

Q. Where are Put/Get command details?

In the CRUD survey, CLI and SDK chapters — this deep set links them instead of copying.

Q. Is TTL free?

Delete processing has nuances; storage drops after delete.

Q. Can I recover TTL-deleted items?

No — use backup/PITR/soft-delete.

Q. Soft-delete forever?

No — add TTL or archive policy.

Q. Link CRUD?

Yes for Put/Update syntax.

27. Interview Q&A

Q. (B) TTL units?

Epoch seconds on a Number attribute.

Q. (B) Instant delete?

No — eventual, often hours.

Q. (M) Soft vs TTL?

Soft=undo flag; TTL=eventual remove.

Q. (M) Streams on TTL?

Deletes can appear as REMOVE events.

Q. (S/MNC) Compliance keep?

Export/archive then delete; PITR separate.

Q. (MNC) Cart TTL after pay?

Clear expiresAt on successful checkout.

28. Tips in English, Marathi and Hindi

Ravindra Bagale's Tip (EN)

TTL is a janitor with a delay — design UX and Streams for that.

Ravindra Bagale's Tip (MR)

TTL janitor delay — UX आणि Streams साठी design.

Tip (HI)

TTL तुरंत नहीं — UX और Streams ध्यान में रखो।

Try at home

  1. Write delete policy for one app table.
  2. Enable TTL on a lab table; disable after.
  3. Re-read CRUD TTL note; note what this deepens.

In short

  1. TTL = epoch seconds, eventual delete.
  2. Soft-delete for undo; S3 for cold keep.
  3. Clear TTL on items that must live.
  4. Link CRUD; do not rewrite Put basics.

Revision, interview and comparison cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Revision card: TTL is a janitor. Expecting instant delete surprises users and Streams. Number epoch seconds → enable TTL → design for delay. Name attr → put expiresAt → describe-time-to-live → alarm storage. Amina Otieno documents that deletes may take hours.
Revision card: TTL is a janitor · Download PNG · Open card page
Revision card: Soft-delete vs archive. Hard delete removes undo; cold keep needs S3. Flag deleted for undo; TTL later; export for compliance. Write policy → soft window → TTL/archive → test restore story. Vikram Patil clears expiresAt on paid carts.
Revision card: Soft-delete vs archive · Download PNG · Open card page
Comparison: TTL janitor vs Soft-delete flag. TTL janitor: Eventual remove after epoch seconds. Soft-delete flag: Keep item with deleted flag and optional later TTL.
Comparison: TTL janitor vs Soft-delete flag · Download PNG · Open card page
Comparison: TTL delete vs S3 archive. TTL delete: Item removed eventually after TTL. S3 archive: Copy to S3 then delete from table.
Comparison: TTL delete vs S3 archive · Download PNG · Open card page
Interview card: Enable TTL lab. TTL never runs if the attribute is not enabled. update-time-to-live then describe-time-to-live. Create lab → enable expiresAt → put item → describe → delete.
Interview card: Enable TTL lab · Download PNG · Open card page
Interview card: Epoch seconds. Millisecond values expire in the far future or never as intended. Store expiresAt as epoch seconds Number. Compute now+ttl_secs → Put N → document units in the runbook.
Interview card: Epoch seconds · Download PNG · Open card page
Interview card: TTL and Streams. Surprise REMOVE floods overwhelm consumers. TTL deletes can emit stream events — filter or capacity plan. Enable Streams knowingly → idempotent consumer → DLQ.
Interview card: TTL and Streams · Download PNG · Open card page
Interview card: Delete policy. No written policy mixes undo and compliance. Soft window / TTL hard / S3 cold — pick per entity. Table → entity → mode → owner → review date.
Interview card: Delete policy · Download PNG · Open card page

More cards: Ravi Sir's Cards.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

Maple Leaf Books is a bookselling business in Toronto. Emily Tremblay looks after its IT and cloud work. They count money in CAD and run their AWS resources in the ca-central-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 16

Easy Choose the expiry timestamp unit

You are adding a numeric expiresAt attribute for DynamoDB TTL. Does it take Unix epoch seconds or milliseconds? Enter the unit.

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Easy Validate an expired session while it still exists

A session item is past expiresAt but still present. Which item value should the application compare to the current time before allowing access?

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Easy Hide a record without deleting it yet

A user must be able to restore an item for a day after removing it from view. Name the approach that marks it deleted while retaining the record.

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Easy Move cold copies out of the primary table

An audit team wants a retained object copy of older records for occasional analysis, apart from the DynamoDB table. Name the AWS object storage service.

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Easy Describe a DynamoDB table

CLI.

Describe table ShopApp

  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy Delete modes YAML

Pick.

YAML: undo: soft-delete, hard: ttl, cold: s3.

  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.

Easy Measure whether TTL is removing expired items

Which DynamoDB metric counts items deleted by the TTL background process?

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Medium Fix an expiry that was saved in milliseconds

A client stored a Unix epoch millisecond value. By what factor should it divide to get epoch seconds for TTL? Enter the number.

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Medium Preserve a paid cart

A draft cart’s TTL attribute expiresAt is set. The same item becomes a paid cart that must remain. Which attribute should the checkout update remove?

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Medium Read the event associated with expiration

What DynamoDB Streams eventName is generated when an item is deleted by a TTL service action?

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Medium Validate a record’s deadline before using it

A TTL-enabled item has not been physically deleted yet. Which field should the app check against now if it wants to disallow an expired item immediately?

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Medium TTL CLI names YAML

Names.

YAML list apis with update-time-to-live and describe-time-to-live.

  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.

Hard Inspect whether the table has expiration enabled

Which read-only API returns whether TTL is enabled and what attribute name it uses?

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Hard Recover a record already removed by TTL

A required record was cleaned up by TTL. The table had continuous recovery enabled at the time. Which feature can restore a new table to a point before that deletion?

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Hard Apply a targeted expiry to one existing draft

You know an existing draft’s entire key and only want to set its expiration attribute, without replacing the other attributes. Which DynamoDB write API should you use?

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.

Hard Use the same attribute name in data and configuration

TTL is configured for expiresAt, but the app writes expires_at instead. What attribute name must the app use for TTL in this configuration?

  1. Read the situation and identify what you need to decide.
  2. Work through the relevant concept or calculation.
  3. Enter only the requested term or number.
  4. Check your answer, then read the explanation.