江湖傳言,在redis中,一個command("get or set key value")中value的長度超過1k的時候,其性能急劇降低。java
稍微看了下redis的代碼,發現它的網絡模塊關閉了nagel算法,若是修改redis的源碼啓動這個算法會如何呢?node
爲了驗證這個算法啓用與否,是否影響了redis的性能。先不改變redis的代碼,利用redis-benchmark進行一番測試,執行下面的命令:redis
./redis-benchmark -p 30000 -n 50000 -k 1 -d 2048 -q
上面命令相關參數意義爲:鏈接服務器port30000、測試50000次、打開keepalive選項、value長度2k、只輸出結果不輸出測試過程。 算法
測試結果是:服務器
SET: 51599.59 requests per second GET: 50403.23 requests per second INCR: 55493.89 requests per second LPUSH: 54112.55 requests per second LPOP: 48875.86 requests per second SADD: 54644.81 requests per second SPOP: 54112.55 requests per second LPUSH (needed to benchmark LRANGE): 49067.71 requests per second LRANGE_600 (first 600 elements): 400.68 requests per second MSET (10 keys): 33134.53 requests per second
下面是/redis-2.8.19/networking.c的createClient函數:網絡
53 redisClient *createClient(int fd) { 54 redisClient *c = zmalloc(sizeof(redisClient)); 55 56 /* passing -1 as fd it is possible to create a non connected client. 57 * This is useful since all the Redis commands needs to be executed 58 * in the context of a client. When commands are executed in other 59 * contexts (for instance a Lua script) we need a non connected client. */ 60 if (fd != -1) { 61 anetNonBlock(NULL,fd); 62 // anetEnableTcpNoDelay(NULL,fd); 63 if (server.tcpkeepalive) 64 anetKeepAlive(NULL,fd,server.tcpkeepalive); 65 if (aeCreateFileEvent(server.el,fd,AE_READABLE, 66 readQueryFromClient, c) == AE_ERR) 67 { 68 close(fd); 69 zfree(c); 70 return NULL; 71 } 72 }
請注意,我已經把line 62註釋掉,採用系統默認設置。socket
執行一樣的命令,其測試結果爲:tcp
SET: 50968.40 requests per second GET: 50607.29 requests per second INCR: 55432.37 requests per second LPUSH: 47438.33 requests per second LPOP: 49950.05 requests per second SADD: 54945.05 requests per second SPOP: 55187.64 requests per second LPUSH (needed to benchmark LRANGE): 47709.93 requests per second LRANGE_600 (first 600 elements): 398.41 requests per second MSET (10 keys): 26766.60 requests per second
一樣地,能夠修改代碼以明確啓用Nagel算法,一樣的代碼塊修改後爲:
函數
53 redisClient *createClient(int fd) { 54 redisClient *c = zmalloc(sizeof(redisClient)); 55 56 /* passing -1 as fd it is possible to create a non connected client. 57 * This is useful since all the Redis commands needs to be executed 58 * in the context of a client. When commands are executed in other 59 * contexts (for instance a Lua script) we need a non connected client. */ 60 if (fd != -1) { 61 anetNonBlock(NULL,fd); 62 // anetEnableTcpNoDelay(NULL,fd); 63 anetDisableTcpNoDelay(NULL,fd); 64 if (server.tcpkeepalive) 65 anetKeepAlive(NULL,fd,server.tcpkeepalive); 66 if (aeCreateFileEvent(server.el,fd,AE_READABLE, 67 readQueryFromClient, c) == AE_ERR) 68 { 69 close(fd); 70 zfree(c); 71 return NULL; 72 } 73 }
明確地啓用Nagel算法後,其測試結果爲:
性能
SET: 49164.21 requests per second GET: 48496.61 requests per second INCR: 52910.05 requests per second LPUSH: 50556.12 requests per second LPOP: 49164.21 requests per second SADD: 53937.43 requests per second SPOP: 53248.14 requests per second LPUSH (needed to benchmark LRANGE): 48169.56 requests per second LRANGE_600 (first 600 elements): 406.73 requests per second MSET (10 keys): 34106.41 requests per second
從以上對比結果來看,大部分狀況下啓用nagel算法後,redis的性能[吞吐率]約有百分之二的提高。
其實真實使用過程當中,redis的配置文件中有"repl-disable-tcp-nodelay"這個配置項,配置爲no便可讓其在redis主從同步數據時發揮做用。