您的位置:首页 > 数据库 > Redis

nginx+php+redis vs nginx+lua+redis

2015-12-13 20:58 507 查看
最近在做通知系统,前端用的轮询方式(后端压力不小),因为时间比较紧,开始我准备把未读通知标识存在数据库中,但是每次拿数据的时候需要查询一遍数据库,总监说你这样效率较低,说你可以根据用户id作为key放在redis中存储。说说自己在做这个的一些体会和闲下来总结的一些笔记。

phpredis connect pconnect

我最开始使用的是connect,每次请求完毕关闭连接

<?php
$redis = new Redis();
$nError = $redis->connect('127.0.0.1', 6379);
if ($nError != 1)
echo -9998;
$redis->incr('newCount');
$redis->close();


而另外一种是pconnect

<?php
$redis = new Redis();
$nError = $redis->pconnect('127.0.0.1', 6379);
if ($nError != 1)
echo -9998;
$redis->incr('newCount');


进行了一下ab压测 ab -c500 -n10000 http://192.168.23.128/redis.php
1.connect

Concurrency Level: 500
Time taken for tests: 25.484 seconds
Complete requests: 10000
Failed requests: 146
(Connect: 0, Receive: 0, Length: 146, Exceptions: 0)
Non-2xx responses: 146
Total transferred: 1868840 bytes
HTML transferred: 78402 bytes
Requests per second: 392.40 [#/sec] (mean)
Time per request: 1274.198 [ms] (mean)
Time per request: 2.548 [ms] (mean, across all concurrent requests)
Transfer rate: 71.62 [Kbytes/sec] received

2.pconnect

Concurrency Level: 500
Time taken for tests: 13.274 seconds
Complete requests: 10000
Failed requests: 4
(Connect: 0, Receive: 0, Length: 4, Exceptions: 0)
Non-2xx responses: 4
Total transferred: 1792160 bytes
HTML transferred: 2148 bytes
Requests per second: 753.34 [#/sec] (mean)
Time per request: 663.713 [ms] (mean)
Time per request: 1.327 [ms] (mean, across all concurrent requests)
Transfer rate: 131.85 [Kbytes/sec] received

会发现pconnect比connect会快一倍(会重用redis连接,不需要消耗建立连接时间)

phpredis api上有描述The connection will not be closed on
close
or end of request until the php process ends


也就是说在php脚本访问结束,也并不会关闭连接,直到php进程结束

我在做pconnect的ab压测结束后,发现有几个redis的连接一直在,直到我杀死php-fpm的进程,也可以通过设置redis.conf文件中的timeout参数

nginx+lua+redis 短连接 连接池

上篇文章提到过nginx+lua+redis,之所以没用在项目中,主要原因是时间紧,对新的东西了解的不多,这里贴一下我闲暇时对于nginx+lua+redis的ab测试

短连接

local redis = require "resty.redis"
local red = redis:new()
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
ok,err=red:incr("newCount")
if not ok then
ngx.say("failed to increase newCount",err)
return
end

local ok,err = red:close()
if not ok then
ngx.say("close redis error : ",err)
return
end


连接池

local redis = require "resty.redis"
local red = redis:new()
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
ok,err=red:incr("newCount")
if not ok then
ngx.say("failed to increase newCount",err)
return
end

local pool_max_idle_time = 10000 --10s
local pool_size = 100 --连接池大小
local ok,err = red:set_keepalive(pool_max_idle_time,pool_size)
if not ok then
ngx.say("set keepalive error : ",err)
return
end


ab测试 ab -c500 -n10000 http://192.168.23.128/redis
短连接

Concurrency Level: 500
Time taken for tests: 4.791 seconds
Complete requests: 10000
Failed requests: 72
(Connect: 0, Receive: 0, Length: 72, Exceptions: 0)
Total transferred: 1492016 bytes
HTML transferred: 1944 bytes
Requests per second: 2087.06 [#/sec] (mean)
Time per request: 239.571 [ms] (mean)
Time per request: 0.479 [ms] (mean, across all concurrent requests)
Transfer rate: 304.09 [Kbytes/sec] received

连接池

Concurrency Level: 500
Time taken for tests: 2.986 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 1490000 bytes
HTML transferred: 0 bytes
Requests per second: 3348.62 [#/sec] (mean)
Time per request: 149.315 [ms] (mean)
Time per request: 0.299 [ms] (mean, across all concurrent requests)
Transfer rate: 487.25 [Kbytes/sec] received

从上面结果可以看出nginx+lua+redis的效率比phpredis的快5倍左右,以后有机会试试在项目中使用

参考链接
http://blog.csdn.net/qmhball/article/details/46988111 https://github.com/openresty/lua-resty-redis
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: