12
返回列表 发新帖
楼主: jieforest

MongoDB规模化的设计模式

[复制链接]
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
11#
 楼主| 发表于 2012-9-30 01:29 | 只看该作者
We've actually improvised performance in two ways using this approach:

1. Pre-allocation means that our documents never grow, so they never get moved

2. By pre-allocating throughout the day, we don't have a "midnight problem" where our upserts all end up inserting a new document and increasing load on the server.

We do, however, have a curious downward trend in performance throughout the day (though much less drastic than before). Where did that come from?

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
12#
 楼主| 发表于 2012-10-1 13:06 | 只看该作者
MongoDB's storage format

To figure out the downward performance through the day, we need to take a brief detour into the actual format that MongoDB uses to store data on disk (and memory), BSON. Normally, we don't need to worry about it, since the pymongo driver converts everything so nicely into native Python types, but in this case BSON presents us a performance problem.

Although MongoDB documents, such as our minute embedded document, are represented in Python as a dict (which is a constant-speed lookup hash table), BSON actually stores documents as an association list. So rather than having a nice hash table for minute, we actually have something that looks more like the following:
  1. 1.minute = [
  2. 2.[ "0000", 3612 ],
  3. 3.[ "0001", 3241 ],
  4. 4.# ...
  5. 5.[ "1439", 2819 ] ]
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
13#
 楼主| 发表于 2012-10-1 13:06 | 只看该作者
Now to actually update a particular minute, the MongoDB server performs the something like the following operations (psuedocode, with lots of special cases ignored):
  1. 1.inc_value(minute, "1439", 1)
  2. 2.
  3. 3.def inc_value(document, key, value)
  4. 4.for entry in document:
  5. 5.if entry[0] == key:
  6. 6.entry[1] += value
  7. 7.break
复制代码
The performance of this algorithm, far from our nice O(1) hash table, is actually O(N) in the number of entries in the document. In the case of the minute document, MongoDB has to actually perform 1439 comparisons before it finds the appropriate slot to update.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
14#
 楼主| 发表于 2012-10-1 13:07 | 只看该作者
Fixing the downward trend with hierarchy

To fix the problem, then, we need to reduce the number of comparisons MongoDB needs to do to find the right minute to increment. The way we can do this is by splitting up the minutes into hours. Our daily stats document now looks like the following:
  1. 01.{ _id: "20101010/metric-1",
  2. 02.metadata: {
  3. 03.date: ISODate("2000-10-10T00:00:00Z"),
  4. 04.metric: "metric-1" },
  5. 05.daily: 5468426,
  6. 06.hourly: {
  7. 07."0": 227850,
  8. 08."1": 210231,
  9. 09....
  10. 10."23": 20457 },
  11. 11.minute: {
  12. 12."00": {      
  13. 13."0000": 3612,
  14. 14."0100": 3241,
  15. 15....
  16. 16.}, ...,
  17. 17."23": { ..., "1439": 2819 }
  18. 18.}
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
15#
 楼主| 发表于 2012-10-1 13:07 | 只看该作者
Our record_hit and preallocate routines have to change a bit as well:
  1. 01.def record_hit_hier(coll, dt, measure):
  2. 02.if PREALLOC and random.random() < (1.0/1500.0):
  3. 03.preallocate_hier(coll, dt + timedelta(days=1), measure)
  4. 04.sdate = dt.strftime('%Y%m%d')
  5. 05.metadata = dict(
  6. 06.date=datetime.combine(
  7. 07.dt.date(),
  8. 08.time.min),
  9. 09.measure=measure)
  10. 10.id='%s/%s' % (sdate, measure)
  11. 11.coll.update(
  12. 12.{ '_id': id, 'metadata': metadata },
  13. 13.{ '$inc': {
  14. 14.'daily': 1,
  15. 15.'hourly.%.2d' % dt.hour: 1,
  16. 16.('minute.%.2d.%.2d' % (dt.hour, dt.minute)): 1 } },
  17. 17.upsert=True)
  18. 18.
  19. 19.def preallocate(coll, dt, measure):
  20. 20.'''Once again, simplified for explanatory purposes'''
  21. 21.metadata, id = # compute metadata and ID
  22. 22.coll.update(
  23. 23.{ '_id': id },
  24. 24.{ '$set': { 'metadata': metadata },
  25. 25.'$inc': {
  26. 26.'daily': 0,
  27. 27.'hourly.00': 0,
  28. 28.# ...
  29. 29.'hourly.23': 0,
  30. 30.'minute.00.00': 0,
  31. 31.# ...
  32. 32.'minute.00.59': 0,
  33. 33.'minute.01.00': 0,
  34. 34.# ...
  35. 35.'minute.23.59': 0 } },
  36. 36.upsert=True)
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
16#
 楼主| 发表于 2012-10-1 13:07 | 只看该作者
Once we've added the hierarchy and re-run our experiment, we get the nice, level performance we'd like to see:

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
17#
 楼主| 发表于 2012-10-2 14:32 | 只看该作者
Conclusion

It's always nice to see "tips and tricks" borne out through actual, quantitative results, so this was probably the most enjoyable talk I've ever put together. The things I got out of it were the following:

1. Growing documents is a very bad thing for performance. Avoid it if at all possible.

2. Awareness of the BSON specification and data representation can actually be quite useful when diagnosing performance problems.

3. To get the best performance out of your system, you need to actually run it (or a highly representative stand-in). Actually seeing the results of performance tweaking in graphical form is incredibly helpful in targeting your efforts.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
18#
 楼主| 发表于 2012-10-2 14:32 | 只看该作者
The source code for all these updates is available in my mongodb-sdas Github repo, and I welcome any feedback either there, or here in the comments. In particular, I'd love to hear of any performance problems you've run into and how you got around them. And of course, if you've got a really perplexing problem, I'm always available for consulting by emailing me at Arborian.com.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
19#
 楼主| 发表于 2012-10-2 14:32 | 只看该作者
over.

使用道具 举报

回复
论坛徽章:
5
2011新春纪念徽章
日期:2011-02-18 11:43:32ITPUB十周年纪念徽章
日期:2011-11-01 16:26:292012新春纪念徽章
日期:2012-01-04 11:57:362012新春纪念徽章
日期:2012-02-07 09:59:35秀才
日期:2016-06-23 14:15:06
20#
发表于 2012-10-22 15:51 | 只看该作者
求git的url.一段一段看不容易看懂

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表