How to choose a partition key in DynamoDB for a chat app

喜夏-厌秋 提交于 2019-12-09 03:44:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!