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
Transcript
54 slides
基于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
More Decks by Zhang ZY
See All by Zhang ZY
Tornado Development Practice
bufferx
1
200
Other Decks in Technology
See All in Technology
New Relicのアラート対応をClaude Codeで自動修正するワークフローを作った話
manabuseki
3
740
コーディングできない僕にBobが舞い降りた
shumpei3
0
230
認知の質も文化醸成も。数年取り組んで見えてきた技術広報のKPI設計 / Tech PR KPIs: Lessons on Awareness Quality and Culture After Several Years
bitkey
PRO
0
270
一口馬主の募集馬をAIで診断してみた
yoshimura_datam
0
110
ポケモンバトルのAIエージェント
koji_kawamura
0
320
Taking back control of your AI development
inesmontani
PRO
0
320
Agent Routerで作るマルチテナント 推論基盤 #CNDW2026 / Building Multi-Tenant Inference Platform with Agent Router #CNDW2026
everpeace
5
650
新規事業開発でのAWS活用成功談&失敗談! 〜新規事業開発を専門に行うRelicならではのAWS活用〜
amixedcolor
1
160
ワークフローからハーネスへ — Web フレームワークの作者たちが AI エージェントに向かう理由
azukiazusa1
4
3.6k
実測データからサービスの成長余力を測る。負荷シナリオ・テストデータ・限界値の設計
kazatohiei
1
190
OSSとミニPCと私
anatofuz
1
120
プラガブルで宣言的なソフトウェアファクトリーを支えるTypeScript
timakin
5
2k
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
273
28k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
4
1.2k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
850
Navigating Team Friction
lara
192
16k
The Cult of Friendly URLs
andyhume
79
7.1k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
550
The Invisible Side of Design
smashingmag
301
52k
Ethics towards AI in product and experience design
skipperchong
2
470
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.4k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.7k
Deep Space Network (abreviated)
tonyrice
0
430