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

[题目讨论] 关于Twitter sharding用timestamp+ID做组合的讨论

全局:

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

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

x
看了关于twitter sharding key的文章,说是用组合key比较好,比如前面31位存epoch seconds,后面17位存auto incrementing sequence

这样的好处是即用了时间排序,最近的twitter可以一起找到,然后后面的sequence可以保证twitter evenly distributed
有一点不明白的是既然这个id用来sharding,如果按mod来取,所有id都会分散,那前面的同一时间的twitter不都分散到各台机器去了,按时间做前缀的好处并没体现出来啊

上一篇:ID讨论
下一篇:亚麻昂赛题 OOD 设计一个canvas
全局:
原文在哪 需要更多的context
回复

使用道具 举报

🔗
 楼主| LionelWang 2019-6-8 00:22:31 | 只看该作者
全局:
Grokking System Design - Design twitter - Data Sharding

What if we can combine sharding by TweedID 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 latest Tweets. For this, we must make each TweetID universally
unique in our system, and each TweetID should contain timestamp too.
We can use epoch time for this. Let’s say our TweetID will have two parts; the first part will be
representing epoch seconds and the second part will be an auto-incrementing sequence. So, to make a
new TweetID, we can take the current epoch time and append an auto-incrementing number to it. We
can figure out shard number from this TweetID and store it there.
What could be the size of our TweetID? Let’s say our epoch time starts today, how many bits we would
need to store the number of seconds for next 50 years?
86400 sec/day * 365 (days a year) * 50 (years) => 1.6B
We would need 31 bits to store this number. Since on average we are expecting 1150 new tweets per
second, we can allocate 17 bits to store auto incremented sequence; this will make our TweetID 48 bits
long. So, every second we can store (2^17 => 130K) new tweets. We can reset our auto incrementing
sequence every second. For fault tolerance and better performance, we can have two database servers to
generate auto-incrementing keys for us, one generating even numbered keys and the other generating
odd numbered keys.
If we assume our current epoch seconds are “1483228800”, our TweetID will look like this:
1483228800 000001
1483228800 000002
1483228800 000003
1483228800 000004
…
If we make our TweetID 64bits (8 bytes) long, we can easily store tweets for next 100 years and also store
them for mili-seconds granularity.
回复

使用道具 举报

🔗
sal12 2019-6-8 12:01:33 | 只看该作者
全局:
因为每一个Shard里面, 都是排好序的, 因为ID是index的, 从每一个shard query的时间快了, 而且不需要index时间了, 这样写进DB的时间快了. 因为这样Shard, 一定是从每一个shard去query然后组合返回给用户
回复

使用道具 举报

🔗
becoo 2019-6-15 13:47:15 | 只看该作者
全局:
我也有同样的疑问啊。。。。。
回复

使用道具 举报

🔗
HanBurger 2019-7-3 06:17:31 | 只看该作者

回帖奖励 +1

全局:
因为在每个shard里已经排过顺序了,query的时候一般都读id最大的(也就是时间最大的),所以估计比较快吧
回复

使用道具 举报

🔗
qiweiren 2019-7-10 03:05:17 | 只看该作者
全局:
对于这个问题,个人的理解跟上面的回复差不多的。但是感觉有一点grokking没提到的是,ID 是不是应该 timestamp + machine ID + sequence ID 比较好?这样的会避免了single point failure

https://instagram-engineering.co ... tagram-1cf5a71e5a5c

或者 flickr这种奇偶发号的:
https://code.flickr.net/2010/02/ ... -keys-on-the-cheap/
回复

使用道具 举报

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

本版积分规则

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