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

Project: Ecommerce Cart and Orders on a DynamoDB Single Table

Projects prove the earlier chapters. Dubai retail Aisha Khan and Pune founder Vikram Patil ship CartLite: USER# profile+cart, ORD# lines, conditional cart updates, checkout transaction sketch. Reuse modeling and transactions — link CRUD for Put syntax.

What you'll learn in this chapter

  • Sketch CartLite keys
  • Mutate cart with conditions
  • Sketch checkout transaction
  • Industry parallels
  • Build lab + cleanup

1. Everyday example: kirana notebook checkout

Shopper page = USER# collection. Cart lines under CART#. Checkout writes ORD# header + ITEM# lines and clears cart — all-or-nothing mindset from transactions chapter.

2. Vertical steps

Patterns -> Keys -> Lab writes -> Checkout sketch -> CleanupPatternsKeysLab writesCheckout sketchCleanup
Cleanup

3. Why do we need this?

Why do we need this?

Reading modeling without building leaves interviews weak. CartLite is the smallest ecommerce slice that forces PK/SK, conditions, and a txn boundary.

4. What is it?

What is it?

Single-table CartLite: PK=USER#id SK=PROFILE|CART#sku|ORD#time#id; PK=ORD#id SK=META|ITEM#sku. Conditional qty updates; checkout TransactWrite sketch.

5. How to do it (step by step)

How to do it (step by step)

  1. Write 8 access patterns (get profile, get cart, add line, checkout…).
  2. Draw sample items for one user + one order.
  3. Create lab-ddk-cart; Put profile + 2 cart lines.
  4. UpdateItem qty with condition version or qty bound.
  5. On paper: TransactWrite checkout (ORD meta+items, delete cart lines).
  6. Query USER# and ORD#; then delete table.

6. Key sketch

EntityPKSK
UserUSER#u1PROFILE
Cart lineUSER#u1CART#sku9
OrderUSER#u1ORD#2026-11-02#o1
Order metaORD#o1META
Order itemORD#o1ITEM#sku9

7. Commands with output and meaning

aws dynamodb create-table --table-name lab-ddk-cart \
  --attribute-definitions AttributeName=PK,AttributeType=S AttributeName=SK,AttributeType=S \
  --key-schema AttributeName=PK,KeyType=HASH AttributeName=SK,KeyType=RANGE \
  --billing-mode PAY_PER_REQUEST --region ap-south-1
aws dynamodb put-item --table-name lab-ddk-cart --item '{"PK":{"S":"USER#u1"},"SK":{"S":"PROFILE"},"EntityType":{"S":"User"},"name":{"S":"Ananya"}}'
aws dynamodb put-item --table-name lab-ddk-cart --item '{"PK":{"S":"USER#u1"},"SK":{"S":"CART#sku9"},"qty":{"N":"2"},"version":{"N":"1"}}'
aws dynamodb query --table-name lab-ddk-cart --key-condition-expression "PK = :pk AND begins_with(SK, :c)" \
  --expression-attribute-values '{":pk":{"S":"USER#u1"},":c":{"S":"CART#"}}'
# Output: cart lines. Meaning: item collection query for cart.

create on-demand; put profile+cart; query begins_with CART#. Checkout txn stays paper+SDK linked to transactions chapter.

8. Industry stories

  • Retail (Aisha): same keys for mobile+web cart.
  • Food (Diego): CART# + storeId sticky region.
  • EdTech (Sofia): cart = course wishlist.
  • Travel (Omar): cart holds fare quotes with short TTL.
  • SaaS (Omar): cart = seat add-ons before invoice.
  • Media (Priya): cart = rental queue.

9. Lab

How to do it (step by step)

  1. Patterns + draw + create lab-ddk-cart.
  2. Put profile, 2 cart lines; conditional qty bump.
  3. Paper checkout txn; Query; delete table.

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
No pattern listWrite 8 first.
Cart under ORD PKCart under USER.
Blind qty PutCondition/version.
Checkout partial writesTransactWrite sketch.
Forget clear cartDelete CART# lines in txn.
Scan for cartQuery begins_with.
Rewrite Put chapterLink CRUD.
No EntityTypeAdd it.
Leave lab tableDelete same day.

25. Troubleshooting checklist

  1. Empty cart Query: SK prefix CART#.
  2. Condition fail: reload version.
  3. Txn cancel: CancellationReasons.
  4. Throttle one user: hot USER — see sharding.
  5. Need joins: rethink keys / RDS question.

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. Must use txn?

For checkout invariants yes — sketch first.

Q. GSI required?

Not for this minimal cart.

Q. TTL on cart?

Yes for abandoned — see TTL chapter.

Q. SDK deep?

Link SDK + transactions chapters.

27. Interview Q&A

Q. (B) Cart PK?

USER#id collection.

Q. (B) Order lines PK?

ORD#id with ITEM# SK.

