Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 37 ยท Projects and Practice

Practice: Design Challenges

Practice DynamoDB design with worked challenges: chat, blog comments, multi-tenant SaaS, IoT readings and cost estimates, plus common interview questions.

You learn DynamoDB modeling by doing it many times. This final lesson gives you five design challenges with worked answers, a cost-estimate exercise you can run, and a list of common interview questions. For each challenge, try it yourself first: write the access patterns, then the keys, and only then open the solution.

Remember the rules that apply to every answer: a pattern must map to one GetItem or Query on a table or index, related items share a partition key, the sort key orders them, and anything else needs an index or a different design.

Challenge 1: chat rooms

Patterns: send a message to a room, read the latest 50 messages of a room, list the rooms a user has joined, list the members of a room.

Solution
Item PK SK
Message ROOM#<id> MSG#<timestamp>#<msgId>
Membership ROOM#<id> MEMBER#<userId>
Membership (inverted) USER#<id> via GSI ROOM#<id>
  • Latest 50: Query PK = ROOM#id, begins_with(MSG#), ScanIndexForward=false, Limit=50.
  • Members: begins_with(MEMBER#). User's rooms: the GSI with GSI1PK = USER#id (an adjacency list).
  • A very busy room is a hot partition: add a time bucket to the key (ROOM#id#2026-03-09) and read the newest bucket first, going back for older pages.

Challenge 2: blog with comments and tags

Patterns: get a post, list its comments oldest first, list the latest posts of an author, list posts with a tag.

Solution

Post and comments share PK = POST#id: SK = META for the post and SK = COMMENT#<time>#<id> for comments (one query for the page and comments). Author listing: GSI1PK = AUTHOR#id, GSI1SK = <publishedAt>. Tags: one edge item per post-tag pair (PK = TAG#python, SK = <publishedAt>#<postId>), written together with the post in a transaction. Do not store the tag list only on the post: you could not query by tag.

Challenge 3: multi-tenant SaaS

Patterns: every query is scoped to one tenant; list projects of a tenant; list tasks of a project; one very large tenant has 100 times the traffic.

Solution

Put the tenant first in every partition key: PK = TENANT#<id>#PROJECT#<pid> with SK = META or TASK#<id>. This also lets IAM LeadingKeys conditions isolate tenants. Large tenants are spread by project in the key, so one huge tenant does not pile on one partition. For stricter isolation or per-tenant billing, give the largest tenants their own table.

Challenge 4: IoT readings

Patterns: write 5,000 readings per second from 100,000 devices; get the latest reading of a device; list readings of a device in a time range; keep 90 days.

Solution

PK = DEVICE#<id>, SK = <iso timestamp>. Many devices means the writes spread naturally. Latest reading: Query reversed with Limit=1. Range: SK BETWEEN :from AND :to. Retention: a TTL attribute 90 days after the reading. If a device writes more than about 1,000 items per second, add a time bucket to the key. For long-term analytics export to S3 (integrations).

Challenge 5: unique usernames

Patterns: create a user with a unique username and a unique email; log in by email.

Solution

DynamoDB enforces uniqueness only for the primary key. Use a transaction with three items: the user (PK = USER#id), a lock for the username (PK = USERNAME#ana, condition attribute_not_exists(PK)) and a lock for the email (PK = EMAIL#[email protected], the same condition). The email item also holds the userId, so login is a GetItem. If either lock exists, the whole transaction cancels (transactions).

Exercise: estimate the cost

Before choosing a design, estimate capacity units. Fill in the numbers for your own workload.

Python Monthly capacity cost estimator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Prices shown are examples, not current quotes. The structure matters: steady traffic usually favors provisioned capacity, while spiky or unknown traffic favors on-demand (capacity modes).

Common interview questions

Practice answering each in two or three sentences.

  1. What is the difference between a partition key and a sort key? Why does the sort key matter for queries?
  2. Why can Scan be a problem, and when is it acceptable (small tables, one-off exports)?
  3. Compare a GSI with a local secondary index: key choice, consistency, size limits, when they can be created.
  4. How do you make a write idempotent or prevent lost updates?
  5. When would you choose on-demand over provisioned capacity?
  6. What is a hot partition and how do you fix it?
  7. Eventually versus strongly consistent reads: cost and availability.
  8. How do you model many-to-many relationships?
  9. What does DynamoDB Streams guarantee about ordering and delivery?
  10. How would you search by an attribute that is not a key?
  11. Why can't DynamoDB enforce unique constraints beyond the primary key, and how do you work around it?
  12. How do you migrate when a new access pattern appears?
Python Self-check: does the design pass the rules?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Where to go next

  • Rebuild the projects with a real table in a free AWS account or DynamoDB Local, and write the access-pattern tests.
  • Read your own workload's CloudWatch metrics and Contributor Insights to find hot keys.
  • Try NoSQL Workbench to model keys and visualize item collections.
  • Revisit the earlier chapters on keys, queries and indexes with your new designs in mind.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the chat room design, why add a time bucket to the room partition key?

    A single room key can be a hot partition. Buckets divide the load and let you read newest first.
  2. 2. How is a unique username enforced?

    Only primary keys are unique. A lock item per value, created conditionally, enforces it.
  3. 3. Which design handles 'posts with a tag' correctly?

    Edge items make tag lookups a key query instead of a scan.
  4. 4. Which workload usually fits provisioned capacity better?

    Steady load can be reserved and auto scaled cheaply. Unknown or idle workloads suit on-demand.

Summary

  • Start every design with access patterns; verify each maps to one key-based operation.
  • Chat, blog, SaaS, IoT and unique-field problems all reduce to the same few patterns: item collections, adjacency lists, time buckets, lock items and sparse indexes.
  • Estimate capacity units and cost before committing to a mode.
  • Practice explaining trade-offs: consistency, hot keys, indexes and transactions.
  • You have now completed this part of the DynamoDB course.

Next: Back to the DynamoDB tutorial