Scrivio Interview library
DesignFoundations7 min read · updated 12 November 2026

For a DynamoDB-style single table, model these access patterns for an e-commerce app: get a customer by id, list a customer's orders newest first, and get an order with its items. Show partition and sort keys.

Store the customer as partition key CUSTOMER#123 with sort key PROFILE. Store order summaries in the same partition with sort keys such as ORDER#2026-03-01T10:00#987, so a Query on CUSTOMER#123 with begins_with(SK, 'ORDER#') and descending order returns orders newest first. Store each order's details under partition ORDER#987 with an ORDER item and ITEM#1, ITEM#2 and so on, so one Query returns the order and its items. Other patterns, such as orders by status, get a global secondary index, and the access patterns are listed before the table is designed, because adding one later may mean a backfill.

Why this question is asked

DynamoDB rewards a way of modelling that feels backwards to anyone trained on relational databases: we start from the queries and shape the keys so each query reads one contiguous range of items. Single-table design takes this furthest, putting several entity types in one table with generic key names. Interviewers ask this to see whether we can turn access patterns into partition and sort keys and explain why each key looks the way it does. In this answer, we will recap DynamoDB's keys and the Query operation, list the access patterns, design each one, look at the whole table, add a secondary index, and see why the list of patterns comes first.

1A Quick Recap of DynamoDB Keys

Every item in a DynamoDB table has a primary key. With a composite primary key it has two parts: the partition key, which DynamoDB hashes to choose the partition that stores the item, and the sort key, which orders items that share a partition key. All items with the same partition key form an item collection, stored together and sorted by sort key.

There are two ways to read many items. Query reads one item collection: it requires an exact partition key and can add a condition on the sort key, such as equals, between, greater than or begins_with, and it can return results in ascending or descending order. Scan reads the whole table and is far too expensive for a request path. So the whole design aims at making every access pattern a Query. In single-table design we name the key attributes generically, PK and SK, because different item types store different things in them.

2Listing the Access Patterns First

We write down every query the application will make, with its parameters and expected size, before designing anything. Here there are three: get a customer by id; list a customer's orders, newest first, perhaps 20 at a time; and get an order with all its items. Each pattern becomes one row in a small table that says which key and condition will serve it. If a pattern cannot be served by a Query on the table or an index, the design is not finished.

3Designing Each Pattern

The customer is easy: PK = CUSTOMER#123 and SK = PROFILE, read with GetItem. The prefix in CUSTOMER#123 tells us what kind of key it is and prevents collisions with other entity types.

For the customer's orders, we put an order summary item in the customer's partition, with a sort key that starts with ORDER# followed by the order time in ISO 8601 format and the order id: ORDER#2026-03-01T10:00#987. ISO timestamps sort correctly as strings, so the sort key order is time order. A Query on PK = CUSTOMER#123 with begins_with(SK, 'ORDER#') returns only the orders, not the PROFILE item, and asking for descending order returns the newest first. The order id at the end keeps keys unique when two orders share a minute.

For an order with its items, the order gets its own partition, PK = ORDER#987. It holds one item with SK = ORDER, carrying status, totals and addresses, and one item per line with SK = ITEM#0001, ITEM#0002 and so on. A Query on PK = ORDER#987 returns the order and all its lines in one request. Notice the zero padding: as strings, ITEM#10 sorts before ITEM#2, so we pad the number to keep lines in order.

Java
// AWS SDK for Java 2.x: list customer 123's orders, newest first
QueryRequest req = QueryRequest.builder()
.tableName("shop")
.keyConditionExpression("PK = :pk AND begins_with(SK, :prefix)")
.expressionAttributeValues(Map.of(
":pk", AttributeValue.fromS("CUSTOMER#123"),
":prefix", AttributeValue.fromS("ORDER#")))
.scanIndexForward(false) // descending sort key: newest first
.limit(20)
.build();
dynamo.query(req).items();
// ORDER#2026-03-01T10:00#987, ORDER#2026-02-14T18:30#912, ... (PROFILE not included)
// Get order 987 with its items in one request
// keyConditionExpression("PK = :pk"), :pk = ORDER#987
// -> ITEM#0001, ITEM#0002, ITEM#0003, ORDER

Notice that ORDER sorts after the ITEM# keys, because the letter O comes after I. If we want the header first, we can name it with a character that sorts earlier, such as A#ORDER or #ORDER. The summary in the customer partition and the header in the order partition hold some of the same data, such as the total and status; we write both in one TransactWriteItems call when an order is placed or its status changes, so they stay in step.

4The Table as a Whole

Putting it together, one table holds three kinds of items. This is what people mean by single-table design: the table is not organised by entity, but by the item collections our queries read.