Q. (M) Qty race?

Conditional UpdateItem / version.

Q. (M) Checkout atomic?

TransactWrite across ORD+cart clears.

Q. (S/MNC) Scale carts?

Per-user partitions; watch hot celebs later.

Q. (MNC) Abandoned?

TTL + soft-delete policy.

28. Tips in English, Marathi and Hindi

Ravindra Bagale's Tip (EN)

Ship the smallest cart that forces conditions and a txn boundary.

Ravindra Bagale's Tip (MR)

सगळ्यात छोटा cart + condition + txn.

Tip (HI)

छोटा cart, condition, txn — यही काफी।

Try at home

  1. 8 patterns on paper.
  2. Lab put+query cart.
  3. Write checkout txn bullet list.

In short

  1. CartLite = USER# + ORD# collections.
  2. Conditions protect qty.
  3. Checkout = txn sketch.
  4. Cleanup labs.

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: CartLite keys. Cart under wrong PK forces Scans. USER# + CART# for cart; ORD# + ITEM# for lines. 8 patterns → draw samples → Put → Query begins_with. Aisha Khan reuses the same keys on mobile and web.
Revision card: CartLite keys · Download PNG · Open card page
Revision card: Checkout boundary. Partial checkout writes leave ghost carts. TransactWrite: ORD meta+items and clear CART# lines. Paper txn → conditions → CancellationReasons practice. Vikram Patil treats checkout as a txn interview story.
Revision card: Checkout boundary · Download PNG · Open card page
Comparison: USER cart vs ORD lines. USER cart: CART lines under USER partition. ORD lines: ITEM lines under ORD partition.
Comparison: USER cart vs ORD lines · Download PNG · Open card page
Comparison: Conditional qty vs Checkout txn. Conditional qty: UpdateItem with version or qty condition. Checkout txn: TransactWrite ORD plus clear cart.
Comparison: Conditional qty vs Checkout txn · Download PNG · Open card page
Interview card: Eight patterns. Coding without patterns recreates Scans. Write 8 CartLite access patterns in English first. Paper list → peer review → then keys.
Interview card: Eight patterns · Download PNG · Open card page
Interview card: Query the cart. Scan for one user cart is wrong. Query PK=USER# and begins_with SK CART#. Put lines → Query → assert count → delete lab.
Interview card: Query the cart · Download PNG · Open card page
Interview card: Qty version. Blind qty overwrites lose concurrent edits. version attribute + condition on UpdateItem. Read → edit → conditional write → retry on fail.
Interview card: Qty version · Download PNG · Open card page
Interview card: Paper checkout. Jumping to code without txn bullets fails interviews. List TransactWrite actions: ORD meta, items, clear cart. Bullets → map to API → CancellationReasons note.
Interview card: Paper checkout · Download PNG · Open card page

More cards: Ravi Sir's Cards.

Practice Tasks

About this scenario

Milan Furniture is a furniture business in Milan. Giulia Rossi looks after its IT and cloud work. They count money in EUR and run their AWS resources in the eu-south-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 Cart under USER# — 1

Why: Key.

What to do: Enter 1 if cart lines live under USER# with CART# SK.

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 Lines under ORD# — 1

Why: Key.

What to do: Enter 1 if order lines use ORD# PK and ITEM# SK.

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 Qty needs conditions — 1

Why: Race.

What to do: Enter 1 if qty updates should use conditions/version.

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 Checkout uses txn sketch — 1

Why: Atomic.

What to do: Enter 1 if checkout invariants use a TransactWrite sketch.

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 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 Cart key YAML

Why: Sketch.

What to do: YAML: pk: USER#u1, sk: CART#sku9.

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 Link modeling + transactions — 1

Why: Reuse.

What to do: Enter 1 if this project reuses modeling and transactions chapters.

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 Patterns before keys — 1

Why: Process.

What to do: Enter 1 if access patterns come before inventing keys.

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 Abandoned carts use TTL — 1

Why: Policy.

What to do: Enter 1 if abandoned carts often use TTL/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.

Medium Query begins_with CART# — 1

Why: Shape.

What to do: Enter 1 if cart reads use Query begins_with CART#.

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 EntityType helps — 1

Why: Hygiene.

What to do: Enter 1 if EntityType helps filters/Streams.

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 Checkout flow YAML

Why: Steps.

What to do: YAML: write: order, clear: cart.

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 Minimal cart may skip GSI — 1

Why: Scope.

What to do: Enter 1 if minimal CartLite may not need a GSI.

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 Clear cart in checkout txn — 1

Why: Txn.

What to do: Enter 1 if checkout should clear CART# lines in the txn.

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 Do not Scan for cart — 1

Why: Anti.

What to do: Enter 1 if Scan is the wrong tool for a user cart.

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 Delete lab same day — 1

Why: Hygiene.

What to do: Enter 1 if lab-ddk-cart should be deleted same day.

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.