DVA-C02 Question 81
Single answerYou are designing a DynamoDB table to store user activity logs for a web application. Each log entry includes a userId, activityType, and timestamp. The table will experience high write throughput, and queries will often retrieve logs for a specific user in a specific time range. How can you design the partition key to ensure balanced partition access and avoid hot partitions?
- A
Use
userIdas the partition key andtimestampas the sort key. - B
Use a composite key by concatenating
userIdwith a hashed value oftimestampas the partition key, and usetimestampas the sort key. - C
Use
activityTypeas the partition key andtimestampas the sort key. - D
Use a random UUID as the partition key and
timestampas the sort key.
Show answer and explanation
Correct answer: B
Explanation
To achieve balanced partition access, the partition key should be designed to distribute data evenly across all partitions, avoiding hot partitions. By concatenating userId with a hashed value of timestamp, you ensure that data for each user is distributed across multiple partitions, balancing the workload. Using timestamp as the sort key enables efficient range queries for specific time intervals.
- A. Incorrect.
Using
userIdas the partition key andtimestampas the sort key can lead to hot partitions if certain users generate significantly more activity than others. This design won't distribute traffic evenly. - B. Correct.
Using a composite key by concatenating
userIdwith a hashed value oftimestampas the partition key ensures better distribution of write and read requests across partitions, minimizing the risk of hot partitions. Thetimestampas the sort key allows efficient querying within specific time ranges. - C. Incorrect.
Using
activityTypeas the partition key andtimestampas the sort key is not appropriate for this use case because queries often focus on specific users, and this design would not align well with the access patterns. - D. Incorrect.
Using a random UUID as the partition key would distribute traffic but makes it impossible to query logs for a specific user efficiently, as the partition key would have no relationship to
userId.