查看: 1567| 回复: 5
跳转到指定楼层
上一主题 下一主题
收起左侧

[题目讨论] 请教一个相关data sharding的问题

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
昨天在读到educative.io里面的设计题:designing twitter
其中的7. Data Sharding环节,里面提到好几种方案:
  • Sharding based on TweetID
  • Sharding based on Tweet creation time
  • What if we can combine sharding by TweetID and Tweet creation time? If we don’t store tweet creation time separately and use TweetID to reflect that, we can get benefits of both the approaches. This way it will be quite quick to find the latest Tweets. For this, we must make each TweetID universally unique in our system and each TweetID should contain a timestamp too.
我不明白的地方是,为什么第三种方案可以同时获得前两种方案的好处?我的理解是:

方案1,好处是数据分布均匀
方案2,好处是最新帖子会集中在少数几个nodes上
方案3,如果同时用tweetID和createTime,也不可能二者兼得啊,如果数据分布均匀了,那么肯定就会分散到多个nodes上,如果想要集中在少数几个nodes,那数据必然分布就不均匀了。我怎么想都觉得二者矛盾。
请哪位大神指点一二,多谢!
顺便求一下米!

评分

参与人数 1大米 +1 收起 理由
wushifish + 1 给你点个赞!

查看全部评分


上一篇:OS操作系统好书推荐!!!面试必备!!
下一篇:非常好的kafka核心原理deep dive
全局:
twitter snowflake算法了解一下

twitterID是一个UUID, 其中的前41bit是时间戳,这就是为什么twitterId can be sorted by creation time.
回复

使用道具 举报

🔗
kuangbyte 2020-8-2 12:29:06 | 只看该作者
全局:
我没有看过educative.io提供的设计方案,以下的回答都是根据我对三种列出的方案,在缺少上下文的想法
> Sharding based on TweetID
> Sharding based on Tweet creation time
如果TweetId和Tweet creation time是以自增的方式增加的话,那么数据分布不是均匀的,在数据的增加过程中,单表密集的写操作会带来性能问题,好处是不涉及数据迁移,而且单表的数据量是可控的。
如果TweetId是随机生成的UUID,那么你的结论就是正确的,但是UUID的字段冗长,如果db是用的clustered index,那么查询性能也会下降,而且作为shard key,语义上没法解读。
如果TweetId和Tweet creation time都是做了mod或者hash+mod操作的话,那么分布是均匀的,不过这也有一部分取决于tweetId的生成的方式和hash算法,即便分表或者分库是按倍数增长的,还是会涉及数据迁移。

> What if we can combine sharding by TweetID and Tweet creation time? If we don’t store tweet creation time separately and use TweetID to reflect that, we can get benefits of both the approaches. This way it will be quite quick to find the latest Tweets. For this, we must make each TweetID universally unique in our system and each TweetID should contain a timestamp too.
同样的,我个人认为如果这个解决方案用mod的话是得不出这个结论的,况且它也没有提到取模。如果是增量式的话,这个就能说通。假设primary key是第一部分是时间戳,第二部分
是一个自增的ID,tweet的表的primary key = tweetId = (creationTime + "_" + id) 构成的话,并且我们把这个primary key用作为shard key,那么以增量方式存储,单表是有序的。如果一个请求是获取最新的十条tweet的数据,那么数据查询就会非常快速了。

