Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

Explain DynamoDB single-table design with an example: store customers, orders and order items in one table so that 'get customer with recent orders' and 'get order with items' are each one Query. Show the key layout.

We give every item generic keys PK and SK. A customer profile is PK = CUSTOMER#123, SK = PROFILE, and each order is PK = CUSTOMER#123, SK = ORDER#2026-11-01#987, so one Query on PK = CUSTOMER#123 returns the customer with their orders, newest first with ScanIndexForward=false. Order items live under PK = ORDER#987 with SK = ITEM#001 and so on, next to an order header item with SK = HEADER, so a second single Query returns an order with all its items.

Why this question is asked

Single-table design is DynamoDB's answer to joins. A relational database would store customers, orders and order items in three tables and join them at read time; DynamoDB has no joins, so we arrange the data in advance so that related items sit together and come back in one request. Interviewers ask for the key layout because it shows whether we really understand partition keys, sort keys and item collections. In this answer, we will list the access patterns, design the keys, run both queries, see how code maps mixed results back to classes, weigh the costs, and see how a new access pattern is added later.

1Starting From the Access Patterns

DynamoDB modelling starts with the questions, not the entities. We have two: 'get a customer with their recent orders' and 'get an order with its items'. Each should be one Query, which means the items it returns must share one partition key, an item collection, and be ordered usefully by the sort key.

So customer data and that customer's orders must share a partition key, and an order's header and its items must share another. That is the whole design, and the rest is choosing the exact values.

2The Key Layout

We use the generic attribute names PK and SK so that each entity type can store its own kind of value. A prefix such as CUSTOMER# or ORDER# tells us what kind of key a value is and stops ids of different types from colliding.

Items in the table
PKSKtypeOther attributes
CUSTOMER#123PROFILECustomername, email
CUSTOMER#123ORDER#2026-10-12#951Orderstatus, total
CUSTOMER#123ORDER#2026-11-01#987Orderstatus, total
ORDER#987HEADEROrdercustomerId, status, total
ORDER#987ITEM#001OrderItemsku, qty, price
ORDER#987ITEM#002OrderItemsku, qty, price
Two item collections: CUSTOMER#123 for the first query, ORDER#987 for the second.

Notice that the order appears twice: once under the customer, for the order list, and once as a HEADER under its own partition key, for the order page. Duplicating a small summary is normal in DynamoDB; we write both in one TransactWriteItems call so they never disagree. The date inside the order's sort key, ORDER#2026-11-01#987, makes orders sort by time, and the order id at the end keeps two orders on the same day apart.

3Running the Two Queries

The first query reads the customer's collection. Because PROFILE sorts after ORDER# alphabetically, a descending read returns the profile first and then the orders from newest to oldest.

Java
// Query 1: customer with recent orders
QueryRequest customer = QueryRequest.builder()
.tableName("Shop")
.keyConditionExpression("PK = :pk")
.expressionAttributeValues(Map.of(":pk", AttributeValue.fromS("CUSTOMER#123")))
.scanIndexForward(false) // PROFILE, ORDER#2026-11-01#987, ORDER#2026-10-12#951
.limit(11) // the profile plus 10 recent orders
.build();
// Only the orders: SK begins_with ORDER#
// keyConditionExpression("PK = :pk AND begins_with(SK, :o)"), :o = "ORDER#"
// Query 2: order with its items
QueryRequest order = QueryRequest.builder()
.tableName("Shop")
.keyConditionExpression("PK = :pk")
.expressionAttributeValues(Map.of(":pk", AttributeValue.fromS("ORDER#987")))
.build(); // HEADER, ITEM#001, ITEM#002

So each access pattern is one request to one partition, typically a few milliseconds whatever the table size. The alternative to the duplicated header is a GSI with GSI1PK = ORDER#987 written on the order item under the customer, which avoids the second copy in the base table at the cost of an eventually consistent index.

4Mapping Mixed Results Back to Classes

A single Query now returns items of different kinds, so each item carries a type attribute and the repository decides which class to build.

Java
List<Map<String, AttributeValue>> rows = client.query(customer).items();
Customer c = null;
List<Order> recent = new ArrayList<>();
for (var row : rows) {
switch (row.get("type").s()) {
case "Customer" -> c = CustomerMapper.from(row); // one profile
case "Order" -> recent.add(OrderMapper.from(row)); // ten orders
default -> { } // ignore types this screen does not need
}
}

Keeping this in a repository class means the rest of the application sees Customer and Order objects, and the key format is built and read in one place.

5What We Gain and What It Costs

The benefit is fewer requests: a page that would need three SQL tables or three DynamoDB tables needs one Query. Related items can also be written together in one transaction within one table. At high scale, this keeps latency low and predictable.

The costs are real. The table is harder to understand, since PK and SK mean different things on different rows. Analytics and ad hoc queries are awkward, so we usually export the data elsewhere for reporting. Migrations are harder, because changing a key means rewriting items. And the whole design depends on knowing the access patterns in advance: a question nobody planned for may need a new index or a backfill.

6Follow-up questions

How do you add a new access pattern later?

First check whether the existing keys already answer it with a different sort key condition. If not, the usual path is a new GSI, or an overloaded existing one, on attributes we add for this pattern. New writes set those attributes immediately, and a backfill job updates existing items to set them too, at which point DynamoDB indexes them. Because the GSI only holds items that have the attributes, the backfill can run gradually. In rare cases the pattern needs a new item type, such as a summary item, maintained in the same transaction as the source items.

What happens when a customer has thousands of orders?

The Query still works because it pages with LastEvaluatedKey and we only read the first page of recent orders. Without an LSI there is no 10 GB item collection limit, but a very active key can become hot, so extremely large customers may need their orders split by year or bucket.

Why put the type in an attribute instead of parsing the SK prefix?

Parsing works, but an explicit type attribute is clearer, survives key format changes and is easy to filter on in exports and analytics.

7Conclusion

In this answer, we designed a single table around two access patterns. Customer profile and orders share PK = CUSTOMER#123 with SKs PROFILE and ORDER#date#id, so one descending Query returns the customer and recent orders, and begins_with ORDER# narrows it to orders. Order header and items share PK = ORDER#987, so one Query returns the order with its items. A type attribute maps items to classes, and we weighed fewer requests against harder understanding, migration and analytics, and the need to know access patterns up front.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start from the two access patterns, because each must be a Query on one partition key. So I use generic keys, PK and SK. The customer profile is PK CUSTOMER#123, SK PROFILE, and each order is written under the same partition key with SK ORDER# plus the date plus the order id, for example ORDER#2026-11-01#987.

Then, if they want more

A Query on PK CUSTOMER#123 then returns the profile and the orders together; with ScanIndexForward false I get the profile first and orders newest first, and begins_with ORDER# on the sort key gives only orders.

For the second pattern, order items go under PK ORDER#987 with SK ITEM#001, ITEM#002, and I also write the order header there with SK HEADER, in the same transaction as the copy under the customer. A Query on ORDER#987 returns the header and all items. Alternatively a GSI with GSI1PK ORDER#987 could serve it.

Every item has a type attribute so the repository maps each row to Customer, Order or OrderItem. The benefit is one request per screen and transactions within one table. The costs are that it is harder to read, migrate and analyse, and that access patterns must be known up front.

To add a pattern later, I add attributes and a GSI, set them on new writes and backfill existing items.

About this answer

Last updated
12 November 2026
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.
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 "Explain DynamoDB single-table design with an example: store customers, orders and order...": https://scrivio-interview-library.netlify.app/databases/cassandra-and-dynamodb/f5560e02ba/