Items in the shop table
PKSKAttributesServes
CUSTOMER#123PROFILEname, emailGet customer
CUSTOMER#123ORDER#2026-03-01T10:00#987total, status, item countCustomer orders, newest first
CUSTOMER#123ORDER#2026-02-14T18:30#912total, statusCustomer orders
ORDER#987ITEM#0001sku, name, qty, priceOrder with items
ORDER#987ITEM#0002sku, name, qty, priceOrder with items
ORDER#987ORDERstatus, totals, address, customer idOrder with items
Each access pattern reads one item collection with one Query.

5Adding a Global Secondary Index for Other Patterns

Suppose the warehouse team needs all orders with status PAID, oldest first. Neither key supports that. A global secondary index, GSI, is a second copy of selected items with a different partition and sort key, maintained by DynamoDB asynchronously. We give the ORDER items two extra attributes, GSI1PK = STATUS#PAID and GSI1SK = 2026-03-01T10:00#987, and query the index on GSI1PK.

There is a catch: a status has only a handful of values, so all paid orders would share one partition key in the index, which concentrates writes on one partition. If the volume is large, we spread it by adding the day, STATUS#PAID#2026-03-01, and the warehouse queries one day at a time. Index reads are eventually consistent, typically a fraction of a second behind, which is fine for a work queue. Indexes also cost: every write to an indexed item also writes the index, so each GSI adds write capacity.

Step throughFrom access pattern to key design
  1. Write down the query and its parameterse.g. orders by status, oldest first
  2. Served by the table's PK and SK?
    YesUse Query on the tableNoNext question
  3. Can a GSI key express it?
    YesAdd GSI, spread hot valuesNoRethink, or use another store
  4. Backfill attributes on existing itemsif the pattern is new

4 steps in order. Play them, or step through them one at a time.

6Why the Patterns Must Come First

In a relational database we can add a new query next year and create an index for it. In DynamoDB, a new pattern often needs new key attributes on existing items, which means a backfill: a job that scans the table and updates every relevant item, while the application starts writing the new attributes for new items. A GSI added later only indexes items that have its key attributes, so without the backfill old items are invisible to it. On a table with hundreds of millions of items that is a real project with real cost.

That is why the design starts with the list of access patterns, reviewed with the product team, and why the list is kept as a living document next to the table definition. Patterns that are analytical, such as monthly revenue by category, are better exported to a warehouse than forced into the table.

7Follow-up questions

How do you paginate the customer's orders?

The Query response includes LastEvaluatedKey when there are more results. We pass it as ExclusiveStartKey in the next request, usually encoded as an opaque cursor in the API, so the next page continues exactly after the last order returned.

Is there a size limit on an item collection?

A single item can be at most 400 KB. Item collections have no size limit unless the table has a local secondary index, in which case each collection is limited to 10 GB. Very large collections can also become hot partitions, so customers with huge numbers of orders may need a bucketed key.

8Conclusion

In this answer, we saw how to turn three access patterns into DynamoDB keys. The customer is PK CUSTOMER#123 with SK PROFILE, order summaries share that partition with ISO-timestamped sort keys so begins_with ORDER# in descending order lists them newest first, and each order has its own partition with an ORDER item and zero-padded ITEM# items returned by one Query. A global secondary index, spread to avoid hot values, serves patterns such as orders by status, and the list of access patterns comes first because adding one later can require a backfill.

What to say in the interviewAbout 80 seconds aloud

Open with

I would start by listing the access patterns, because in DynamoDB every request should be a Query on one partition key. For get customer by id, the item has PK CUSTOMER#123 and SK PROFILE, read with GetItem.

Then, if they want more

For a customer's orders newest first, I put an order summary in the same partition with a sort key like ORDER#2026-03-01T10:00#987: the ISO timestamp sorts correctly as a string and the order id keeps it unique.

Querying PK CUSTOMER#123 with begins_with on ORDER# and ScanIndexForward false returns the orders newest first and skips the PROFILE item, with a limit and LastEvaluatedKey for paging.

For an order with its items, the order gets its own partition, ORDER#987, with an ORDER item for the header and items ITEM#0001, ITEM#0002, zero padded so they sort properly, so one Query returns the whole order. The summary and the header share some fields, so I write both with TransactWriteItems.

For other patterns such as orders by status I would add a global secondary index on attributes like STATUS#PAID plus the date, spreading hot values by day. And I would stress that the patterns come first: adding a new one later usually means new attributes and a backfill over existing items.

About this answer

Last updated
12 November 2026
Versions it names
Java 2
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Redis documentation, MongoDB manual
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "For a DynamoDB-style single table, model these access patterns for an e-commerce app...": https://scrivio-interview-library.netlify.app/databases/nosql-fundamentals/6864bdeb78/