DVA-C02 Question 83
Single answerYou are designing a DynamoDB table to store user activity logs for a mobile application. Each activity log must be stored with the user ID, timestamp, and activity type. The table is expected to handle high traffic with millions of users. To ensure balanced partition access and avoid hot partitions, what is the BEST strategy for defining the partition key?
- A
Use the user ID as the partition key.
- B
Use the timestamp as the partition key.
- C
Use a composite key combining user ID and a random suffix as the partition key.
- D
Use a hash of the user ID as the partition key.
Show answer and explanation
Correct answer: C
Explanation
To achieve balanced partition access in DynamoDB, the partition key must have high cardinality to distribute data evenly across partitions. By combining the user ID with a random suffix, you create a more distributed set of partition keys while still enabling querying by user ID when required. This avoids hot partitions caused by traffic imbalances associated with specific users or timestamps.
- A. Incorrect.
Using the user ID as the partition key can lead to hot partitions because some users may generate significantly more activity logs than others, causing an imbalance in partition access.
- B. Incorrect.
Using the timestamp as the partition key can create a hot partition as all writes for a given moment in time will go to the same partition, especially during peak traffic periods.
- C. Correct.
Using a composite key combining user ID and a random suffix ensures high-cardinality partition keys, distributing traffic more evenly across partitions and avoiding hot partitions.
- D. Incorrect.
Using a hash of the user ID may help distribute traffic but does not account for differences in traffic patterns per user or allow for querying based on user ID directly.