Recently in the notification system, the front end of the polling method (the back end of the pressure is not small), because the time is tight, beginning I am ready to put the unread notification ID exists in the database, but each time the data need to query the database, the director said you are less efficient, said you can be based on the user ID as key in Redis storage Talk about yourself in doing this some of the experience and idle down to summarize some notes.
Phpredis Connect Pconnect
I started with connect and closed the connection every time I requested it.
1<?PHP2 $redis=NewRedis ();3 $nError=$redis->connect (' 127.0.0.1 ', 6379); 4 if($nError! = 1)5 Echo-9998;6 $redis->INCR (' Newcount ');7 $redis->close ();
And the other one is pconnect.
1 <? PHP 2 $redis New Redis (); 3 $nError $redis->pconnect (' 127.0.0.1 ', 6379); 4 if ($nError ! = 1) 5 echo -9998; 6 $redis->incr (' Newcount ');
A bit of ab pressure test 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
You'll find that the pconnect is a few times faster than connect (it reuses the Redis connection and does not need to be consumed to establish the connection time)
The Phpredis API has a description of the connection would not being closed on close
or end of request until the PHP process ends
This means that at the end of the PHP script access, the connection is not closed until the end of the PHP process
After I did the AB Pconnect, I found that there were several redis connections until I killed the php-fpm process, or by setting the timeout parameter in the redis.conf file
Nginx+lua+redis Short Connection Connection pool
The previous article mentioned Nginx+lua+redis, the reason is useless in the project, the main reason is the time is tight, the new things do not know much, here I put my spare time for Nginx+lua+redis AB test
Short connection
1 LocalRedis =require "Resty.redis"2 LocalRed =redis:new ()3 LocalOK, err = Red:connect ("127.0.0.1",6379)4 if notOk Then5Ngx.say ("failed to connect:", Err)6 return7 End8OK,ERR=RED:INCR ("Newcount")9 if notOk ThenTenNgx.say ("failed to increase Newcount", Err) One return A End - - LocalOk,err =red:close () the if notOk Then -Ngx.say ("Close Redis Error:", Err) - return - End
Connection pool
1 LocalRedis =require "Resty.redis"2 LocalRed =redis:new ()3 LocalOK, err = Red:connect ("127.0.0.1",6379)4 if notOk Then5Ngx.say ("failed to connect:", Err)6 return7 End8OK,ERR=RED:INCR ("Newcount")9 if notOk ThenTenNgx.say ("failed to increase Newcount", Err) One return A End - - LocalPool_max_idle_time =10000 --10s the LocalPool_size = - --Connection Pool Size - LocalOk,err =red:set_keepalive (pool_max_idle_time,pool_size) - if notOk Then -Ngx.say ("set keepalive error:", Err) + return - End
AB Test ab-c500-n10000 Http://192.168.23.128/redis
Short connection
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
Connection pool
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
From the above results can be seen Nginx+lua+redis efficiency is about 5 times times faster than the Phpredis, later have the opportunity to try to use in the project
Reference links
http://blog.csdn.net/qmhball/article/details/46988111
Https://github.com/openresty/lua-resty-redis
Nginx+php+redis vs Nginx+lua+redis