今天遇到个Redis奇慢的问题,断点分析发现跟multi有关。

由于本人太忙不想去阅读Redis Server的源代码(其实是懒),就通过TCPDump来分析吧。

步骤:

1. 先在Redis Server上启动TCPDump: tcpdump -i eno16777736 port 6379 -X

2. 测试Multi:

  1. <?php
  2.  
  3. $redis = new redis();
  4. $result = $redis->connect('localhost', 6379);
  5. //默认是Redis::MULTI
  6. $multi = $redis->multi();
  7.  
  8. for($i=0;$i<100;$i++)
  9. $multi->set('testkey' . (string)$i, 'helloworld' . (string)$i);
  10.  
  11. $replies = $multi->exec();
  12.  
  13. var_dump($replies);
  14.  
  15. ?>

  

TCPDump抓包:

  1. sudo tcpdump -i lo0 port 6379 -XX -vvv

  

TCPDump数据:

  1. 19:09:22.437455 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 64, bad cksum 0 (->3cb6)!)
  2. localhost.60828 > localhost.6379: Flags [S], cksum 0xfe34 (incorrect -> 0x48c3), seq 3085749856, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 472321303 ecr 0,sackOK,eol], length 0
  3. 0x0000: 0200 0000 4500 0040 0000 4000 4006 0000 ....E..@..@.@...
  4. 0x0010: 7f00 0001 7f00 0001 ed9c 18eb b7ec ce60 ...............`
  5. 0x0020: 0000 0000 b002 ffff fe34 0000 0204 3fd8 .........4....?.
  6. 0x0030: 0103 0305 0101 080a 1c27 0d17 0000 0000 .........'......
  7. 0x0040: 0402 0000 ....
  8. 19:09:22.437505 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 64, bad cksum 0 (->3cb6)!)
  9. localhost.6379 > localhost.60828: Flags [S.], cksum 0xfe34 (incorrect -> 0x8ab4), seq 934042899, ack 3085749857, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 472321303 ecr 472321303,sackOK,eol], length 0
  10. 0x0000: 0200 0000 4500 0040 0000 4000 4006 0000 ....E..@..@.@...
  11. 0x0010: 7f00 0001 7f00 0001 18eb ed9c 37ac 5d13 ............7.].
  12. 0x0020: b7ec ce61 b012 ffff fe34 0000 0204 3fd8 ...a.....4....?.
  13. 0x0030: 0103 0305 0101 080a 1c27 0d17 1c27 0d17 .........'...'..
  14. 0x0040: 0402 0000 ....
  15. 19:09:22.437515 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)
  16. localhost.60828 > localhost.6379: Flags [.], cksum 0xfe28 (incorrect -> 0xd2d0), seq 1, ack 1, win 12759, options [nop,nop,TS val 472321303 ecr 472321303], length 0
  17. 0x0000: 0200 0000 4500 0034 0000 4000 4006 0000 ....E..4..@.@...
  18. 0x0010: 7f00 0001 7f00 0001 ed9c 18eb b7ec ce61 ...............a
  19. 0x0020: 37ac 5d14 8010 31d7 fe28 0000 0101 080a 7.]...1..(......
  20. 0x0030: 1c27 0d17 1c27 0d17 .'...'..
  21. 19:09:22.437525 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)
  22. localhost.6379 > localhost.60828: Flags [.], cksum 0xfe28 (incorrect -> 0xd2d0), seq 1, ack 1, win 12759, options [nop,nop,TS val 472321303 ecr 472321303], length 0
  23. 0x0000: 0200 0000 4500 0034 0000 4000 4006 0000 ....E..4..@.@...
  24. 0x0010: 7f00 0001 7f00 0001 18eb ed9c 37ac 5d14 ............7.].
  25. 0x0020: b7ec ce61 8010 31d7 fe28 0000 0101 080a ...a..1..(......
  26. 0x0030: 1c27 0d17 1c27 0d17 .'...'..
  27. 19:09:22.437866 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 67, bad cksum 0 (->3cb3)!)
  28. localhost.60828 > localhost.6379: Flags [P.], cksum 0xfe37 (incorrect -> 0x7d88), seq 1:16, ack 1, win 12759, options [nop,nop,TS val 472321303 ecr 472321303], length 15: RESP "MULTI"
  29. 0x0000: 0200 0000 4500 0043 0000 4000 4006 0000 ....E..C..@.@...
  30. 0x0010: 7f00 0001 7f00 0001 ed9c 18eb b7ec ce61 ...............a
  31. 0x0020: 37ac 5d14 8018 31d7 fe37 0000 0101 080a 7.]...1..7......
  32. 0x0030: 1c27 0d17 1c27 0d17 2a31 0d0a 2435 0d0a .'...'..*1..$5..
  33. 0x0040: 4d55 4c54 490d 0a MULTI..
  34. 19:09:22.437885 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)
  35. localhost.6379 > localhost.60828: Flags [.], cksum 0xfe28 (incorrect -> 0xd2c2), seq 1, ack 16, win 12758, options [nop,nop,TS val 472321303 ecr 472321303], length 0
  36. 0x0000: 0200 0000 4500 0034 0000 4000 4006 0000 ....E..4..@.@...
  37. 0x0010: 7f00 0001 7f00 0001 18eb ed9c 37ac 5d14 ............7.].
  38. 0x0020: b7ec ce70 8010 31d6 fe28 0000 0101 080a ...p..1..(......
  39. 0x0030: 1c27 0d17 1c27 0d17 .'...'..
  40. 19:09:22.437979 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 57, bad cksum 0 (->3cbd)!)
  41. localhost.6379 > localhost.60828: Flags [P.], cksum 0xfe2d (incorrect -> 0x5259), seq 1:6, ack 16, win 12758, options [nop,nop,TS val 472321303 ecr 472321303], length 5: RESP "OK"
  42. 0x0000: 0200 0000 4500 0039 0000 4000 4006 0000 ....E..9..@.@...
  43. 0x0010: 7f00 0001 7f00 0001 18eb ed9c 37ac 5d14 ............7.].
  44. 0x0020: b7ec ce70 8018 31d6 fe2d 0000 0101 080a ...p..1..-......
  45. 0x0030: 1c27 0d17 1c27 0d17 2b4f 4b0d 0a .'...'..+OK..
  46. 19:09:22.437993 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)
  47. localhost.60828 > localhost.6379: Flags [.], cksum 0xfe28 (incorrect -> 0xd2bc), seq 16, ack 6, win 12759, options [nop,nop,TS val 472321303 ecr 472321303], length 0
  48. 0x0000: 0200 0000 4500 0034 0000 4000 4006 0000 ....E..4..@.@...
  49. 0x0010: 7f00 0001 7f00 0001 ed9c 18eb b7ec ce70 ...............p
  50. 0x0020: 37ac 5d19 8010 31d7 fe28 0000 0101 080a 7.]...1..(......
  51. 0x0030: 1c27 0d17 1c27 0d17 .'...'..
  52. 19:09:22.438224 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 97, bad cksum 0 (->3c95)!)
  53. localhost.60828 > localhost.6379: Flags [P.], cksum 0xfe55 (incorrect -> 0x6423), seq 16:61, ack 6, win 12759, options [nop,nop,TS val 472321303 ecr 472321303], length 45: RESP "SET" "testkey0" "helloworld0"
  54. 0x0000: 0200 0000 4500 0061 0000 4000 4006 0000 ....E..a..@.@...
  55. 0x0010: 7f00 0001 7f00 0001 ed9c 18eb b7ec ce70 ...............p
  56. 0x0020: 37ac 5d19 8018 31d7 fe55 0000 0101 080a 7.]...1..U......
  57. 0x0030: 1c27 0d17 1c27 0d17 2a33 0d0a 2433 0d0a .'...'..*3..$3..
  58. 0x0040: 5345 540d 0a24 380d 0a74 6573 746b 6579 SET..$8..testkey
  59. 0x0050: 300d 0a24 3131 0d0a 6865 6c6c 6f77 6f72 0..$11..hellowor
  60. 0x0060: 6c64 300d 0a ld0..
  61. 19:09:22.438245 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)
  62. localhost.6379 > localhost.60828: Flags [.], cksum 0xfe28 (incorrect -> 0xd291), seq 6, ack 61, win 12757, options [nop,nop,TS val 472321303 ecr 472321303], length 0
  63. 0x0000: 0200 0000 4500 0034 0000 4000 4006 0000 ....E..4..@.@...
  64. 0x0010: 7f00 0001 7f00 0001 18eb ed9c 37ac 5d19 ............7.].
  65. 0x0020: b7ec ce9d 8010 31d5 fe28 0000 0101 080a ......1..(......
  66. 0x0030: 1c27 0d17 1c27 0d17 .'...'..
  67. 19:09:22.438291 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 61, bad cksum 0 (->3cb9)!)
  68. localhost.6379 > localhost.60828: Flags [P.], cksum 0xfe31 (incorrect -> 0xae97), seq 6:15, ack 61, win 12757, options [nop,nop,TS val 472321303 ecr 472321303], length 9: RESP "QUEUED"
  69. 0x0000: 0200 0000 4500 003d 0000 4000 4006 0000 ....E..=..@.@...
  70. 0x0010: 7f00 0001 7f00 0001 18eb ed9c 37ac 5d19 ............7.].
  71. 0x0020: b7ec ce9d 8018 31d5 fe31 0000 0101 080a ......1..1......
  72. 0x0030: 1c27 0d17 1c27 0d17 2b51 5545 5545 440d .'...'..+QUEUED.
  73. 0x0040: 0a .
  74. 19:09:22.438307 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)
  75. localhost.60828 > localhost.6379: Flags [.], cksum 0xfe28 (incorrect -> 0xd287), seq 61, ack 15, win 12758, options [nop,nop,TS val 472321303 ecr 472321303], length 0
  76. 0x0000: 0200 0000 4500 0034 0000 4000 4006 0000 ....E..4..@.@...
  77. 0x0010: 7f00 0001 7f00 0001 ed9c 18eb b7ec ce9d ................
  78. 0x0020: 37ac 5d22 8010 31d6 fe28 0000 0101 080a 7.]"..1..(......
  79. 0x0030: 1c27 0d17 1c27 0d17 .'...'..
  80. 19:09:22.438362 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 97, bad cksum 0 (->3c95)!)
  81. localhost.60828 > localhost.6379: Flags [P.], cksum 0xfe55 (incorrect -> 0x61ee), seq 61:106, ack 15, win 12758, options [nop,nop,TS val 472321303 ecr 472321303], length 45: RESP "SET" "testkey1" "helloworld1"
  82. 0x0000: 0200 0000 4500 0061 0000 4000 4006 0000 ....E..a..@.@...
  83. 0x0010: 7f00 0001 7f00 0001 ed9c 18eb b7ec ce9d ................
  84. 0x0020: 37ac 5d22 8018 31d6 fe55 0000 0101 080a 7.]"..1..U......
  85. 0x0030: 1c27 0d17 1c27 0d17 2a33 0d0a 2433 0d0a .'...'..*3..$3..
  86. 0x0040: 5345 540d 0a24 380d 0a74 6573 746b 6579 SET..$8..testkey
  87. 0x0050: 310d 0a24 3131 0d0a 6865 6c6c 6f77 6f72 1..$11..hellowor
  88. 0x0060: 6c64 310d 0a ld1..
  89. 19:09:22.438378 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)
  90. localhost.6379 > localhost.60828: Flags [.], cksum 0xfe28 (incorrect -> 0xd25c), seq 15, ack 106, win 12756, options [nop,nop,TS val 472321303 ecr 472321303], length 0
  91. 0x0000: 0200 0000 4500 0034 0000 4000 4006 0000 ....E..4..@.@...
  92. 0x0010: 7f00 0001 7f00 0001 18eb ed9c 37ac 5d22 ............7.]"
  93. 0x0020: b7ec ceca 8010 31d4 fe28 0000 0101 080a ......1..(......
  94. 0x0030: 1c27 0d17 1c27 0d17 .'...'..
  95. 19:09:22.438396 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 61, bad cksum 0 (->3cb9)!)
  96. localhost.6379 > localhost.60828: Flags [P.], cksum 0xfe31 (incorrect -> 0xae62), seq 15:24, ack 106, win 12756, options [nop,nop,TS val 472321303 ecr 472321303], length 9: RESP "QUEUED"
  97. 0x0000: 0200 0000 4500 003d 0000 4000 4006 0000 ....E..=..@.@...
  98. 0x0010: 7f00 0001 7f00 0001 18eb ed9c 37ac 5d22 ............7.]"
  99. 0x0020: b7ec ceca 8018 31d4 fe31 0000 0101 080a ......1..1......
  100. 0x0030: 1c27 0d17 1c27 0d17 2b51 5545 5545 440d .'...'..+QUEUED.
  101. 0x0040: 0a .
  102. 19:09:22.438407 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)
  103. localhost.60828 > localhost.6379: Flags [.], cksum 0xfe28 (incorrect -> 0xd250), seq 106, ack 24, win 12758, options [nop,nop,TS val 472321304 ecr 472321303], length 0
  104. 0x0000: 0200 0000 4500 0034 0000 4000 4006 0000 ....E..4..@.@...
  105. 0x0010: 7f00 0001 7f00 0001 ed9c 18eb b7ec ceca ................
  106. 0x0020: 37ac 5d2b 8010 31d6 fe28 0000 0101 080a 7.]+..1..(......
  107. 0x0030: 1c27 0d18 1c27 0d17 .'...'..
  108. 19:09:22.438444 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 97, bad cksum 0 (->3c95)!)
  109. localhost.60828 > localhost.6379: Flags [P.], cksum 0xfe55 (incorrect -> 0x5fb7), seq 106:151, ack 24, win 12758, options [nop,nop,TS val 472321304 ecr 472321303], length 45: RESP "SET" "testkey2" "helloworld2"
  110. 0x0000: 0200 0000 4500 0061 0000 4000 4006 0000 ....E..a..@.@...
  111. 0x0010: 7f00 0001 7f00 0001 ed9c 18eb b7ec ceca ................
  112. 0x0020: 37ac 5d2b 8018 31d6 fe55 0000 0101 080a 7.]+..1..U......
  113. 0x0030: 1c27 0d18 1c27 0d17 2a33 0d0a 2433 0d0a .'...'..*3..$3..
  114. 0x0040: 5345 540d 0a24 380d 0a74 6573 746b 6579 SET..$8..testkey
  115. 0x0050: 320d 0a24 3131 0d0a 6865 6c6c 6f77 6f72 2..$11..hellowor
  116. 0x0060: 6c64 320d 0a ld2..
  117. 19:09:22.438457 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)
  118. localhost.6379 > localhost.60828: Flags [.], cksum 0xfe28 (incorrect -> 0xd226), seq 24, ack 151, win 12754, options [nop,nop,TS val 472321304 ecr 472321304], length 0
  119. 0x0000: 0200 0000 4500 0034 0000 4000 4006 0000 ....E..4..@.@...
  120. 0x0010: 7f00 0001 7f00 0001 18eb ed9c 37ac 5d2b ............7.]+
  121. 0x0020: b7ec cef7 8010 31d2 fe28 0000 0101 080a ......1..(......
  122. 0x0030: 1c27 0d18 1c27 0d18 .'...'..
  123. 19:09:22.438484 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 61, bad cksum 0 (->3cb9)!)
  124. localhost.6379 > localhost.60828: Flags [P.], cksum 0xfe31 (incorrect -> 0xae2c), seq 24:33, ack 151, win 12754, options [nop,nop,TS val 472321304 ecr 472321304], length 9: RESP "QUEUED"
  125. 0x0000: 0200 0000 4500 003d 0000 4000 4006 0000 ....E..=..@.@...
  126. 0x0010: 7f00 0001 7f00 0001 18eb ed9c 37ac 5d2b ............7.]+
  127. 0x0020: b7ec cef7 8018 31d2 fe31 0000 0101 080a ......1..1......
  128. 0x0030: 1c27 0d18 1c27 0d18 2b51 5545 5545 440d .'...'..+QUEUED.   (加入队列)
  129. 0x0040: 0a .
  130. 19:09:22.438493 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)
  131. localhost.60828 > localhost.6379: Flags [.], cksum 0xfe28 (incorrect -> 0xd219), seq 151, ack 33, win 12758, options [nop,nop,TS val 472321304 ecr 472321304], length 0
  132. 0x0000: 0200 0000 4500 0034 0000 4000 4006 0000 ....E..4..@.@...
  133. 0x0010: 7f00 0001 7f00 0001 ed9c 18eb b7ec cef7 ................
  134. 0x0020: 37ac 5d34 8010 31d6 fe28 0000 0101 080a 7.]4..1..(......
  135. 0x0030: 1c27 0d18 1c27 0d18 .'...'..
  136. 19:09:22.438534 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 66, bad cksum 0 (->3cb4)!)
  137. localhost.60828 > localhost.6379: Flags [P.], cksum 0xfe36 (incorrect -> 0xd1e4), seq 151:165, ack 33, win 12758, options [nop,nop,TS val 472321304 ecr 472321304], length 14: RESP "EXEC"
  138. 0x0000: 0200 0000 4500 0042 0000 4000 4006 0000 ....E..B..@.@...
  139. 0x0010: 7f00 0001 7f00 0001 ed9c 18eb b7ec cef7 ................
  140. 0x0020: 37ac 5d34 8018 31d6 fe36 0000 0101 080a 7.]4..1..6......
  141. 0x0030: 1c27 0d18 1c27 0d18 2a31 0d0a 2434 0d0a .'...'..*1..$4..
  142. 0x0040: 4558 4543 0d0a EXEC..
  143. 19:09:22.438546 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)
  144. localhost.6379 > localhost.60828: Flags [.], cksum 0xfe28 (incorrect -> 0xd20f), seq 33, ack 165, win 12754, options [nop,nop,TS val 472321304 ecr 472321304], length 0
  145. 0x0000: 0200 0000 4500 0034 0000 4000 4006 0000 ....E..4..@.@...
  146. 0x0010: 7f00 0001 7f00 0001 18eb ed9c 37ac 5d34 ............7.]4
  147. 0x0020: b7ec cf05 8010 31d2 fe28 0000 0101 080a ......1..(......
  148. 0x0030: 1c27 0d18 1c27 0d18 .'...'..
  149. 19:09:22.438582 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 71, bad cksum 0 (->3caf)!)
  150. localhost.6379 > localhost.60828: Flags [P.], cksum 0xfe3b (incorrect -> 0x3d7e), seq 33:52, ack 165, win 12754, options [nop,nop,TS val 472321304 ecr 472321304], length 19: RESP "OK" "OK" "OK"
  151. 0x0000: 0200 0000 4500 0047 0000 4000 4006 0000 ....E..G..@.@...
  152. 0x0010: 7f00 0001 7f00 0001 18eb ed9c 37ac 5d34 ............7.]4
  153. 0x0020: b7ec cf05 8018 31d2 fe3b 0000 0101 080a ......1..;......
  154. 0x0030: 1c27 0d18 1c27 0d18 2a33 0d0a 2b4f 4b0d .'...'..*3..+OK.
  155. 0x0040: 0a2b 4f4b 0d0a 2b4f 4b0d 0a .+OK..+OK.. (执行3次请求后批量返回3个结果)
  156. 19:09:22.438593 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)
  157. localhost.60828 > localhost.6379: Flags [.], cksum 0xfe28 (incorrect -> 0xd1f9), seq 165, ack 52, win 12757, options [nop,nop,TS val 472321304 ecr 472321304], length 0
  158. 0x0000: 0200 0000 4500 0034 0000 4000 4006 0000 ....E..4..@.@...
  159. 0x0010: 7f00 0001 7f00 0001 ed9c 18eb b7ec cf05 ................
  160. 0x0020: 37ac 5d47 8010 31d5 fe28 0000 0101 080a 7.]G..1..(......
  161. 0x0030: 1c27 0d18 1c27 0d18 .'...'.. 

3. 测试Multi PIPELINE:

  1. <?php
  2.  
  3. $redis = new redis();
  4. $result = $redis->connect('localhost', 6379);
  5.  
  6. $multi = $redis->multi(Redis::PIPELINE);
  7.  
  8. for($i=0;$i<100;$i++)
  9. $multi->set('testkey' . (string)$i, 'helloworld' . (string)$i);
  10.  
  11. $replies = $multi->exec();
  12.  
  13. var_dump($replies);

TCPDUMP数据分析:

  1. 19:21:38.905913 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 64, bad cksum 0 (->3cb6)!)
  2. localhost.63949 > localhost.6379: Flags [S], cksum 0xfe34 (incorrect -> 0x21b4), seq 3990454584, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 473055013 ecr 0,sackOK,eol], length 0
  3. 0x0000: 0200 0000 4500 0040 0000 4000 4006 0000 ....E..@..@.@...
  4. 0x0010: 7f00 0001 7f00 0001 f9cd 18eb edd9 8138 ...............8
  5. 0x0020: 0000 0000 b002 ffff fe34 0000 0204 3fd8 .........4....?.
  6. 0x0030: 0103 0305 0101 080a 1c32 3f25 0000 0000 .........2?%....
  7. 0x0040: 0402 0000 ....
  8. 19:21:38.905963 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 64, bad cksum 0 (->3cb6)!)
  9. localhost.6379 > localhost.63949: Flags [S.], cksum 0xfe34 (incorrect -> 0x077e), seq 3128427605, ack 3990454585, win 65535, options [mss 16344,nop,wscale 5,nop,nop,TS val 473055013 ecr 473055013,sackOK,eol], length 0
  10. 0x0000: 0200 0000 4500 0040 0000 4000 4006 0000 ....E..@..@.@...
  11. 0x0010: 7f00 0001 7f00 0001 18eb f9cd ba78 0455 .............x.U
  12. 0x0020: edd9 8139 b012 ffff fe34 0000 0204 3fd8 ...9.....4....?.
  13. 0x0030: 0103 0305 0101 080a 1c32 3f25 1c32 3f25 .........2?%.2?%
  14. 0x0040: 0402 0000 ....
  15. 19:21:38.905975 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)
  16. localhost.63949 > localhost.6379: Flags [.], cksum 0xfe28 (incorrect -> 0x4f9a), seq 1, ack 1, win 12759, options [nop,nop,TS val 473055013 ecr 473055013], length 0
  17. 0x0000: 0200 0000 4500 0034 0000 4000 4006 0000 ....E..4..@.@...
  18. 0x0010: 7f00 0001 7f00 0001 f9cd 18eb edd9 8139 ...............9
  19. 0x0020: ba78 0456 8010 31d7 fe28 0000 0101 080a .x.V..1..(......
  20. 0x0030: 1c32 3f25 1c32 3f25 .2?%.2?%
  21. 19:21:38.905985 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)
  22. localhost.6379 > localhost.63949: Flags [.], cksum 0xfe28 (incorrect -> 0x4f9a), seq 1, ack 1, win 12759, options [nop,nop,TS val 473055013 ecr 473055013], length 0
  23. 0x0000: 0200 0000 4500 0034 0000 4000 4006 0000 ....E..4..@.@...
  24. 0x0010: 7f00 0001 7f00 0001 18eb f9cd ba78 0456 .............x.V
  25. 0x0020: edd9 8139 8010 31d7 fe28 0000 0101 080a ...9..1..(......
  26. 0x0030: 1c32 3f25 1c32 3f25 .2?%.2?%
  27. 19:21:38.914905 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 187, bad cksum 0 (->3c3b)!)
  28. localhost.63949 > localhost.6379: Flags [P.], cksum 0xfeaf (incorrect -> 0x09c9), seq 1:136, ack 1, win 12759, options [nop,nop,TS val 473055022 ecr 473055013], length 135: RESP "SET" "testkey0" "helloworld0" "SET" "testkey1" "helloworld1" "SET" "testkey2" "helloworld2"
  29. 0x0000: 0200 0000 4500 00bb 0000 4000 4006 0000 ....E.....@.@...
  30. 0x0010: 7f00 0001 7f00 0001 f9cd 18eb edd9 8139 ...............9
  31. 0x0020: ba78 0456 8018 31d7 feaf 0000 0101 080a .x.V..1.........
  32. 0x0030: 1c32 3f2e 1c32 3f25 2a33 0d0a 2433 0d0a .2?..2?%*3..$3..
  33. 0x0040: 5345 540d 0a24 380d 0a74 6573 746b 6579 SET..$8..testkey
  34. 0x0050: 300d 0a24 3131 0d0a 6865 6c6c 6f77 6f72 0..$11..hellowor
  35. 0x0060: 6c64 300d 0a2a 330d 0a24 330d 0a53 4554 ld0..*3..$3..SET
  36. 0x0070: 0d0a 2438 0d0a 7465 7374 6b65 7931 0d0a ..$8..testkey1..
  37. 0x0080: 2431 310d 0a68 656c 6c6f 776f 726c 6431 $11..helloworld1
  38. 0x0090: 0d0a 2a33 0d0a 2433 0d0a 5345 540d 0a24 ..*3..$3..SET..$
  39. 0x00a0: 380d 0a74 6573 746b 6579 320d 0a24 3131 8..testkey2..$11
  40. 0x00b0: 0d0a 6865 6c6c 6f77 6f72 6c64 320d 0a ..helloworld2.. (一次性,批量发送、执行)
  41. 19:21:38.914935 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)
  42. localhost.6379 > localhost.63949: Flags [.], cksum 0xfe28 (incorrect -> 0x4f05), seq 1, ack 136, win 12755, options [nop,nop,TS val 473055022 ecr 473055022], length 0
  43. 0x0000: 0200 0000 4500 0034 0000 4000 4006 0000 ....E..4..@.@...
  44. 0x0010: 7f00 0001 7f00 0001 18eb f9cd ba78 0456 .............x.V
  45. 0x0020: edd9 81c0 8010 31d3 fe28 0000 0101 080a ......1..(......
  46. 0x0030: 1c32 3f2e 1c32 3f2e .2?..2?.
  47. 19:21:38.915037 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 67, bad cksum 0 (->3cb3)!)
  48. localhost.6379 > localhost.63949: Flags [P.], cksum 0xfe37 (incorrect -> 0xf1b4), seq 1:16, ack 136, win 12755, options [nop,nop,TS val 473055022 ecr 473055022], length 15: RESP "OK" "OK" "OK"
  49. 0x0000: 0200 0000 4500 0043 0000 4000 4006 0000 ....E..C..@.@...
  50. 0x0010: 7f00 0001 7f00 0001 18eb f9cd ba78 0456 .............x.V
  51. 0x0020: edd9 81c0 8018 31d3 fe37 0000 0101 080a ......1..7......
  52. 0x0030: 1c32 3f2e 1c32 3f2e 2b4f 4b0d 0a2b 4f4b .2?..2?.+OK..+OK
  53. 0x0040: 0d0a 2b4f 4b0d 0a ..+OK.. (一次性,返回结果)
  1. 19:21:38.915060 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!) localhost.63949 > localhost.6379: Flags [.], cksum 0xfe28 (incorrect -> 0x4ef3), seq 136, ack 16, win 12758, options [nop,nop,TS val 473055022 ecr 473055022], length 0 0x0000: 0200 0000 4500 0034 0000 4000 4006 0000 ....E..4..@.@... 0x0010: 7f00 0001 7f00 0001 f9cd 18eb edd9 81c0 ................ 0x0020: ba78 0465 8010 31d6 fe28 0000 0101 080a .x.e..1..(...... 0x0030: 1c32 3f2e 1c32 3f2e .2?..2?. 19:21:38.916018 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)

  

总结一下:

1. Multi:

  1.1. 每发送一条指令,都需要单独发给服务器,服务器再单独返回“该条指令已加入队列”这个消息。这是比Pipeline慢的原因之一。

  1.2. Multi执行的时候会先暂停其他命令的执行,类似于加了个锁,直到整个Multi结束完成再继续其他客户端的请求。这是Multi能保证一致性的原因,也是比Pipeline慢的原因之二。(需要读Redis Server的代码,从TCPDump上看不出)

2. Pipeline:

  2.1. 将所有命令打包一次性发送。发送成功后,服务端不用返回类似“命令已收到”这样的消息,而是一次性批量执行所有命令,成功后再一次性返回所有处理结果。

  2.2. 服务端处理命令的时候,不需要加锁,而是与其他客户端的命令混合在一起处理,所以无法保证一致性。

适用场景:

1. 如果要顺序执行一组命令(既网上所谓的“Redis事务”),Multi很合适。

2. 如果要往Redis里批量插入Log, 或者使用Redis List做为队列并插入很多消息的话,Pipleline是挺合适的。

3. 网络不好,又顺序性不强的数据,适合Pipleline

性能:

  1. without pipelining 1.185238 seconds
  2. with pipelining 0.250783 seconds (有时能快一个数量级)

参考:https://redis.io/topics/pipelining

Redis的Multi的内幕真相的更多相关文章

  1. redis中multi和pipeline区别以及效率(推荐使用pipeline)

    手册得知 pipeline 只是把多个redis指令一起发出去,redis并没有保证这些指定的执行是原子的:multi相当于一个redis的transaction的,保证整个操作的原子性,避免由于中途 ...

  2. redis watch multi exec 关系

    EXEC 执行所有事务块内的命令. 假如某个(或某些) key 正处于 WATCH 命令的监视之下,且事务块中有和这个(或这些) key 相关的命令,那么EXEC 命令只在这个(或这些) key 没有 ...

  3. 洞悉Redis技术内幕:缓存,数据结构,并发,集群与算法

    "为什么这个功能用不了?" 程序员:"清一下缓存" 上篇洞悉系列文章给大家详细介绍了MySQL的存储内幕:洞悉MySQL底层架构:游走在缓冲与磁盘之间.既然聊过 ...

  4. Redis - 事务(multi,exec,watch,unwatch)

    转载自:https://blog.csdn.net/wgh1015398431/article/details/53156027:加了一些自己的注解 1.事务 1.1 概述 Redis中的事务(tra ...

  5. Redis之eval+lua实现初步

    目录 目录 1 1. 前言 1 2. 执行方式 1 3. 执行过程 3 4. 使用原则 3 1. 前言 Redis的实现保证eval的执行是原子的,即使eval执行的lua超时,Redis也不会自动终 ...

  6. redis源码学习之lua执行原理

    聊聊redis执行lua原理 从一次面试场景说起   "看你简历上写的精通redis" "额,还可以啦" "那你说说redis执行lua脚本的原理&q ...

  7. Redis 数据库入门指南

    Redis 是一个开源数据库,它使用内存数据结构存储,可作为数据库.缓存和消息代理使用.Redis 支持丰富的数据结构,有:字符串(Strings).哈希(Hashs).列表(Lists).集合(Se ...

  8. Redis 对比 Memcached 并在 CentOS 下进行安装配置

    了解一下 Redis Redis 是一个开源.支持网络.基于内存.键值对的 Key-Value 数据库,使用 ANSI C 编写,并提供多种语言的 API ,它几乎没有上手难度,只需要几分钟我们就能完 ...

  9. Redis系列-冷知识

    下面是一些看了,但觉得用处不大,不记下又可惜的东西. Redis删除过期数据 redis通过expire/expireat(秒为单位)或者pexpire/pexpireat(毫秒为单位)来设置key的 ...

随机推荐

  1. activeMQ点对点

    摘要: ActiveMQ 点对点消息 Point-to-Point 是一对一 创建消息生产者 /**  * 点对点消息生产者  *   * @author Edward  *   */ public  ...

  2. 元组&哈希&字典

    1.元组(1)特性:不可变(不能修改.添加.删除),可以做切片 元组本身不可变,如果元组中还包含其他可变元素,这些可变元素可以改变(元组里套用列表,列表中的值可变)(2)功能: index count ...

  3. MyEclipse导入Maven项目以及Maven转化为Dynamic Web Module(转)

    MyEclipse导入Maven项目(这个博主写的比较详细)https://blog.csdn.net/w12345_ww/article/details/52094756 Maven转化为Dynam ...

  4. Python基础数据类型-列表(list)和元组(tuple)和集合(set)

    Python基础数据类型-列表(list)和元组(tuple)和集合(set) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客使用的是Python3.6版本,以及以后分享的 ...

  5. PostgreSQL操作-psql基本命令

    一.建立数据库连接----------------接入PostgreSQL数据库: psql -h IP地址 -p 端口 -U 数据库名 之后会要求输入数据库密码 二.访问数据库 1.列举数据库:\l ...

  6. 多目标遗传算法 ------ NSGA-II (部分源码解析)两个个体支配判断 dominance.c

    /* Domination checking routines */ # include <stdio.h> # include <stdlib.h> # include &l ...

  7. 为什么要用redis

    服务端的程序如何去识别客户端的状态: http是没有状态的,比如说用户A访问了服务器程序,那服务器如何知道下一次访问的时候还是A呢,这里就要用到session, 这个session是服务器的sessi ...

  8. Java 编程下使用 Class.forName() 加载类【转】

    在一些应用中,无法事先知道使用者将加载什么类,而必须让使用者指定类名称以加载类,可以使用 Class 的静态 forName() 方法实现动态加载类.下面的范例让你可以指定类名称来获得类的相关信息. ...

  9. JQuery弹出层,点击按钮后弹出遮罩层,有关闭按钮【转】

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  10. mvn依赖冲突

    https://www.cnblogs.com/twoheads/p/10082453.html