春哥老师,想请教您C1000K关于Nginx高性能多核QPS,线性增长的问题

792 views
Skip to first unread message

Lucien Li

unread,
Jan 7, 2016, 10:31:08 AM1/7/16
to openresty
您好:
      章老师好,打扰您了,想请教您个问题! 关于单机C1000K的优化经验! 目前我用Nginx开发了一个Proxy, 用Upstream模块开发的!  
单核的QPS在2w多, 2核的QPS在4w左右, 4核心的QPS在6w左右, 6核心的QPS在8w左右....

目前的情况是:
目前我的是千兆网卡, 每秒读写吞吐也就在70M左右, 通过dstat 查询的, 并且内存, 以及upstream后端服务器CPU也都不是瓶颈。
声明下,我的upstream模块里面并没有用mmap之类的共享内存, 也没有锁之类的东西。
Nginx CPU差不多快跑满了, 但是增加核数, 没线性增长


问题1: 当我线性的增加Nginx配置文件里面的CPU核数, 相应的QPS应该是线性增长, 不求非常精确, 但是也不至于,相差太多。 
目前我的Nginx绑定了CPU情缘性10核心,但是QPS在13w多

问题2: 关于高性能Nginx服务器的C1000k的优化经验, 非常感谢大家的分享经验


Nginx配置文件如下:
user root;
worker_processes  10;
#daemon off; 
#master_process  off;

worker_cpu_affinity
#000000000001
#000000000010
000000000100
000000001000
000000010000
000000100000
000001000000
000010000000
000100000000
001000000000
010000000000
100000000000
;

#error_log   logs/error.log debug;
error_log   logs/error.log;

worker_rlimit_core 200m;
working_directory /tmp;

pid logs/nginx.pid;
events {
worker_connections 204800;
        use epoll;
        accept_mutex off;
        multi_accept on;
}

http {
    sendfile        on;
    tcp_nodelay        on;
    tcp_nopush  on;
access_log off;

server_tokens off;
reset_timedout_connection on;

keepalive_timeout 120;
keepalive_requests 100000;

client_max_body_size 20m;
client_body_buffer_size 1024k;
client_body_temp_path /tmp;

upstream redis_cluster {
testupstream_node $node_ip;
server 0.0.0.0;
keepalive 1024;
}
server {
        listen   80;
        server_name  localhost backlog=204800;

set $backserver "redis_cluster";
set $node_ip "";

location ~* "^/hdp/kvstore/" {
testupstream_pass $backserver;
testupstream_next_upstream error timeout invalid_response;
        }
    }
}

内核参数:
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_max_tw_buckets = 262144
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 200000
net.core.rmem_default = 67108864
net.core.wmem_default = 67108864
net.core.rmem_max = 67108864
net.core.wmem_max = 67108864
net.ipv4.tcp_rmem = 4096 87380 6291456
net.ipv4.tcp_wmem = 4096 65536 4194304
net.ipv4.tcp_mem = 3097431 4129911 6194862
net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.ip_forward = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_keepalive_time = 120
vm.overcommit_memory = 1
fs.file-max = 1048576

Lucien Li

unread,
Jan 7, 2016, 10:41:58 AM1/7/16
to openresty
cpuspeed和irqbalance服务器的两大性能杀手
我也都关闭了, 并且预留了2核心给网卡软中断, 观察网卡软中断也并不高

在 2016年1月7日星期四 UTC+8下午11:31:08,Lucien Li写道:

泡泡鱼

unread,
Jan 7, 2016, 10:43:11 AM1/7/16
to openresty
1、NGINX目前还做不到C1000K,是C10K;
2、其次NGINX对TCP及之上的HTTP代理是TPS,非QPS;(T--事务,Q--请求,还是略有差别)
3、要定位一下瓶颈,是不是在accept
4、看你sendfile on的,查下buffer是否用到了硬盘,硬盘是有I/O瓶颈的。



------------------ 原始邮件 ------------------
发件人: "Lucien Li";<lihan...@126.com>;
发送时间: 2016年1月7日(星期四) 晚上11:31
收件人: "openresty"<open...@googlegroups.com>;
主题: [openresty] 春哥老师,想请教您C1000K关于Nginx高性能多核QPS,线性增长的问题
--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html

Lucien Li

unread,
Jan 7, 2016, 10:44:58 AM1/7/16
to openresty
目前也都是内外长连接,后端也是长连接
time wait就几个, 也不高!


在 2016年1月7日星期四 UTC+8下午11:31:08,Lucien Li写道:
您好:

泡泡鱼

unread,
Jan 7, 2016, 10:48:37 AM1/7/16
to openresty
试试多找台机对后端redis加压,看看是不是redis性能到上限了。


------------------ 原始邮件 ------------------
发件人: "Lucien Li";<lihan...@126.com>;
发送时间: 2016年1月7日(星期四) 晚上11:44
收件人: "openresty"<open...@googlegroups.com>;
主题: [openresty] Re: 春哥老师,想请教您C1000K关于Nginx高性能多核QPS,线性增长的问题

