More Related Content Similar to 从林书豪到全明星 - 虎扑网技术架构如何化解流量高峰 Similar to 从林书豪到全明星 - 虎扑网技术架构如何化解流量高峰 (20) 从林书豪到全明星 - 虎扑网技术架构如何化解流量高峰13. Shared-nothing 架构
• 什么是 Shared-nothing ( SN )架构?
–一个系统(网站)中的每一个节点(应用服务器)
都是互相独立的,彼此之间没有竞争或冲突。
• SN 在现实生活中的例子
–电驴, P2P
–蚂蚁
14
17. 问题一:如何管理 Session ?
•SN 架构下管理 Session 的三种方案
–通过区分 IP 将用户分配到固定的服务器上
•分布不均匀,易用性差,无法应对复杂的网络环境
•软件实现逻辑复杂,硬件实现价格昂贵
–使用外部数据库存储 Session
•严格意义上并没有实现 SN
–将 Session 加密后存在 Cookie 里
•对 Session 数据的大小有限制
•如何验证 Cookie 有效性?
18
19. 问题三:如何部署 SN 架构下的网站应
用?
• 应用服务器数量多
–如何快速升级应用到新版本?
• 应用复杂度变高
–如何验证代码的正确性?
–发现问题后如何回滚?
• 部署的新版本需要改变数据库结构怎么办?
20
31. 经典的缓存方式
Nginx App Memcache
(PHP-FPM)
1. 接受浏览器请求
2. 透传所有请求去访问 App
3.App 判断 Cache 是否可用,如果不可用则生成 Cache
4. 如果 Cache 可用则获取 Cache
5.Web Server 获取到 App 返回的结果
6.Web Server 将结果返回给用户
33
32. 什么是微缓存?
Nginx App
(PHP-FPM)
Nginx
Cache
1. 接受到大量请求
2a. 透传其中的一个请求去访问 App
3a. 将 App 返回的结果写入 Cache
2b. 直接从 Cache 读取页面缓存
3b. 将 Cache 的结果直接返回给用户
34
33. 微缓存的优势
• 优势
–部署简单
•使用时仅需配置前端反向代理(如 Nginx )
–应用偶和度低
• 不侵占应用 、不修改程序代码
–页面更新速度快
•透传频率可调,最快每秒一次(以 Nginx 为例)
–即使访问量再大,程序运行次数依然是固定的
35
36. 包含页面片段的三种解决方案
• Client Side Include
–实现原理:通过 javascript 、 iframe 载入其他页面
• Edge Side Include
–实现原理:通过 ESI 标签中的 url 地址获取页面嵌
套内容
• Server Side Include
–实现原理:通过 SSI 标签中的文件地址获取页面嵌
套内容
38
37. ESI 和 SSI 的两点最重要的区别
• 载入方式不同:
–SSI 只能加载静态文件和本地 HTTP 地址
–ESI 可以加载远程 HTTP 地址,且可以设置 TTL 时
间
• 各类 Web 服务软件的支持度不同
–SSI 主要被用在各种 Web 服务器上,例如 Nginx 、
Apache
–ESI 主要被用在各种前端缓存服务器上,例如
Varnish 、 Squid
39
39. 页面头部
TTL :永久
视频及播放数
TTL : 5 分钟
相关视频
TTL : 60 分钟
留言
TTL :新内容产生时
今日头条推荐
TTL : 24 小时
41
42. 虎扑网使用的 NoSQL 服务
• Memcache
–30%
• Tokyo Tyrant
–40%
• Redis
–30%
• Mongodb
–开发中的游戏项目正在内部试用
44
43. 虎扑网使用 NoSQL 的一些经验
• 勤用 Redis 的数据结构解决各种问题。
–List 可以当做简单的消息队列来使用
–Sorted Set 用来存放各种排序数据
• 使用 SSD 作为持久化的容器
–虎扑网大量使用英某某公司的 520 系列
• Redisco
–用 Redis 模拟类 SQL 操作的 Python 类库
45
44. Redisco
MySQL Redis(With Redisco)
select * from person.. Person.objects.all()
..where name = ‘a’ Person.objects.filter(name='a')
..where name != ‘a’ Person.objects.exclude(name='a')
..where age > 5 Person.objects.zfilter(age__gt=5)
..where name = ‘a’ and age > 5 Person.objects.zfilter(name=‘a’,age__gt=5)
..where name = ‘a’ limit 0 1 Person.objects.all().limit(1)
..order by name Person.objects.all().order('name')
46
47. 实际例子 1
• 赛事文字直播
–通过降低页面自动刷新的频率,增加系统相应能力
通过增加一倍的间隔时间,可服务用户数也会增加一倍
假设:单台服务器可以提供 500 的 RPS
自动刷新间隔 可服务用户
12 秒 12*500=6000
24 秒 24*500=12000
49
51. 参考资料
• 虎扑网架构
– 《 2011 架构师大会:虎扑网架构升级之路》 http://wenku.it168.com/d_000055728.shtml
– 《 2011 架构师大会:基于 Symfony 框架下的快速企业级应用开发》 http://wenku.it168.com/d_000055742.shtml
• 林书豪
– 《林书豪 - 百度百科》 http://baike.baidu.com/view/3143415.htm
– 《林书豪 - 维基百科》 http://zh.wikipedia.org/zh/%E6%9E%97%E6%9B%B8%E8%B1%AA
• 季后赛
– 《 NBA 季后赛 - 维基百科》 http://zh.wikipedia.org/wiki/NBA%E5%AD%A3%E5%90%8E%E8%B5%9B
• 网站流量高峰从何而来?恶意攻击
– 《爆吧》 http://baike.baidu.com/view/423530.htm
• Shared-nothing
– 《 Shared nothing architecture 》 http://en.wikipedia.org/wiki/Shared-nothing
– 《 The Share-Nothing Architecture 》 http://zef.me/883/the-share-nothing-architecture
– 《 What is the best PHP MVC framework for scalability 》 http://stackoverflow.com/questions/132056/what-is-the-best-php-mvc-framework-for-
scalability
– 《 将 Session 加密在 Cookie 里的 PHP 实现: sfCookieSessionStoragePlugin 》 http://plugins.symfony-
project.org/get/sfCookieSessionStoragePlugin/sfCookieSessionStoragePlugin-0.9.2.tgz
• Webistrano
– 《 Webistrano - Capistrano deployment the easy way 》 http://www.peritor.com/en/products/webistrano/
– 《 Capistrano 》 https://github.com/capistrano/capistrano
54
52. 参考资料
• RabbitMQ
– 《 rabbitmq.com 》 http://www.rabbitmq.com/
– 《 Rabbitmq Client & Developer Tools 》 http://www.rabbitmq.com/devtools.html
– 《 Rabbitmq in Action 》 http://www.manning.com/videla/
• 微缓存
– 《 MICROCACHING: SPEED YOUR APP UP 250X WITH NO NEW CODE 》 http://fennb.com/microcaching-speed-your-app-up-250x-with-no-n
– 《 Microcaching - Hacker News 》 http://news.ycombinator.com/item?id=3109283
– 《 Nginx - HttpProxyModule 》 http://wiki.nginx.org/HttpProxyModule
– 《 nginx microcaching config example 》 https://gist.github.com/1283573
– 《 Why You Should Always Use Nginx With Microcaching 》 http://www.howtoforge.com/why-you-should-always-use-nginx-with-microcaching
• ESI
– 《 ESI - 维基百科》 http://en.wikipedia.org/wiki/Edge_Side_Includes
– 《 Varnish-cache : ESI Features 》 https://www.varnish-cache.org/trac/wiki/ESIfeatures
– 《 Varnish-cache: Tutorial ESI 》 https://www.varnish-cache.org/docs/3.0/tutorial/esi.html
– 《 Apache Tutorial: Introduction to Server Side Includes 》 http://httpd.apache.org/docs/2.2/howto/ssi.html
– 《 Symfony 2 - HTTP Cache 》 http://symfony.com/doc/current/book/http_cache.html#edge-side-includes
– 《 How to use Varnish to speed up my Website 》 http://symfony.com/doc/current/cookbook/cache/varnish.html
• NoSQL
– 《 Redisco 》 https://github.com/kiddouk/redisco
– 《 Introducing redisco - Python Containers and Simple Models for Redis 》 http://www.timmedina.net/introducing-redisco-python-containers-
and-sim
– 《 python 使用 redis 神器 ---redisco 》 http://pako.iteye.com/blog/1581139
55
Editor's Notes 1. 通过两个体育领域的例子,解释流量高峰产生的原因 2. 中小型网站 3. 虎扑网用到或我以前工作经验 4. 一些经验 1. 介绍林书豪,早期不被看好,纽约尼克斯, 2 月初爆发,媒体:超过姚明、最有可能接近篮球之神乔丹的人 2. 无准备,突然爆发,流量增长 50% 以上,宕机 1.nba 季后赛,东西各 8 支球队,打近百场比赛 2. 比赛场次多,持续时间久,比赛悬念大,预测高峰困难 总结在后一页 加字段,先加字段,后升级 删字段,先升级,后删字段 解决办法 2 :虎扑目前的 passport 和支付都是独立的 4. 举例:有 10000 个页面,透传频率为 10 秒一次,那每秒的 rps 则平均为 1000 注意: 微缓存并不是静态化的替代品,也不能完全代替 Memcache 缓存 1. 实现方式是建索引并且取交集, O ( N*M ) 例子,看电影