【问题标题】:Email database architecture [closed]电子邮件数据库架构 [关闭]
【发布时间】:2014-09-25 17:14:18
【问题描述】:

我不确定这是否是一个好的电子邮件数据库架构。

我为每个用户使用 redis 收件箱、存档、已发送列表来存储对话 ID(或线程 ID)。每个对话 ID 都指向一个消息 ID 的 redis 列表。每个消息 id 都指向一个 PostgreSQL 消息表。每当发送、归档或删除消息时,我都会在 redis 列表中移动 ID。

只有在搜索消息和从线程中获取消息时,事情才会有点混乱,因为您必须始终检查线程中的消息是否属于您,有时人们会在同一个线程中回复但不同一组用户。

这是一个好方法吗?有更好的想法吗?如何改进这一点?

【问题讨论】:

  • 为什么这个schema的终点是一张PostgreSQL表?
  • 我不能自信地争论为什么。我认为如果我也将消息存储在 Redis 中,它将占用太多内存。我不确定。

标签: database email database-design redis database-schema


【解决方案1】:

嗯,最后你的论点是正确的:)

http://redis.io/topics/faq

Redis 是一个内存中但持久在磁盘上的数据库,所以它 代表了一个不同的权衡,其中非常高的写入和读取速度 是在不能更大的数据集的限制下实现的 比记忆

因此,您不能将所有内容都保存在 Redis 中。此外,您不能方便地进行文本搜索,因此必须将实际消息数据保存在 PostgreSQL 或其他数据库(如 Solr)中。

思考在您的案例中创建 Redis 层的真正用途是什么。如果你保留 Redis,它可以非常有效地处理 ID,你也可以在其中保留主题和电子邮件地址。

最好的办法是开始一些用例:)

  • 显示最新消息(按时间戳ts排序)

    > sadd messages 1 2
    > hmset message:1 ts 1411783175 author_id 1 subject "An e-mail for starting things up"
    > hmset message:2 ts 1411783150 author_id 2 subject "An early e-mail from the startup one"
    > hmset author_id:1 name "Johnny" email "john@somewhere.com"
    > hmset author_id:2 name "Sarah" email "masterchief@sarah.com"
    > sort messages by message:*->ts get message:*->subject
    1) "An early e-mail from the startup one"
    2) "An e-mail for starting things up"
    

在这种情况下,ts是每条消息的纪元时间

  • 在线程中列出用户(例如线程#1

    > sadd thread:1:messages 1 2
    > sort thread:1:messages get message:*->author_id store thread:1:authors
    > sort thread:1:authors get author_id:*->name get author_id:*->email
    1) "Johnny"
    2) "john@somewhere.com"
    3) "Sarah"
    4) "masterchief@sarah.com"
    

这里 thread:1:messages 有消息 ID,我们将作者存储在 thread:1:authors 键中。

根据我使用 Redis 的经验,您必须选择一个好的客户端(对于 PHP,无疑是 PHPRedis [https://github.com/nicolasff/phpredis],因为它是编译为 php 模块的 C 扩展),并且在您的应用程序中也进行大量处理。

希望有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-28
    • 2015-02-11
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 2020-05-24
    • 2023-03-06
    • 2019-03-14
    相关资源
    最近更新 更多