Single-Table Design and Data Modeling
Why It Feels Backwards
| Relational design order | DynamoDB design order |
|---|---|
| 1. Model entities and their relationships (3NF normalization) | 1. Enumerate every access pattern the application will ever need (every distinct query shape, with expected frequency) |
| 2. Write queries against the normalized schema as needs arise | 2. Design partition key + sort key (and a small number of GSIs) so each access pattern maps to exactly one Query |
| 3. Let the query planner handle joins, and add indexes reactively as slow queries are found | 3. Denormalize aggressively — duplicate data across items, embed one entity's data inside another's item — because there is no join to fall back on later |
The relational approach defers the hard thinking to query time, because the query planner is powerful enough to make that deferral safe. DynamoDB has deliberately declined to build that query planner (see What Is DynamoDB and Partitioning and Request Routing) — so the hard thinking about how data will be accessed must happen at schema-design time, or it becomes expensive/impossible to retrofit. This is the direct, unavoidable consequence of the query-model restriction that makes DynamoDB's latency guarantee possible in the first place — you're not being asked to model this way arbitrarily; it's the price of the guarantee from the Central Question.
The Overloaded Index Pattern
The technique that makes "one table, many entity types" actually work: reuse the same generically-named GSI (conventionally GSI1PK/GSI1SK) to serve structurally different relationships for different entity types, by putting different kinds of values into that generic attribute depending on the item type.
Table: AppData PK SK GSI1PK GSI1SK
─────────────────────────────────────────────────────────────────────────────────
Customer record CUSTOMER#123 METADATA ORG#55 CUSTOMER#123
Order (belongs to cust) CUSTOMER#123 ORDER#001 ORDER#001 2026-01-15
Order (belongs to cust) CUSTOMER#123 ORDER#002 ORDER#002 2026-03-02
Org record ORG#55 METADATA — —
Access patterns this single table + one GSI now satisfies:
1. Get customer + all their orders:
Query(PK = "CUSTOMER#123") — one call, base table
2. Get all customers belonging to an org:
Query(GSI1PK = "ORG#55") — one call, GSI1
3. Get a specific order by ID directly:
Query(GSI1PK = "ORDER#001") — one call, GSI1
4. Get a customer's orders in date order:
Query(PK = "CUSTOMER#123", SK begins_with "ORDER#")
This is the adjacency-list pattern: related entities (a customer and its orders) share a partition key, so fetching a customer "with" its orders is a single Query returning an item collection — no join, because the related items were placed adjacent to each other on disk (within the same partition, sorted by sort key) at write time, not assembled at read time.
Sparse Indexes
A GSI only contains items that actually have the attribute(s) the GSI is keyed on — items missing that attribute simply don't appear in the index at all. This is used deliberately to model cheap filtered views: give only PENDING orders a GSI2PK = "PENDING" attribute (and omit it, or remove it, once an order ships), and Query(GSI2PK = "PENDING") returns exactly the pending orders — without scanning the whole table and filtering client-side, and without paying to index the (usually much larger) set of non-pending orders at all.
A Second Worked Example: Many-to-Many via Adjacency List
The adjacency-list pattern shown above handles one-to-many (a customer "has many" orders). Many-to-many relationships — say, students enrolled in courses, where a student has many courses and a course has many students — use the identical mechanic in both directions at once, by making both entities' identifiers appear as both PK and GSI1PK on the same "edge" item:
Table: SchoolData PK SK GSI1PK GSI1SK
──────────────────────────────────────────────────────────────────────────────────────────
Student record STUDENT#901 METADATA — —
Course record COURSE#CS101 METADATA — —
Enrollment (the "edge") STUDENT#901 COURSE#CS101 COURSE#CS101 STUDENT#901
Enrollment (the "edge") STUDENT#901 COURSE#MATH200 COURSE#MATH200 STUDENT#901
Access patterns:
1. All courses a student is enrolled in:
Query(PK = "STUDENT#901", SK begins_with "COURSE#") — base table
2. All students enrolled in a course:
Query(GSI1PK = "COURSE#CS101") — GSI1
3. Is student 901 enrolled in CS101 specifically?
GetItem(PK = "STUDENT#901", SK = "COURSE#CS101") — base table, O(1)
One "enrollment" item serves as the single edge record for the relationship, queryable from either direction — the base table answers "given a student, which courses" and the GSI answers "given a course, which students," from the exact same underlying data, without a join table needing to be queried twice and intersected the way a relational many-to-many junction table would require.
Hierarchical Data via Composite Sort Keys
A related technique models multi-level hierarchies (e.g., Country → State → City) by concatenating levels into a single sort key with a consistent delimiter, letting begins_with serve as a prefix filter at any level of the hierarchy:
PK = "LOCATIONS"
SK = "USA#CA#SanFrancisco"
SK = "USA#CA#LosAngeles"
SK = "USA#NY#NewYork"
Query(PK="LOCATIONS", SK begins_with "USA#CA#") → every city in California
Query(PK="LOCATIONS", SK begins_with "USA#") → every city in the USA
This is the same begins_with mechanic used for a customer's orders earlier in this part, applied to a different domain — the general principle is that the sort key is the only place DynamoDB gives you range-query flexibility, so hierarchical or time-ordered access patterns should be designed to land in the sort key deliberately, not the partition key.
The Actual Payoff
- One round trip instead of N. A relational "get customer + their 40 most recent orders" is a join (or two round trips); the adjacency-list version above is one
Querywith a bounded, predictable cost. - One item collection can be updated together transactionally — because related entities share a partition,
TransactWriteItemsacross them stays within the single-partition/single-region ACID boundary of the Storage Engine and Replication part and The Consistency Model, rather than needing cross-table coordination. - Simpler capacity planning and fewer moving parts operationally — one table (or a small number) instead of a proliferation of tables each needing their own capacity mode, backup policy, and Streams configuration.
The Cost — and When Not To Do This
| Cost | Why it happens |
|---|---|
| Access patterns discovered late are expensive | Adding a genuinely new access pattern after launch often means adding a new GSI, which triggers an asynchronous backfill (a table-wide write burst consuming real WCU) and only helps future queries — it cannot retroactively make an old design efficient for a pattern nobody designed for. Contrast Postgres, where CREATE INDEX (even CONCURRENTLY) is a comparatively low-ceremony, well-understood operation for the same situation |
| Ad-hoc/analytical queries are simply not available | "How many orders over $500 in the last quarter, broken down by region" has no efficient single-table answer — this class of query needs to be served by streaming changes out (see DynamoDB Streams) into Redshift, Athena-over-S3, or OpenSearch, i.e., a genuinely separate analytical system, not the operational table |
| Team ramp-up cost | Every item's PK/SK are generic, overloaded strings ("ORG#55", "CUSTOMER#123") whose meaning depends on the item type stored alongside them — the self-documenting nature of a normalized relational schema (a table literally named orders with a column literally named customer_id) is traded away for query efficiency. New engineers cannot read the schema and understand the domain; they need the access-pattern design doc |
| The GSI consistency gap resurfaces here concretely | Access pattern #1 above (base-table Query by customer) can be read strongly consistent. Access pattern #2/#3 (GSI-based) cannot (see The Consistency Model) — so a single-table design that spreads access patterns across the base table and several GSIs is, by construction, presenting different consistency guarantees for different queries against data that was written in the same write. A caller who doesn't know which access pattern went through a GSI can be surprised by staleness that "the same data, queried differently" doesn't have in a normalized relational schema (where the underlying storage — and its consistency properties — is uniform regardless of which index served the query) |
| Hot-partition risk concentrates by design | An adjacency-list partition key groups many related items under one partition key (e.g., a customer with an unusually large number of orders, or — worse — a deliberately coarse grouping key like ORG#55 shared by every customer in a large org). This is precisely the shape of key that Adaptive Capacity and Hot Partitions's 3,000 RCU / 1,000 WCU ceiling punishes; single-table design's central technique (grouping related data under one key) is in direct tension with Adaptive Capacity and Hot Partitions's core lesson (don't concentrate traffic under one key), and reconciling the two — bounding how much can legitimately share a partition key — is a real design judgment call, not something the pattern handles automatically |