解剖 Twitter【4】抗洪需要隔离

2010-03-09 15:25

如果说如何巧用 Cache 是 Twitter 的一大看点,那么另一大看点是它的消息队列 (Message Queue)。为什么要使用消息队列?[14] 的解释是 “隔离用户请求与相关操作,以便烫平流量高峰 (Move operations out of the synchronous request cycle, amortize load over time)”。

为了理解这段话的意思,不妨来看一个实例。2009 年 1 月 20 日星期二,美国总统 Barack Obama 就职并发表演说。作为美国历史上第一位黑人总统,Obama 的就职典礼引起强烈反响,导致 Twitter 流量猛增,如 Figure 4 所示。

Figure 4. Twitter burst during the inauguration of Barack Obama, 1/20/2009, Tuesday
Courtesy http://farm3.static.flickr.com/2615/4071879010_19fb519124_o.png

其中洪峰时刻,Twitter 网站每秒钟收到 350 条新短信,这个流量洪峰维持了大约 5 分钟。根据统计,平均每个 Twitter 用户被 120 人 “追”,这就 是说,这 350 条短信,平均每条都要发送 120 次 [16]。这意味着,在这 5 分钟的洪峰时刻,Twitter 网站每秒钟需要发送 350 x 120 = 42,000 条短信。

面对洪峰,如何才能保证网站不崩溃?办法是迅速接纳,但是推迟服务。打个比方,在晚餐高峰时段,餐馆常常客满。对于新来的顾客,餐馆服务员不是拒之门外,而是让这些顾客在休息厅等待。这就是 [14] 所说的 “隔离用户请求与相关操作,以便烫平流量高峰”。

如何实施隔离呢?当一位用户访问 Twitter 网站时,接待他的是 Apache Web Server。Apache 做的事情非常简单,它把用户的请求解析以后,转发给 Mongrel Rails Sever,由 Mongrel 负责实际的处理。而 Apache 腾出手来,迎接下一位用户。这样就避免了在洪峰期间,用户连接不上 Twitter 网站的尴尬局面。

虽然 Apache 的工作简单,但是并不意味着 Apache 可以接待无限多的用户。原因是 Apache 解析完用户请求,并且转发给 Mongrel Server 以后,负责解析这个用户请求的进程 (process),并没有立刻释放,而是进入空循环,等待 Mongrel Server 返回结果。这样,Apache 能够同时接待的用户数量,或者更准确地说,Apache 能够容纳的并发的连接数量 (concurrent connections),实际上受制于 Apache 能够容纳的进程数量。Apache 系统内部的进程机制参见 Figure 5,其中每个 Worker 代表一个进程。

Apache 能够容纳多少个并发连接呢?[22] 的实验结果是 4,000 个,参见 Figure 6。如何才能提高 Apache 的并发用户容量呢?一种思路是不让连接受制于进程。不妨把连接作为一个数据结构,存放到内存中去,释放进程,直到 Mongrel Server 返回结果时,再把这个数据结构重新加载到进程上去。

事实上 Yaws Web Server[24],就是这么做的 [23]。所以,Yaws 能够容纳 80,000 以上的并发连接,这并不奇怪。但是为什么 Twitter 用 Apache,而不用 Yaws 呢?或许是因为 Yaws 是用 Erlang 语言写的,而 Twitter 工程师对这门新语言不熟悉 (But you need in house Erlang experience [17])。

Figure 5. Apache web server system architecture [21]
Courtesy http://farm3.static.flickr.com/2699/4071355801_db6c8cd6c0_o.png

Figure 6. Apache vs. Yaws.
The horizonal axis shows the parallel requests,
the vertical one shows the throughput (KBytes/second).
The red curve is Yaws, running on NFS.
The blue one is Apache, running on NFS,
while the green one is also Apache but on a local file system.
Apache dies at about 4,000 parallel sessions,
while Yaws is still functioning at over 80,000 parallel connections. [22]
Courtesy http://farm3.static.flickr.com/2709/4072077210_3c3a507a8a_o.jpg

Reference,

[14] Improving running component of Twitter.
(http://qconlondon.com/london-2009/file?path=/qcon-london-
2009/slides/EvanWeaver_ImprovingRunningComponentsAtTwitter.pdf)
[16] Updating Twitter without service disruptions.
(http://gojko.net/2009/03/16/qcon-london-2009-upgrading-
twitter-without-service-disruptions/)
[17] Fixing Twitter. (http://assets.en.oreilly.com/1/event/29/Fixing_Twitter_
Improving_the_Performance_and_Scalability_of_the_World_s_Most_Popular_
Micro-blogging_Site_Presentation%20Presentation.pdf)
[21] Apache system architecture.
(http://www.fmc-modeling.org/download/publications/
groene_et_al_2002-architecture_recovery_of_apache.pdf)
[22] Apache vs Yaws. (http://www.sics.se/~joe/apachevsyaws.html)
[23] 质疑 Apache 和 Yaws 的性能比较. (http://www.javaeye.com/topic/107476)
[24] Yaws Web Server. (http://yaws.hyber.org/)
[25] Erlang Programming Language. (http://www.erlang.org/)

登录,参与讨论前请先登录

评论在审核通过后将对所有人可见

正在加载中

移动互联网的围观者、起哄者、以及肇事者。

本篇来自栏目

解锁订阅模式,获得更多专属优质内容