Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
基于Tornado后端系统架构
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Zhang ZY
March 16, 2015
Technology
330
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
基于Tornado后端系统架构
Back-end system architecture based on Tornado Framework
Zhang ZY
March 16, 2015
More Decks by Zhang ZY
See All by Zhang ZY
Tornado Development Practice
bufferx
1
190
Other Decks in Technology
See All in Technology
OptunaHub: ブラックボックス最適化のためのプラットフォーム
pfn
PRO
0
140
JAWS-UGグッズ職人仕事の流儀
kentapapa
0
120
オントロジーって 結局なにもの? AI エージェントに「意味」を渡す知識設計の話
greenmidori
0
230
hackers_champloo_2026_tidb
makocchi
0
170
DBについての基礎説明(MySQL編) - コロプラ 2026年度 新卒研修
colopl
0
110
Running Kotlin + Quarkus with Lambda
nealle
1
3.9k
Classmethod Company Deck
satoshi7
1
730
Optunaの最新リリースv5.0について
pfn
PRO
0
120
パッチ更新でterraform initが落ちた日
grimoh
1
140
Temporal 最高!でも改めて「日付」について調べたら人類の負債すぎる件
riyaamemiya
0
170
Gemini Intelligence 入門 ― AppFunctions と Gemini Nano
takuroabe
0
290
[JAWS-UG彩の国埼玉#12]そろそろ準備する?re:Invent2026をやさしく解説 / reinvent2026-sainokuni
sh_fk2
1
350
Featured
See All Featured
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
Optimising Largest Contentful Paint
csswizardry
37
4k
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
The Limits of Empathy - UXLibs8
cassininazir
2
720
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
450
Building AI with AI
inesmontani
PRO
1
1.3k
Un-Boring Meetings
codingconduct
0
460
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
330
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
560
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
ラッコキーワード サービス紹介資料
rakko
1
5.4M
Transcript
基于tornado后端系统架构 twitter: @bf4x github: @bufferx
About Tornado • written in Python • non-blocking • event
driven, base on epoll on linux • tens of thousands of long connections • lightweight
what • web server ◦ support HTTP/WSGI/WebSocket/SPDY
what • asynchronous networking library ◦ DIY Protocol
Like • Nginx • Libevent • ...
Core && Key Point • IOLoop • IOStream • stack_context
• gen
Feature • 单进程单线程 ◦ one ioloop per thread ◦ GIL的原因,
多线程不能有效利用多核 ◦ 切忌阻塞操作
Feature • Proactor(IOStream) ◦ Non-Blocking IO + IOLoop + Biz
Callback
Feature • Coroutine ◦ yield + IOLoop + Callback
应用场景 WEB SERVER • Long Polling/COMET服务 • WebSockets • 接口服务
应用场景 Async Client • 存储服务(Mongo/Redis/Memcached/...) • 基于HTTP的异步下载(HTTPCLIENT) • AMQP •
移动消息推送服务(DIY || APNS) • ....
Best Practices - 聚合服务 • 单请求可指定批量写任务,统一的读任务 • 读任务需消重 • 根据读任务需要请求多个CACHE服务(A类)
• 多个CACHE数据同步返回方可执行批量任务 • 批量任务需要先异步请求计算服务 • 对每个计算服务返回可异步写CACHE(B类)
聚合服务 -- 特点 • 异步接口 ◦ 收到用户REQUEST后, 立即RESPONSE • 同步逻辑写出异步代码
聚合服务 -- 指标 • 高吞吐 ◦ 亿级网络IO • 延迟不敏感 ◦
依赖于各UPSTREAM SERVICE • 日常负载<0.5
Best Practices - Cache Proxy • 读 • 写 •
删除 • MQ
Cache Proxy -- 架构 • lvs(keepalived) + sentinel(>=3) + redis(multi)
Cache Proxy -- 读 • 按业务KEY设置 过期/有效 时间 • 3段论
过期/有效 策略, 可指定读类型
Cache Proxy -- 写 • 缓存时间一般比较大
Cache Proxy -- 删除 • 删除KEY
Cache Proxy -- MQ • 通过MQ通知数据有效/无效 • 每进程缓存全量KEY
Cache Proxy -- MQ • 使用RabbitMQ作为Broker • Exchange Type: direct
• Bind_Queue Instead of fanout exchange
Cache Proxy -- 指标 • 高吞吐 ◦ 亿级REQUEST • 高可用
◦ 50ms可用率P999 • 传输字节 ◦ KB-MB/Request • QPS ◦ Peak: 接近10K ◦ 可Scale Out
Best Practices -- Notify • 异步化第三方服务调用 • 解耦系统间依赖 • 保证任务最终到达
Notify - How • STATUS ◦ NEW,PENDING,SUSPENED,(FAILURE),FINAL • RETRY_POLICY ◦
1,2,4,8,12,16 • ALARM ◦ 离线统计 ◦ 即时触发
Notify - 特点 • 标准化状态转换 • 配置状态机字典 • 使用Timer功能实现Retry Policy
Notify - 指标 • 高可靠 ◦ 线上运行良好 • 可扩展
Notify - 架构 • 分布式 • 使用Mysql事务机制实现分布式任务锁
Best Practices -- Push • 自定义二进制应用层协议 • 长连接 • 流量小
• 耗电少 ◦ 客户端保证
Push -- 指标 • 高并发 ◦ 单机支持几百K量级, 瓶颈在于系统内存大小 • 可扩展
◦ 增添机器可自动Scale Out
Push -- 架构 • 路由服务 • Push Service向路由注册 • Terminal通过路由获取一个可用Push
Service • Terminal与Push Service建立连接并使用私有 协议通信
Push -- 协议设计 • 消息头 ◦ 版本 + 命令ID +
消息体长度 • 消息体 ◦ payload为Scheme Free,对Push Service透明 • 支持命令 ◦ 注册 ◦ 心跳 ◦ 推送 ◦ ...
Push -- 快速开发 • 原型: 3天 • 完成: 1周
整体架构 • RESTFUL API • Name Server
整体架构 -- RESTFUL • GET • PUT • POST •
DELETE
整体架构 -- Name Server • HERE • 有状态服务 ◦ Consistent
Hash... • 无状态服务 ◦ Consistent Hash, WRR
整体架构 -- Name Server 整体架构 -- Name Server(Dubbo Config Server)
服务治理 • SLA • 超时控制 ◦ 作为CLIENT侧时,超时设置为SLA约定延迟时间 • 过载保护 ◦
不能无节制地同时与后端服务建立大量连接 ◦ 可使用滑动窗口, 使用连接池并设置连接阈值 ◦ 每连接增加生命周期, IDLE时及时释放连接 • 服务降级 ◦ 系统整体不可用时, 可支持次要用户服务降级为不可用 ◦ 保证优先级服务可用
SLA • 服务级别协议 • 用于规范服务提供者与使用者双方的约定, 如 响应时间/吞吐/并发/冗灾/数据完整性等, 对于 服务提供方, 是一种服务标准承诺
Push -- 开发 SLA -- 可用性(并发,QPS,RT关系)
SLA -- 冗灾 • 一般可根据具体业务要求定义 • 比如是否需要重试, 重试策略等
SLA -- SPOF • 无单点, 多机多实例, 跨IDC
SLA -- Fail Over • 故障服务自动摘除 • 无状态服务 ◦ 返回错误码
◦ 自动路由至其它服务 • 有状态服务 ◦ 故障数据恢复 ◦ 数据一致性 ◦ ...
SLA -- Recover • 故障服务恢复时间
SLA -- Overload Protection • 针对并发建立过载阈值, 可丢弃过载请求
体系建设 • 代码规范 ◦ PEP8 OR Google Style • 版本控制
• 日志规范 • 私有Pypi • virtualenv • supervisord • 一键发布 ◦ puppet ◦ fabric
调优 • 充分利用多核
调优 • python语言级 ◦ 列表包含/生成器表达式/协程/闭包等 ◦ 使用装饰器 ◦ 使用__slots__ ◦
避免使用(.)运算符 ◦ ...
调优 • C扩展 ◦ Python C Extension ◦ Swig
调优 • pypy ◦ twice times
who use • facebook • 10gen • bitly • quora
• zhihu • admaster • sae • kuxun • ...
周边生态 • https://github. com/tornadoweb/tornado/wiki/Links
我的开源 • twork • tornado • toredis • asyncmongo •
stormed-amqp • ...
人生苦短 我用Python
Thanks