泡泡鱼

unread,
Jan 7, 2016, 10:53:42 AM1/7/16
to Lucien Li, openresty
对了,你还要查一下cpu多核中断是都做了负载均衡

---原始邮件---
发件人: "Lucien Li"<lihan...@126.com>
发送时间: 2016年1月7日 23:42:02
收件人: "openresty"<open...@googlegroups.com>;
主题: [openresty] Re: 春哥老师,想请教您C1000K关于Nginx高性能多核QPS,线性增长的问题

cpuspeed和irqbalance服务器的两大性能杀手
我也都关闭了, 并且预留了2核心给网卡软中断, 观察网卡软中断也并不高

vincen...@gmail.com

unread,
Jan 7, 2016, 12:34:20 PM1/7/16
to openresty


On Thursday, January 7, 2016 at 7:44:58 AM UTC-8, Lucien Li wrote:
目前也都是内外长连接,后端也是长连接
time wait就几个, 也不高!

可能和你的问题没什么关系,但可以了解一下为何Linux在百万TCP连接时的一些瓶颈
 

The Secret to 10 Million Concurrent Connections -The Kernel is the Problem, Not the Solution


这里我分享一下, 春哥多包涵:) 

如何利用 mTCP 和 DPDK 构造百万千万并发TCP连接

http://my.oschina.net/u/2600415/blog/597225




国内有人移植nginx 到FreeBSD tcp/ip userspace stack + DPDK, 但用户太的TCP/IP stack没开源 https://github.com/opendp/dpdk-nginx



泡泡鱼

unread,
Jan 7, 2016, 1:01:08 PM1/7/16
to openresty
https://github.com/opendp/dpdk-odp----用户态的协议栈


------------------ 原始邮件 ------------------
发送时间: 2016年1月8日(星期五) 凌晨1:34
收件人: "openresty"<open...@googlegroups.com>;
主题: [openresty] Re: 春哥老师,想请教您C1000K关于Nginx高性能多核QPS,线性增长的问题

Yichun Zhang (agentzh)

unread,
Jan 7, 2016, 1:40:21 PM1/7/16
to openresty
Hello!

2016-01-07 7:31 GMT-08:00 Lucien Li:
> 章老师好,打扰您了,想请教您个问题! 关于单机C1000K的优化经验! 目前我用Nginx开发了一个Proxy,
> 用Upstream模块开发的!
> 单核的QPS在2w多, 2核的QPS在4w左右, 4核心的QPS在6w左右, 6核心的QPS在8w左右....
>
> 目前的情况是:
> 目前我的是千兆网卡, 每秒读写吞吐也就在70M左右, 通过dstat 查询的, 并且内存, 以及upstream后端服务器CPU也都不是瓶颈。
> 声明下,我的upstream模块里面并没有用mmap之类的共享内存, 也没有锁之类的东西。
> Nginx CPU差不多快跑满了, 但是增加核数, 没线性增长

可以使用火焰图工具进行性能分析:

https://openresty.org/#Profiling

另外,开启 listen 配置指令的 reuseport 参数有可能也会有帮助:

http://nginx.org/en/docs/http/ngx_http_core_module.html#listen

Regards,
-agentzh

Yichun Zhang (agentzh)

unread,
Jan 7, 2016, 1:41:21 PM1/7/16
to openresty
Hello!

2016-01-07 7:43 GMT-08:00 泡泡鱼:
> 1、NGINX目前还做不到C1000K,是C10K;

朱德江做过单机 C2000K 的尝试,不过是慢连接的场景。可以参见

http://iresty.com/download/ebook/2015_con/zhudejiang.pdf

Regards,
-agentzh

Lucien Li

unread,
Jan 7, 2016, 9:29:10 PM1/7/16
to openresty
我的都是内外请求, 作为内外的api 服务工作, 几乎都是上连接, upstream到后端也是长连接。 
目前测试的QPS没有线性增长, 以前测试过框架不包含业务逻辑单机QPS最高30w, 网卡就打满了, 我在优化优化吧。 
先排除业务逻辑测试下框架是否能线性增长吧。 
感谢大家了

在 2016年1月8日星期五 UTC+8上午2:41:21,agentzh写道:

Lucien Li

unread,
Jan 7, 2016, 10:06:29 PM1/7/16
to openresty
 [emerg] invalid parameter "reuseport"

应该是3.9的内核才支持的吧。 
我目前的内核是: 2.6.32-573.el6.x86_64

在 2016年1月8日星期五 UTC+8上午2:40:21,agentzh写道:

Lucien Li

unread,
Jan 7, 2016, 10:09:05 PM1/7/16
to openresty
感谢了, 我学习下

在 2016年1月8日星期五 UTC+8上午1:34:20,vincen...@gmail.com写道:

sh198...@gmail.com

unread,
Apr 22, 2024, 9:30:18 PMApr 22
to openresty
链接失效了,春哥能否更新下链接呢?
Reply all
Reply to author
Forward
0 new messages