问题
I have a requirement to use DynamoDB for a chat app that will be used occasionally, on an event day. The app will be reading/writing few thousands of messages on a particular day, and almost none after that.
Here's what I have in mind:
Table: Messages
HashKey: Event day name
SortKey: Message timestamp
I can get recent 20 messages (chat app usually get latest data only, no?), but the all read/write will be directed to one partition only.
I could try this way
Table: EventDayMessage
HashKey: Message timestamp
With this, the read/write will be spread out to the more recent message, but I have no way to get the recent messages due to no sort key.
Is method 2 better in my scenario? What can I use for the sort key? Is there any other better suggestions (other than switching db)?
回答1:
my first recommendation is to move to a proper db (like elasticsearch) that answer this usage (time based search) and create a new index for each day
but if still you want to use dynamodb, a good hack can be:
- if your app write/read thousands of msges per day, so you wont get many partitions (depends also on you data size).
lets assume you are going to have 3 partitions. your keys can be like that:
HashKey: Event day name + 'part_X' (where x is a number between 1-5. it can be a round rubin, or some '%' on a user_id or something like that)
SortKey: Message timestamp
so in your case, to get last 20 comments: you should get 20 comments from each partition (hash = event_day_name_part_1, event_day_name_part_2 ..) and then get to top recent.
来源:https://stackoverflow.com/questions/34837753/how-to-choose-a-partition-key-in-dynamodb-for-a-chat-app