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

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.

What you'll learn 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. Why do we need this?

Why do we need this?

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. What is it?

What is it?

TTL: number attribute (epoch seconds). DynamoDB deletes typically within about 48h after expiry — not instant. Soft-delete: deleted=true still Queryable. Archive: export or app copy to S3 then delete.

5. How to do it (step by step)

How to do it (step by step)

  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

How to do it (step by step)

  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

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 TTL uses epoch seconds — 1

Why: Units.

What to do: Enter 1 if TTL uses epoch seconds on a Number attribute.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy TTL delete is eventual — 1

Why: Timing.

What to do: Enter 1 if TTL deletes are eventual not always instant.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Soft-delete keeps the item — 1

Why: Undo.

What to do: Enter 1 if soft-delete flags an item but keeps it.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Archive cold data to S3 — 1

Why: Cold.

What to do: Enter 1 if cold compliance copies often go to S3.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Describe a DynamoDB table

Why: CLI.

What to do: Describe table ShopApp

How:
  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

Why: Pick.

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

How:
  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 Watch storage growth — 1

Why: Ops.

What to do: Enter 1 if storage growth alarms help catch missing expiry.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Do not store TTL in ms — 1

Why: Trap.

What to do: Enter 1 if milliseconds in expiresAt is a common mistake.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Clear TTL after payment — 1

Why: Cart.

What to do: Enter 1 if paid carts should clear expiresAt.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium TTL can emit Streams REMOVE — 1

Why: CDC.

What to do: Enter 1 if TTL deletes can appear as stream REMOVE events.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Avoid TTL on money ledgers — 1

Why: Safety.

What to do: Enter 1 if money ledgers should not rely on TTL alone.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium TTL CLI names YAML

Why: Names.

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

How:
  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 Enable TTL before relying on it — 1

Why: Order.

What to do: Enter 1 if the table TTL setting must be enabled for the attribute.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard TTL delete not easily undone — 1

Why: Risk.

What to do: Enter 1 if recovering TTL-deleted items usually needs backup/PITR/soft-delete.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard Link CRUD for Put syntax — 1

Why: No dup.

What to do: Enter 1 if Put/Update syntax stays in linked CRUD chapter.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard Attribute name must match enable — 1

Why: Match.

What to do: Enter 1 if expiresAt name must match the enabled TTL attribute.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.