Diagnosing DynamoDB Data Modelling Problems
This lesson explains how to spot the modelling problem behind a slow or expensive DynamoDB table, since DynamoDB delivers consistent performance only for access patterns its keys were designed for.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how keys are designed from access patterns.
- Distinguish global from local secondary indexes.
- Describe composite sort keys, sparse indexes and other techniques.
Design from access patterns
Relational design starts with entities and normalises them; DynamoDB design starts with the queries. List every access pattern, then choose partition and sort keys so each one is a Query or GetItem, never a Scan. Related entities that are read together often share a table and partition key (single-table design), with the sort key distinguishing them: CUST#42 / PROFILE, CUST#42 / ORDER#2026-03-01#981.
Secondary indexes
DynamoDB has two kinds of secondary index, which differ in flexibility. A global secondary index (GSI) can use any partition and sort key and can be created at any time, so it serves access patterns discovered later. It supports only eventually consistent reads, and it has its own capacity. If writes to the GSI are throttled, writes to the table are throttled too, so a GSI must be given enough capacity. It has no size limit.
A local secondary index (LSI) keeps the table's partition key with a different sort key and can be created only with the table. It supports strongly consistent reads and shares the table's capacity, but all items with the same partition key value, an item collection, are limited to 10 GB. The following table summarises the differences.
A sparse index includes only items that have the index key attribute, so an index on pendingSince, set only while an order is pending, finds pending orders cheaply. Project only the attributes a pattern needs to keep index reads and writes small.
Modeling techniques
Several techniques let one table serve many access patterns. Composite sort keys, such as COUNTRY#CITY#STORE, support queries at several levels with begins_with: all stores in a country, or all stores in a city. Index overloading stores different entity types in one GSI using generic attribute names, so one index serves several patterns. Adjacency lists model many-to-many relationships, such as students and courses, as items keyed by both ends.
Item size matters, because every read and write consumes capacity in proportion to it: large attributes, such as documents or images, are stored in S3 with a pointer in the item. Time to live deletes expired items at no cost, which suits sessions and temporary records, and DynamoDB Streams lets other components react to changes, for example to maintain aggregates.
An order table is keyed by orderId. A new requirement is to list a customer's orders from the last 30 days, newest first. The current implementation scans the table and filters, which has become slow and expensive as the table has grown.
A scan reads every item, so its cost grows with the table. Add a GSI with partition key customerId and sort key orderDate, projecting only the attributes the listing shows. The listing becomes a Query on the customer with a date condition and ScanIndexForward=false, whose cost depends only on the orders returned.
Why does single-table design store several entity types in one table?
Show answer
Related items share a partition key and can be read together with one Query, which serves the access patterns without joins.
Exam Tips
Remember for the exam
- Model DynamoDB from access patterns; every frequent pattern should be a Query or GetItem.
- GSIs can be added any time but are eventually consistent; LSIs need table creation and allow strong reads.
- Sparse indexes and narrow projections make index reads cheap.
- Replace scans with a GSI keyed for the access pattern.
- Store large objects in S3 and keep a pointer in the item.