You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Effective DynamoDB table design is radically different from relational modelling. Instead of normalising data into many tables and joining them at query time, you denormalise and design your tables around your application's access patterns. This lesson covers key design, secondary indexes, and single-table design — the techniques that unlock DynamoDB's full potential.
Before creating a table, list every query your application needs to perform. For example, an e-commerce application might need:
The partition key determines how DynamoDB distributes data across physical partitions. A good partition key has high cardinality — many distinct values — so that no single partition becomes a hot spot.
| Good Partition Keys | Bad Partition Keys |
|---|---|
| userId, orderId, deviceId | status ("active" / "inactive") |
| date + random suffix | country (too few values) |
| tenantId (multi-tenant SaaS) | boolean flags |
The sort key orders items within a partition and supports rich comparison operators:
= equals
< less than
> greater than
<= less than or equal to
>= greater than or equal to
BETWEEN ... AND ...
begins_with(...)
Sometimes your primary key does not support all of your access patterns. DynamoDB offers two types of secondary indexes:
Your Orders table uses customerId as the partition key and orderDate as the sort key. You need to query orders by status. Create a GSI:
GSI: StatusDateIndex
Partition Key: status
Sort Key: orderDate
Projected Attributes: ALL
Now you can query all "shipped" orders sorted by date without scanning the entire table.
Your Orders table uses customerId (PK) and orderDate (SK). You also want to sort orders by totalAmount:
LSI: AmountIndex
Partition Key: customerId (same as table)
Sort Key: totalAmount
Projected Attributes: KEYS_ONLY
| Feature | GSI | LSI |
|---|---|---|
| Partition key | Different from table | Same as table |
| Sort key | Optional | Required (different from table) |
| When created | Any time | Table creation only |
| Read consistency | Eventually consistent | Eventually or strongly consistent |
| Throughput | Separate | Shared with table |
| Maximum per table | 20 | 5 |
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.