Partition & Sort Keys Interview Questions
Partition & Sort Keys interview questions for DynamoDB — fundamentals through advanced scenarios.
- 20Questions with answers
- 3Difficulty levels
Questions (20)
Browse beginner, intermediate, and advanced questions with answers — hide them when you want to self-test.
What does the partition key physically control in DynamoDB?
It hashes to a storage partition that owns that key’s items and their throughput budget. Uneven keys pile traffic onto few partitions and throttle while the rest of the table looks idle. High cardinality and even access are therefore part of the data model, not only capacity settings.
What Query conditions can you apply to a sort key?
Equality, <, <=, >, >=, between, and begins_with—always together with a full partition key. You cannot Query “all sort keys matching a prefix” across the table without a GSI. FilterExpression after the Query is not a substitute for a sort-key condition; it still reads matching PK items.
Why can two items not share the same partition key and sort key?
The composite primary key is the item’s identity. A second Put with the same PK+SK overwrites (or fails a condition), it does not append a row. If you need history, encode uniqueness in the sort key (timestamp, UUID) instead of reusing the same pair.
How does begins_with on a sort key support hierarchical collections?
A convention such as ADDR#home or ORDER#2026-09-18#<id> groups related children so one Query retrieves a type or a date prefix. Depth is limited by the string you store, not by SQL-style foreign keys. Poorly padded dates or mixed casings break the intended range.
Why must every Query specify a partition key?
DynamoDB locates items by hashing that key; without it the request would be a Scan. APIs that omit PK are a modeling failure, not a missing WHERE clause. If callers only know an email or SKU, that attribute belongs on a GSI as the index partition key.
When is a simple primary key (partition key only) the right choice?
Use it for true 1:1 lookup items—session tokens, feature flags, a user profile with no child collection on that table. Adding an unused sort key complicates every Get. As soon as you need 1:N under the same identity, switch to a composite key rather than stuffing arrays into one item.
Why is a low-cardinality or time-bucket partition key usually a problem?
Keys such as status=OPEN or date=today concentrate today’s traffic on one partition. Adaptive capacity helps brief spikes, not a permanent celebrity key. Combine a high-cardinality identifier, or shard the time bucket, if that access pattern is required.
How do composite sort keys encode more than one access path?
Concatenate typed segments (ENTITY#id#TS) so begins_with can peel the hierarchy. Document the exact separator and zero-padding so range queries stay ordered. Overloaded sort keys are powerful; undocumented ones become unqueryable archaeology.
How do you detect a hot partition key in production?
CloudWatch throttling with healthy table-level consumed capacity, plus Contributor Insights on most-accessed keys, usually means one PK value. Client retries then amplify the hotspot. Fix the key or cache that item; raising provisioned WCUs on the whole table rarely fixes skew.
What can adaptive capacity fix, and what can it not?
It can shift extra throughput toward a partition that is briefly hotter than its neighbors. It cannot invent more than the table’s limits or repair a key that is hot all day, every day. Permanent skew still needs sharding, caching, or a different PK.
How do you Query the newest N items under a partition?
Use a sort key that orders by time (ISO-8601 or zero-padded epoch) and Query ScanIndexForward=false with a Limit. Do not Scan the table and sort in the app. If “newest” must be global across all users, that is a different key or a GSI, not a local collection trick.
What is an item collection, and why do LSIs and some transactions care?
All items that share a partition key form a collection. LSIs are scoped to that collection, and some transactional patterns assume colocated items. A collection that grows without bound also grows Query cost for anything that reads a wide SK range.
Why does UTF-8 sort order and numeric padding matter on sort keys?
String comparison is lexicographic: "9" sorts after "10" unless you pad (0009, 0010) or use a fixed-width epoch. Mixed types coerced to strings will not match human numeric order. Interviews often catch this on “list IDs 1..11” style keys.
When should you shard a partition key versus changing the access pattern?
Shard when the entity must exist but is too hot (popular product, viral user). Change the pattern when you were hashing on the wrong attribute. Sharding adds fan-out reads; do not shard a key you could have Query’d on a GSI with even cardinality instead.
How would you key a time-series workload so a single day does not become one hot partition?
Include a high-cardinality dimension (deviceId, customerId) as PK and time as SK, or shard the time bucket (day#shard). A PK of YYYY-MM-DD alone collapses midnight traffic onto one partition. Downsampling to rollup items is the usual way to keep Query windows bounded.
How can one table support “get profile by userId” and “list that user’s orders in a date range”?
PK=userId with SK=PROFILE for the profile item and SK=ORDER#<timestamp>#<orderId> for orders. GetItem or Query equal on PROFILE; Query begins_with ORDER# plus a timestamp range for history. A GSI is only needed if you must also look up by orderId alone.
How do you migrate partition key values with effectively zero downtime?
Dual-write old and new keys, backfill, switch readers with a flag, then stop old writes. In-place rewrite of PK is not supported; you copy items. Keep idempotent keys so retries do not create twins during the overlap window.
What is the risk of a monotonically increasing sort key on a popular partition?
New writes always hit the tail of the same partition, which can create a hot key even when cardinality looks high. Random suffixes, swapping time into the PK, or caching the tail mitigate it. This shows up in append-only feeds and high-rate event logs.
How would you encode tenant isolation into keys while still allowing support tooling to look up a user?
PK includes tenantId so IAM LeadingKeys and Query paths cannot wander. A GSI on userId (or email) lets support find the tenant+user without scanning. Never use a global PK=userId if tenants can collide or if isolation must be cryptographic in IAM.
How do you validate key design against peak QPS before launch?
Generate realistic key cardinality in a load test, not a tight loop on one PK. Measure throttles per key, p99, and consumed capacity. A test that reuses USER#1 proves nothing about partition spread and often ships a hotspot into production.
Practice with AI mock interviews
Run DynamoDB mock interviews with AI follow-ups, instant feedback, and analytics on AiLx.
Free to start · No credit card required