q's People
q's Issues
高并发 疑问
提问
1. Node 的 libuv 解决了 c10k,那为啥用 express做个返回 hello world
响应报文的服务 达不到 10k每秒请求数。
测试脚本wrk -t1 -c400 -d30s http://loalhost/test
2. 一个 web app 要提高 每秒请求数(高并发),面临哪些挑战?如何解决?
3. 高并发有哪些指标?如何制定合理基准线?
4. 何时需要考虑用 go 开发 web app,go 的优势是?
预备知识
web app 中的 io/cpu 密集操作
io: 取自 socket db disk 的数据
cpu: 解析请求、路由转发(正则)、渲染模板
重cpu: 图片视频转码
web app 自底向上的结构
一个web应用:tcp server -> http server -> http server framework -> web app
nodejs 提供了 http server
express koa egg 属于 http server framework
blog bbs weibo 属于 web app
http server
* 基于Nodejs自身 tcp server (socket)
* 为上层提供 http协议的技术规范,或者说 为上层提供 http 抽象
* 比如:socket 格式(binary) -> http 报文格式(binary)
http server framework
* 基于 http server
* 为上层 各类型webapp的通用操作 提供一层抽象
* 包括:
* 解析请求, http 报文格式(binary) -> http request object
* 路由转发
web app
* 基于 http server framework
* 视不同类型可能有以下构成:
* 从 db disk 获取文件
* 渲染模板
* 图片转码 音视转码 (反)序列化
c10k 问题
单机每秒可处理10k个连接 c10k wiki
这是 tcp server 这一层要考虑的问题
本质是 io 模型的选型:select < poll < epoll write a c10k web framework, py
现在主流的 web framework 依赖的 tcp server 是 epoll 模型
比如 py 的 gevent, node 的 libuv
自答
1. 基于 express 部署一个 hello world 应用为啥 达不到 10k的请求数
tcp server -> http server -> http server framework -> web app
每一层的封装都有 overload
nodejs 的 libuv 已解决 c10k 问题,测试脚本@td。
nodejs 的 tcp server 对于100k大小的package 是可以达到10k requests/sec 的 nodejs tcp图表。
* c10k 的指标 10k connections/sec ≠ 10k requests/sec,对于 requests 来说,在于 Throughput(流量)。
nodejs 的 http server 对于100k大小的package 降到了 3.5k requests/sec nodejs http图表。
* overhead: 将 socket 封装成了 http 报文
express 的 hello world
例子,用 wrk
测试远小于 3.5k
* overhead: 解析请求、路由转发
为什么 http server framework 喜欢测 hello world
因为http server framework 关注 web app 需要处理的通用部分:解析请求、路由转发。
2016 web framework 性能基准
2. 一个 web app 要提高 每秒请求数,面临哪些挑战?如何解决?
< 50qps
,随便
50 - 100
,DB极限 -> Cache + 多DB负载
300 - 800
,带宽极限 -> CDN加速 + 异地缓存 + 多机负载
500 - 1k
,内网带宽极限 + Redis极限 -> ?
1k - 2k
,-> 锁模式极限 -> 数据分布存储、分布处理
2k+
-> c10k 极限 -> ?
高并发 瓶颈与提高
分布式 CAP 理论
CAP 原则:
* C 一致性consistent
* A 可用性
* P 网络分区的可容忍性 :用户等待 or 用旧数据
* 网络分区:当一个服务挂掉后 网络部分瘫痪部分运行的情况
即,出现 P情况下,选 A or C
CAP 例子
* 小米,排队抢手机; 牺牲 A;
* 电商,秒杀活动;牺牲 A;
* 也有些秒杀,C 不重要,保证了 A;比如某淘宝店秒杀多卖了100条内裤,没库存,进货周期短,那就等下次补货再发给用户;
3. 高并发有哪些指标?如何制定合理基准线?
RT Throughput 并发用户数 QPS
ref
4. 何时需要考虑用 go 开发 web app,go 的优势是?
接口是 io 密集 -> 在于io模型,选择 go c++ 还是 node py 区别不大
接口是 cpu 密集 -> 多线程能力以重复利用多核cpu, 选择 go c++
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.