简介
redis2-nginx-module 是一个支持 Redis 2.0 协议的 Nginx upstream 模块,它可以让 Nginx 以非阻塞方式直接防问远方的 Redis 服务,同时支持 TCP 协议和 Unix Domain Socket 模式,并且可以启用强大的 Redis 连接池功能。详情见github https://github.com/openresty/redis2-nginx-module
示例
upstream redisPool{
server ${KVSTORE_HOST}:${KVSTORE_PORT};
keepalive 1024;
}
server {
location = /v1/qrcode/loginInfo {
default_type application/json;
redis2_query auth ${KVSTORE_AUTH};
redis2_query select ${KVSTORE_DB};
redis2_query get $arg_uuid;
redis2_pass redisPool;
}
}
测试结果
对比使用nginx+php+redis,性能大概提升了6倍左右
API | COMMAND | RESULT |
---|---|---|
使用redis2-nginx-module从redis 中取键值为hello的值 |
siege '${url}?uuid=hello' -c 500 -r 50 -b -q |
Transactions: 25000 hits Availability: 100.00 % Elapsed time: 2.31 secs Data transferred: 1.07 MB Response time: 0.04 secs Transaction rate: 10822.51 trans/sec Throughput: 0.46 MB/sec Concurrency: 479.55 Successful transactions: 25000 Failed transactions: 0 Longest transaction: 0.15 Shortest transaction: 0.01 |
使用redis+php+redis从redis 中取键值为hello的值 |
siege '{$url}?uuid=hello' -c 500 -r 50 -b -q |
Transactions: 25000 hits Availability: 100.00 % Elapsed time: 16.44 secs Data transferred: 0.17 MB Response time: 0.32 secs Transaction rate: 1520.68 trans/sec Throughput: 0.01 MB/sec Concurrency: 493.92 Successful transactions: 25000 Failed transactions: 0 Longest transaction: 0.58 Shortest transaction: 0.02 |
这种使用方法适合对性能并发要求高的接口,比如二维码登陆中客户端轮询用户是否扫码成功,订单支付流程中客户端轮询订单是否支付成功
由于没有做参数检查,为了安全考虑, redis中db应该选择单独的库
网友评论