顺带说一下, google cloud doc把这两种方式都定义成了anti-pattern (https://cloud.google.com/spanner ... _timestamp_ordering)。

2楼的答案提供了一种生成分布式ID的思路,取模后用做shard key也是可以的。
回复

使用道具 举报

🔗
kuangbyte 2020-8-2 12:29:45 | 只看该作者
全局:
我没有看过educative.io提供的设计方案,以下的回答都是根据我对三种列出的方案,在缺少上下文的想法
> Sharding based on TweetID
> Sharding based on Tweet creation time
如果TweetId和Tweet creation time是以自增的方式增加的话,那么数据分布不是均匀的,在数据的增加过程中,单表密集的写操作会带来性能问题,好处是不涉及数据迁移,而且单表的数据量是可控的。
如果TweetId是随机生成的UUID,那么你的结论就是正确的,但是UUID的字段冗长,如果db是用的clustered index,那么查询性能也会下降,而且作为shard key,语义上没法解读。
如果TweetId和Tweet creation time都是做了mod或者hash+mod操作的话,那么分布是均匀的,不过这也有一部分取决于tweetId的生成的方式和hash算法,即便分表或者分库是按倍数增长的,还是会涉及数据迁移。

> What if we can combine sharding by TweetID and Tweet creation time? If we don’t store tweet creation time separately and use TweetID to reflect that, we can get benefits of both the approaches. This way it will be quite quick to find the latest Tweets. For this, we must make each TweetID universally unique in our system and each TweetID should contain a timestamp too.
同样的,我个人认为如果这个解决方案用mod的话是得不出这个结论的,况且它也没有提到取模。如果是增量式的话,这个就能说通。假设primary key是第一部分是时间戳,第二部分
是一个自增的ID,tweet的表的primary key = tweetId = (creationTime + "_" + id) 构成的话,并且我们把这个primary key用作为shard key,那么以增量方式存储,单表是有序的。如果一个请求是获取最新的十条tweet的数据,那么数据查询就会非常快速了。

顺带说一下, google cloud doc把这两种方式都定义成了anti-pattern (https://cloud.google.com/spanner ... _timestamp_ordering)。

2楼的答案提供了一种生成分布式ID的思路,取模后用做shard key也是可以的。
回复

使用道具 举报

🔗
kuangbyte 2020-8-2 12:30:12 | 只看该作者
全局:
我没有看过educative.io提供的设计方案,以下的回答都是根据我对三种列出的方案,在缺少上下文的想法
> Sharding based on TweetID
> Sharding based on Tweet creation time
如果TweetId和Tweet creation time是以自增的方式增加的话,那么数据分布不是均匀的,在数据的增加过程中,单表密集的写操作会带来性能问题,好处是不涉及数据迁移,而且单表的数据量是可控的。
如果TweetId是随机生成的UUID,那么你的结论就是正确的,但是UUID的字段冗长,如果db是用的clustered index,那么查询性能也会下降,而且作为shard key,语义上没法解读。
如果TweetId和Tweet creation time都是做了mod或者hash+mod操作的话,那么分布是均匀的,不过这也有一部分取决于tweetId的生成的方式和hash算法,即便分表或者分库是按倍数增长的,还是会涉及数据迁移。

> What if we can combine sharding by TweetID and Tweet creation time? If we don’t store tweet creation time separately and use TweetID to reflect that, we can get benefits of both the approaches. This way it will be quite quick to find the latest Tweets. For this, we must make each TweetID universally unique in our system and each TweetID should contain a timestamp too.
同样的,我个人认为如果这个解决方案用mod的话是得不出这个结论的,况且它也没有提到取模。如果是增量式的话,这个就能说通。假设primary key是第一部分是时间戳,第二部分
是一个自增的ID,tweet的表的primary key = tweetId = (creationTime + "_" + id) 构成的话,并且我们把这个primary key用作为shard key,那么以增量方式存储,单表是有序的。如果一个请求是获取最新的十条tweet的数据,那么数据查询就会非常快速了。

顺带说一下, google cloud doc把这两种方式都定义成了anti-pattern (https://cloud.google.com/spanner ... _timestamp_ordering)。

2楼的答案提供了一种生成分布式ID的思路,取模后用做shard key也是可以的。
回复

使用道具 举报

🔗
kuangbyte 2020-8-2 12:34:37 | 只看该作者
全局:
不知道为什么回复了3次,刷新的时候完全没有显示。谁有权限的话就删了其中2个外加这个留言,着实抱歉。
回复

使用道具 举报

无效楼层,该帖已经被删除
您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表