DVA-C02 Question 84
Single answerYou are designing a DynamoDB table to store user activity logs for a mobile application. Each log entry includes a user ID, timestamp, and activity type. The application's users are globally distributed, and activity logs are frequently queried by user ID and timestamp range. To ensure balanced partition access and avoid hot partitions, how should you design the partition key?
- A
Use 'user ID' as the partition key and 'timestamp' as the sort key.
- B
Use a composite key combining 'user ID' and a hashed value of 'timestamp' as the partition key.
- C
Use 'region' as the partition key and 'user ID' as the sort key.
- D
Use a random UUID as the partition key and store all other attributes as additional columns.
Show answer and explanation
Correct answer: B
Explanation
To ensure balanced partition access in DynamoDB, the partition key must have high cardinality and distribute data evenly across partitions. By using a composite key combining 'user ID' and a hashed value of 'timestamp,' you can achieve even data distribution while retaining the ability to query efficiently by user ID and timestamp range.
- A. Incorrect.
Using only 'user ID' as the partition key could result in hot partitions if certain users generate a high volume of activity logs, as queries would frequently target a small subset of partitions.
- B. Correct.
A composite key combining 'user ID' and a hashed value of 'timestamp' ensures high cardinality by spreading the data across multiple partitions while still allowing efficient queries by user ID and timestamp range.
- C. Incorrect.
Using 'region' as the partition key would not adequately distribute data because many users within the same region could cause uneven partition usage, leading to hot partitions.
- D. Incorrect.
Using a random UUID as the partition key would create high cardinality, but it would make querying by user ID and timestamp range inefficient, as no meaningful relationship exists between the keys and query patterns.