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

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

问题描述

我需要将 DynamoDB 用于偶尔在活动当天使用的聊天应用程序.该应用将在特定的一天读取/写入数千条消息,之后几乎没有.

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.

这是我的想法:

Table: Messages
HashKey: Event day name
SortKey: Message timestamp

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

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.

我可以试试这个方法

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.

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

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

推荐答案

我的第一个建议是转移到一个合适的数据库(如 elasticsearch)来回答这种用法(基于时间的搜索)并为每天创建一个新索引

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

但如果你仍然想使用 dynamodb,一个好的 hack 可以是:

but if still you want to use dynamodb, a good hack can be:

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

假设您将拥有 3 个分区.你的钥匙可以是这样的:

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

所以在你的情况下,要获得最后 20 条评论:您应该从每个分区中获得 20 条评论(hash = event_day_name_part_1, event_day_name_part_2 ..),然后进入最近的顶部.

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天全站免登陆