Partition key, sort key & composite keys
Every item is located by its primary key. A simple key is just a partition key (hash key) — items are distributed across partitions by a hash of this value, so it must have high cardinality to spread load. A composite key adds a sort key: items with the same partition key are stored together, sorted, enabling efficient range queries (e.g. all orders for a customer, newest first). Good key design encodes your main access patterns directly into the keys.
- Partition (hash) key spreads items across partitions — use high cardinality
- Sort key groups + orders items within a partition (range queries)
- Encode access patterns into the partition + sort key
Exam tip: partition key = WHICH partition; sort key = ORDER/RANGE within it. "Get all X for a Y, sorted by time" ⇒ partition key Y, sort key time.
Source: AWS docs — DynamoDB Developer Guide
Query vs Scan
Query fetches items by an exact partition key (optionally narrowed by a sort-key condition) and reads only the matching items — efficient and the normal way to read. Scan reads the ENTIRE table and filters afterward — expensive and slow at scale, acceptable only for small tables, exports or admin tasks. If you find yourself needing Scan for a routine access pattern, that's a signal the key/index design doesn't fit the pattern.
- Query = by partition key (+ sort condition), reads only matches — efficient
- Scan = reads the whole table then filters — expensive, avoid at scale
- Needing Scan routinely means the data model is wrong for that pattern
Exam tip: Prefer Query; avoid Scan on large tables. A filter expression on a Scan still READS (and charges for) the whole table before filtering.
Source: AWS docs — DynamoDB Developer Guide
Eventual vs strong consistency
By default reads are eventually consistent — a read right after a write might not see the latest value (usually within a second) — and they cost half as much. A strongly-consistent read always returns the latest committed data but costs more read capacity and isn't available on GSIs. Choose strong consistency only where staleness is unacceptable (e.g. read-after-write on a balance); use the cheaper eventual reads elsewhere.
- Eventual (default): possibly stale for ~a second, cheaper
- Strong: always latest, costs more, NOT available on GSIs
- Use strong only where read-after-write correctness is required
Exam tip: Strong consistency is opt-in, costs more RCUs, and is unavailable on GSIs. Eventual is the cheaper default.
Source: AWS docs — DynamoDB Developer Guide
Comparison: DynamoDB vs RDS
DynamoDB vs RDS is a foundational data-store decision. DynamoDB: NoSQL, key-value/document, single-digit-ms at massive scale, access-pattern-first, no joins/ad-hoc queries, horizontal scaling with no server management. RDS: relational SQL, joins, multi-row transactions, flexible ad-hoc queries, vertical + read-replica scaling with a capacity ceiling. Choose DynamoDB for known, high-scale key-based access patterns; choose RDS/Aurora when you need relational integrity, joins, or unpredictable queries.
- DynamoDB: NoSQL, access-pattern-first, huge scale, no joins/ad-hoc queries
- RDS: relational, joins/transactions/ad-hoc queries, capacity ceiling
- Pick by access pattern: known key-based/high-scale ⇒ DynamoDB; relational/flexible ⇒ RDS
Source: AWS docs — DynamoDB Developer Guide