Skip to the page

NoSQL and MongoDB · senior

Key design in DynamoDB and Cassandra

These stores can answer only the queries their keys support. You start from the access patterns and choose partition and sort keys, then handle hot partitions.

The notes

What you must know

  • Write the list of access patterns first. Tables are shaped around them, not around entities.
  • Orders for a user, newest first: partition key user_id, sort key created_at plus order_id. Read in reverse and page with the last key.
  • Lookup by order id: the base item keyed by order_id with a global secondary index on user_id (DynamoDB), or a second table orders_by_id (Cassandra).
  • Restaurant orders for today: partition key restaurant_id#date, so partitions stay bounded, sorted by time.
  • Hot partitions: one very busy key overloads one partition. Split it with a suffix (restaurant_id#date#0 to 9) and read all suffixes.
  • Keep the views in sync with DynamoDB TransactWriteItems or streams, or in Cassandra with idempotent writes and retries. Cassandra logged batches are costly.
  • Consistency: DynamoDB reads are eventually consistent unless you ask for a strong read, and strong reads are not available on global secondary indexes. Cassandra lets you pick a level such as QUORUM.
  • For reports, stream or export to a warehouse. Do not scan the table.

The red pen

Common traps: mistake → fix

  • Mistake: Design the tables like SQL and filter with scans

    Fix: a scan reads everything and is slow and costly.

  • Mistake: One partition per restaurant forever

    Fix: it grows without limit and becomes hot; include the date.

  • Mistake: All reads are strongly consistent

    Fix: by default they are not.

Easy to hard

Questions interviewers ask, easy to hard

  1. Design order history tables in DynamoDB.
  2. What is a hot partition and how do you fix it?
  3. How do you support a second query, by order id?
  4. How do you keep two views in sync?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview