【问题标题】:instagram timeline data model in cassandracassandra中的instagram时间线数据模型
【发布时间】:2015-03-22 22:13:33
【问题描述】:

我想要像 instagram 这样的设计时间线(主页),但大多数采样像 "twissandra-j" 使用波纹管模式:

     -- Users user is following
CREATE TABLE following (
    username text,
    followed text,
    PRIMARY KEY(username, followed)
);

-- Users who follow user
CREATE TABLE followers (
    username  text,
    following text,
    PRIMARY KEY(username, following)
);

-- Materialized view of tweets created by user
CREATE TABLE userline (
    tweetid  timeuuid,
    username text,
    body     text,
    PRIMARY KEY(username, tweetid)
);

-- Materialized view of tweets created by user, and users she follows
CREATE TABLE timeline (
    username  text,
    tweetid   timeuuid,
    posted_by text,
    body      text,
    PRIMARY KEY(username, tweetid)
);

在这个设计中,每插入一个新帖子,每个关注者都会在时间轴上插入一条新记录。如果一个用户有 10k 关注者和 1000 个用户使用应用程序,程序失败,有没有更好的方法?

// Insert the tweet into follower timelines
    for (String follower : getFollowers(username)) {
        execute("INSERT INTO timeline (username, tweetid, posted_by, body) VALUES ('%s', %s, '%s', '%s')",
                follower,
                id.toString(),
                username,
                body);

【问题讨论】:

    标签: cassandra schema data-modeling nosql


    【解决方案1】:

    我想,这两个解决方案/建议之一可能会有所帮助:

    1)- 第一个建议,例如以 1000 个插入语句的批处理模式插入 TIMELINE。

    execute("
      BEGIN BATCH
        INSERT INTO timeline (username, tweetid, posted_by, body) VALUES ('%s', %s, '%s', '%s')", follower, id.toString(), username, body);
        INSERT INTO timeline (username, tweetid, posted_by, body) VALUES ('%s', %s, '%s', '%s')", follower, id.toString(), username, body);
        INSERT INTO timeline (username, tweetid, posted_by, body) VALUES ('%s', %s, '%s', '%s')", follower, id.toString(), username, body);
        ...
        // n statements
    APPLY BATCH");
    
    • 批处理多个语句可以节省客户端/服务器和服务器协调器/副本之间的网络交换。

    • 还有一点,默认情况下批处理是原子的(在 Cassandra 1.2 及更高版本中)。在 Cassandra 批处理操作的上下文中,原子意味着如果任何批处理成功,则全部成功,否则不成功。

    2)-第二个建议,实现异步插入TIMELINE(前端带有成功回调函数):

    当然,也许你可以将两者结合起来。

    【讨论】:

    • 与 Eric Evans(Debian 开发人员、Apache Cassandra 提交者和 The Rackspace Cloud 的系统架构师)就 Twissandra 中的数据建模问题进行交流 (cmets):fr.slideshare.net/jericevans/…
    猜你喜欢
    • 2013-08-02
    • 1970-01-01
    • 2013-04-17
    • 2013-07-13
    • 2013-11-18
    • 2017-04-12
    • 2015-12-30
    • 1970-01-01
    • 2018-06-24
    相关资源
    最近更新 更多