簡介
? ? ? ?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應該選擇單獨的庫