如何在DynamoDB中为聊天应用程序选择分区键 [英] How to choose a partition key in DynamoDB for a chat app

查看:120
本文介绍了如何在DynamoDB中为聊天应用程序选择分区键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,将DynamoDB用于聊天应用程序,该应用程序在活动日偶尔会使用。该应用程序将在特定的一天读取/写入数千条消息,此后几乎没有消息。



这里是我的主意:

 表:消息
HashKey:活动日期名称
SortKey:消息时间戳记

我可以获取最近的20条消息(聊天应用程序通常仅获取最新数据,不是吗?),但是所有读/写操作只会定向到一个分区。



我可以这样尝试

 表格:EventDayMessage 
HashKey:消息时间戳记

通过此操作,读/写操作将扩展到最近的消息中,但是由于没有排序键,我无法获取最新消息。



在我的场景中,方法2更好吗?我可以为排序键使用什么?除了切换数据库外,还有其他更好的建议吗?

解决方案

我的第一个建议是移至适当的数据库(例如elasticsearch),可以回答这种用法(基于时间的搜索)并为每天创建一个新索引



,但是如果您仍然想使用dynamodb,可以使用以下方法: / p>


  • 如果您的应用每天写入/读取数千个msges,那么您将不会得到很多分区(还取决于您的数据大小)。



让我们假设您将有3个分区。
您的密钥可以像这样:

  HashKey:活动日期名称+'part_X'(其中x是数字)介于1-5之间。它可以是圆形的rubin,也可以是user_id上的%或类似符号)
SortKey:消息时间戳

因此,要获得最后20条评论:
,您应该从每个分区中获得20条评论(哈希= event_day_name_part_1,event_day_name_part_2 ..),然后排名靠前


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)?

解决方案

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.

这篇关于如何在DynamoDB中为聊天应用程序选择分区键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