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

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

步骤:

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

2. 测试Multi:

<?php$redis = new redis();
$result = $redis->connect('localhost', 6379);
//默认是Redis::MULTI
$multi = $redis->multi();for($i=0;$i<100;$i++)
$multi->set('testkey' . (string)$i, 'helloworld' . (string)$i);$replies = $multi->exec();var_dump($replies);?>

  

TCPDump抓包:

sudo tcpdump -i lo0 port 6379 -XX -vvv

  

TCPDump数据:

19:09:22.437455 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 64, bad cksum 0 (->3cb6)!)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 00x0000:  0200 0000 4500 0040 0000 4000 4006 0000  ....E..@..@.@...0x0010:  7f00 0001 7f00 0001 ed9c 18eb b7ec ce60  ...............`0x0020:  0000 0000 b002 ffff fe34 0000 0204 3fd8  .........4....?.0x0030:  0103 0305 0101 080a 1c27 0d17 0000 0000  .........'......0x0040:  0402 0000                                ....
19:09:22.437505 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 64, bad cksum 0 (->3cb6)!)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 00x0000:  0200 0000 4500 0040 0000 4000 4006 0000  ....E..@..@.@...0x0010:  7f00 0001 7f00 0001 18eb ed9c 37ac 5d13  ............7.].0x0020:  b7ec ce61 b012 ffff fe34 0000 0204 3fd8  ...a.....4....?.0x0030:  0103 0305 0101 080a 1c27 0d17 1c27 0d17  .........'...'..0x0040:  0402 0000                                ....
19:09:22.437515 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)localhost.60828 > localhost.6379: Flags [.], cksum 0xfe28 (incorrect -> 0xd2d0), seq 1, ack 1, win 12759, options [nop,nop,TS val 472321303 ecr 472321303], length 00x0000:  0200 0000 4500 0034 0000 4000 4006 0000  ....E..4..@.@...0x0010:  7f00 0001 7f00 0001 ed9c 18eb b7ec ce61  ...............a0x0020:  37ac 5d14 8010 31d7 fe28 0000 0101 080a  7.]...1..(......0x0030:  1c27 0d17 1c27 0d17                      .'...'..
19:09:22.437525 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)localhost.6379 > localhost.60828: Flags [.], cksum 0xfe28 (incorrect -> 0xd2d0), seq 1, ack 1, win 12759, options [nop,nop,TS val 472321303 ecr 472321303], length 00x0000:  0200 0000 4500 0034 0000 4000 4006 0000  ....E..4..@.@...0x0010:  7f00 0001 7f00 0001 18eb ed9c 37ac 5d14  ............7.].0x0020:  b7ec ce61 8010 31d7 fe28 0000 0101 080a  ...a..1..(......0x0030:  1c27 0d17 1c27 0d17                      .'...'..
19:09:22.437866 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 67, bad cksum 0 (->3cb3)!)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"0x0000:  0200 0000 4500 0043 0000 4000 4006 0000  ....E..C..@.@...0x0010:  7f00 0001 7f00 0001 ed9c 18eb b7ec ce61  ...............a0x0020:  37ac 5d14 8018 31d7 fe37 0000 0101 080a  7.]...1..7......0x0030:  1c27 0d17 1c27 0d17 2a31 0d0a 2435 0d0a  .'...'..*1..$5..0x0040:  4d55 4c54 490d 0a                        MULTI..
19:09:22.437885 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)localhost.6379 > localhost.60828: Flags [.], cksum 0xfe28 (incorrect -> 0xd2c2), seq 1, ack 16, win 12758, options [nop,nop,TS val 472321303 ecr 472321303], length 00x0000:  0200 0000 4500 0034 0000 4000 4006 0000  ....E..4..@.@...0x0010:  7f00 0001 7f00 0001 18eb ed9c 37ac 5d14  ............7.].0x0020:  b7ec ce70 8010 31d6 fe28 0000 0101 080a  ...p..1..(......0x0030:  1c27 0d17 1c27 0d17                      .'...'..
19:09:22.437979 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 57, bad cksum 0 (->3cbd)!)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"0x0000:  0200 0000 4500 0039 0000 4000 4006 0000  ....E..9..@.@...0x0010:  7f00 0001 7f00 0001 18eb ed9c 37ac 5d14  ............7.].0x0020:  b7ec ce70 8018 31d6 fe2d 0000 0101 080a  ...p..1..-......0x0030:  1c27 0d17 1c27 0d17 2b4f 4b0d 0a         .'...'..+OK..
19:09:22.437993 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)localhost.60828 > localhost.6379: Flags [.], cksum 0xfe28 (incorrect -> 0xd2bc), seq 16, ack 6, win 12759, options [nop,nop,TS val 472321303 ecr 472321303], length 00x0000:  0200 0000 4500 0034 0000 4000 4006 0000  ....E..4..@.@...0x0010:  7f00 0001 7f00 0001 ed9c 18eb b7ec ce70  ...............p0x0020:  37ac 5d19 8010 31d7 fe28 0000 0101 080a  7.]...1..(......0x0030:  1c27 0d17 1c27 0d17                      .'...'..
19:09:22.438224 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 97, bad cksum 0 (->3c95)!)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"0x0000:  0200 0000 4500 0061 0000 4000 4006 0000  ....E..a..@.@...0x0010:  7f00 0001 7f00 0001 ed9c 18eb b7ec ce70  ...............p0x0020:  37ac 5d19 8018 31d7 fe55 0000 0101 080a  7.]...1..U......0x0030:  1c27 0d17 1c27 0d17 2a33 0d0a 2433 0d0a  .'...'..*3..$3..0x0040:  5345 540d 0a24 380d 0a74 6573 746b 6579  SET..$8..testkey0x0050:  300d 0a24 3131 0d0a 6865 6c6c 6f77 6f72  0..$11..hellowor0x0060:  6c64 300d 0a                             ld0..
19:09:22.438245 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)localhost.6379 > localhost.60828: Flags [.], cksum 0xfe28 (incorrect -> 0xd291), seq 6, ack 61, win 12757, options [nop,nop,TS val 472321303 ecr 472321303], length 00x0000:  0200 0000 4500 0034 0000 4000 4006 0000  ....E..4..@.@...0x0010:  7f00 0001 7f00 0001 18eb ed9c 37ac 5d19  ............7.].0x0020:  b7ec ce9d 8010 31d5 fe28 0000 0101 080a  ......1..(......0x0030:  1c27 0d17 1c27 0d17                      .'...'..
19:09:22.438291 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 61, bad cksum 0 (->3cb9)!)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"0x0000:  0200 0000 4500 003d 0000 4000 4006 0000  ....E..=..@.@...0x0010:  7f00 0001 7f00 0001 18eb ed9c 37ac 5d19  ............7.].0x0020:  b7ec ce9d 8018 31d5 fe31 0000 0101 080a  ......1..1......0x0030:  1c27 0d17 1c27 0d17 2b51 5545 5545 440d  .'...'..+QUEUED.0x0040:  0a                                       .
19:09:22.438307 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)localhost.60828 > localhost.6379: Flags [.], cksum 0xfe28 (incorrect -> 0xd287), seq 61, ack 15, win 12758, options [nop,nop,TS val 472321303 ecr 472321303], length 00x0000:  0200 0000 4500 0034 0000 4000 4006 0000  ....E..4..@.@...0x0010:  7f00 0001 7f00 0001 ed9c 18eb b7ec ce9d  ................0x0020:  37ac 5d22 8010 31d6 fe28 0000 0101 080a  7.]"..1..(......0x0030:  1c27 0d17 1c27 0d17                      .'...'..
19:09:22.438362 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 97, bad cksum 0 (->3c95)!)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"0x0000:  0200 0000 4500 0061 0000 4000 4006 0000  ....E..a..@.@...0x0010:  7f00 0001 7f00 0001 ed9c 18eb b7ec ce9d  ................0x0020:  37ac 5d22 8018 31d6 fe55 0000 0101 080a  7.]"..1..U......0x0030:  1c27 0d17 1c27 0d17 2a33 0d0a 2433 0d0a  .'...'..*3..$3..0x0040:  5345 540d 0a24 380d 0a74 6573 746b 6579  SET..$8..testkey0x0050:  310d 0a24 3131 0d0a 6865 6c6c 6f77 6f72  1..$11..hellowor0x0060:  6c64 310d 0a                             ld1..
19:09:22.438378 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)localhost.6379 > localhost.60828: Flags [.], cksum 0xfe28 (incorrect -> 0xd25c), seq 15, ack 106, win 12756, options [nop,nop,TS val 472321303 ecr 472321303], length 00x0000:  0200 0000 4500 0034 0000 4000 4006 0000  ....E..4..@.@...0x0010:  7f00 0001 7f00 0001 18eb ed9c 37ac 5d22  ............7.]"0x0020:  b7ec ceca 8010 31d4 fe28 0000 0101 080a  ......1..(......0x0030:  1c27 0d17 1c27 0d17                      .'...'..
19:09:22.438396 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 61, bad cksum 0 (->3cb9)!)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"0x0000:  0200 0000 4500 003d 0000 4000 4006 0000  ....E..=..@.@...0x0010:  7f00 0001 7f00 0001 18eb ed9c 37ac 5d22  ............7.]"0x0020:  b7ec ceca 8018 31d4 fe31 0000 0101 080a  ......1..1......0x0030:  1c27 0d17 1c27 0d17 2b51 5545 5545 440d  .'...'..+QUEUED.0x0040:  0a                                       .
19:09:22.438407 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)localhost.60828 > localhost.6379: Flags [.], cksum 0xfe28 (incorrect -> 0xd250), seq 106, ack 24, win 12758, options [nop,nop,TS val 472321304 ecr 472321303], length 00x0000:  0200 0000 4500 0034 0000 4000 4006 0000  ....E..4..@.@...0x0010:  7f00 0001 7f00 0001 ed9c 18eb b7ec ceca  ................0x0020:  37ac 5d2b 8010 31d6 fe28 0000 0101 080a  7.]+..1..(......0x0030:  1c27 0d18 1c27 0d17                      .'...'..
19:09:22.438444 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 97, bad cksum 0 (->3c95)!)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"0x0000:  0200 0000 4500 0061 0000 4000 4006 0000  ....E..a..@.@...0x0010:  7f00 0001 7f00 0001 ed9c 18eb b7ec ceca  ................0x0020:  37ac 5d2b 8018 31d6 fe55 0000 0101 080a  7.]+..1..U......0x0030:  1c27 0d18 1c27 0d17 2a33 0d0a 2433 0d0a  .'...'..*3..$3..0x0040:  5345 540d 0a24 380d 0a74 6573 746b 6579  SET..$8..testkey0x0050:  320d 0a24 3131 0d0a 6865 6c6c 6f77 6f72  2..$11..hellowor0x0060:  6c64 320d 0a                             ld2..
19:09:22.438457 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)localhost.6379 > localhost.60828: Flags [.], cksum 0xfe28 (incorrect -> 0xd226), seq 24, ack 151, win 12754, options [nop,nop,TS val 472321304 ecr 472321304], length 00x0000:  0200 0000 4500 0034 0000 4000 4006 0000  ....E..4..@.@...0x0010:  7f00 0001 7f00 0001 18eb ed9c 37ac 5d2b  ............7.]+0x0020:  b7ec cef7 8010 31d2 fe28 0000 0101 080a  ......1..(......0x0030:  1c27 0d18 1c27 0d18                      .'...'..
19:09:22.438484 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 61, bad cksum 0 (->3cb9)!)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"0x0000:  0200 0000 4500 003d 0000 4000 4006 0000  ....E..=..@.@...0x0010:  7f00 0001 7f00 0001 18eb ed9c 37ac 5d2b  ............7.]+0x0020:  b7ec cef7 8018 31d2 fe31 0000 0101 080a  ......1..1......0x0030:  1c27 0d18 1c27 0d18 2b51 5545 5545 440d  .'...'..+QUEUED.   (加入队列)0x0040:  0a                                       .
19:09:22.438493 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)localhost.60828 > localhost.6379: Flags [.], cksum 0xfe28 (incorrect -> 0xd219), seq 151, ack 33, win 12758, options [nop,nop,TS val 472321304 ecr 472321304], length 00x0000:  0200 0000 4500 0034 0000 4000 4006 0000  ....E..4..@.@...0x0010:  7f00 0001 7f00 0001 ed9c 18eb b7ec cef7  ................0x0020:  37ac 5d34 8010 31d6 fe28 0000 0101 080a  7.]4..1..(......0x0030:  1c27 0d18 1c27 0d18                      .'...'..
19:09:22.438534 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 66, bad cksum 0 (->3cb4)!)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"0x0000:  0200 0000 4500 0042 0000 4000 4006 0000  ....E..B..@.@...0x0010:  7f00 0001 7f00 0001 ed9c 18eb b7ec cef7  ................0x0020:  37ac 5d34 8018 31d6 fe36 0000 0101 080a  7.]4..1..6......0x0030:  1c27 0d18 1c27 0d18 2a31 0d0a 2434 0d0a  .'...'..*1..$4..0x0040:  4558 4543 0d0a                           EXEC..
19:09:22.438546 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)localhost.6379 > localhost.60828: Flags [.], cksum 0xfe28 (incorrect -> 0xd20f), seq 33, ack 165, win 12754, options [nop,nop,TS val 472321304 ecr 472321304], length 00x0000:  0200 0000 4500 0034 0000 4000 4006 0000  ....E..4..@.@...0x0010:  7f00 0001 7f00 0001 18eb ed9c 37ac 5d34  ............7.]40x0020:  b7ec cf05 8010 31d2 fe28 0000 0101 080a  ......1..(......0x0030:  1c27 0d18 1c27 0d18                      .'...'..
19:09:22.438582 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 71, bad cksum 0 (->3caf)!)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"0x0000:  0200 0000 4500 0047 0000 4000 4006 0000  ....E..G..@.@...0x0010:  7f00 0001 7f00 0001 18eb ed9c 37ac 5d34  ............7.]40x0020:  b7ec cf05 8018 31d2 fe3b 0000 0101 080a  ......1..;......0x0030:  1c27 0d18 1c27 0d18 2a33 0d0a 2b4f 4b0d  .'...'..*3..+OK.0x0040:  0a2b 4f4b 0d0a 2b4f 4b0d 0a              .+OK..+OK..          (执行3次请求后批量返回3个结果)
19:09:22.438593 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)localhost.60828 > localhost.6379: Flags [.], cksum 0xfe28 (incorrect -> 0xd1f9), seq 165, ack 52, win 12757, options [nop,nop,TS val 472321304 ecr 472321304], length 00x0000:  0200 0000 4500 0034 0000 4000 4006 0000  ....E..4..@.@...0x0010:  7f00 0001 7f00 0001 ed9c 18eb b7ec cf05  ................0x0020:  37ac 5d47 8010 31d5 fe28 0000 0101 080a  7.]G..1..(......0x0030:  1c27 0d18 1c27 0d18                      .'...'.. 

3. 测试Multi PIPELINE:

<?php$redis = new redis();
$result = $redis->connect('localhost', 6379);$multi = $redis->multi(Redis::PIPELINE);for($i=0;$i<100;$i++)
$multi->set('testkey' . (string)$i, 'helloworld' . (string)$i);$replies = $multi->exec();var_dump($replies);

TCPDUMP数据分析:

19:21:38.905913 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 64, bad cksum 0 (->3cb6)!)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 00x0000:  0200 0000 4500 0040 0000 4000 4006 0000  ....E..@..@.@...0x0010:  7f00 0001 7f00 0001 f9cd 18eb edd9 8138  ...............80x0020:  0000 0000 b002 ffff fe34 0000 0204 3fd8  .........4....?.0x0030:  0103 0305 0101 080a 1c32 3f25 0000 0000  .........2?%....0x0040:  0402 0000                                ....
19:21:38.905963 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 64, bad cksum 0 (->3cb6)!)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 00x0000:  0200 0000 4500 0040 0000 4000 4006 0000  ....E..@..@.@...0x0010:  7f00 0001 7f00 0001 18eb f9cd ba78 0455  .............x.U0x0020:  edd9 8139 b012 ffff fe34 0000 0204 3fd8  ...9.....4....?.0x0030:  0103 0305 0101 080a 1c32 3f25 1c32 3f25  .........2?%.2?%0x0040:  0402 0000                                ....
19:21:38.905975 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 -> 0x4f9a), seq 1, ack 1, win 12759, options [nop,nop,TS val 473055013 ecr 473055013], length 00x0000:  0200 0000 4500 0034 0000 4000 4006 0000  ....E..4..@.@...0x0010:  7f00 0001 7f00 0001 f9cd 18eb edd9 8139  ...............90x0020:  ba78 0456 8010 31d7 fe28 0000 0101 080a  .x.V..1..(......0x0030:  1c32 3f25 1c32 3f25                      .2?%.2?%
19:21:38.905985 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)localhost.6379 > localhost.63949: Flags [.], cksum 0xfe28 (incorrect -> 0x4f9a), seq 1, ack 1, win 12759, options [nop,nop,TS val 473055013 ecr 473055013], length 00x0000:  0200 0000 4500 0034 0000 4000 4006 0000  ....E..4..@.@...0x0010:  7f00 0001 7f00 0001 18eb f9cd ba78 0456  .............x.V0x0020:  edd9 8139 8010 31d7 fe28 0000 0101 080a  ...9..1..(......0x0030:  1c32 3f25 1c32 3f25                      .2?%.2?%
19:21:38.914905 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 187, bad cksum 0 (->3c3b)!)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"0x0000:  0200 0000 4500 00bb 0000 4000 4006 0000  ....E.....@.@...0x0010:  7f00 0001 7f00 0001 f9cd 18eb edd9 8139  ...............90x0020:  ba78 0456 8018 31d7 feaf 0000 0101 080a  .x.V..1.........0x0030:  1c32 3f2e 1c32 3f25 2a33 0d0a 2433 0d0a  .2?..2?%*3..$3..0x0040:  5345 540d 0a24 380d 0a74 6573 746b 6579  SET..$8..testkey0x0050:  300d 0a24 3131 0d0a 6865 6c6c 6f77 6f72  0..$11..hellowor0x0060:  6c64 300d 0a2a 330d 0a24 330d 0a53 4554  ld0..*3..$3..SET0x0070:  0d0a 2438 0d0a 7465 7374 6b65 7931 0d0a  ..$8..testkey1..0x0080:  2431 310d 0a68 656c 6c6f 776f 726c 6431  $11..helloworld10x0090:  0d0a 2a33 0d0a 2433 0d0a 5345 540d 0a24  ..*3..$3..SET..$0x00a0:  380d 0a74 6573 746b 6579 320d 0a24 3131  8..testkey2..$110x00b0:  0d0a 6865 6c6c 6f77 6f72 6c64 320d 0a    ..helloworld2..        (一次性,批量发送、执行)
19:21:38.914935 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 52, bad cksum 0 (->3cc2)!)localhost.6379 > localhost.63949: Flags [.], cksum 0xfe28 (incorrect -> 0x4f05), seq 1, ack 136, win 12755, options [nop,nop,TS val 473055022 ecr 473055022], length 00x0000:  0200 0000 4500 0034 0000 4000 4006 0000  ....E..4..@.@...0x0010:  7f00 0001 7f00 0001 18eb f9cd ba78 0456  .............x.V0x0020:  edd9 81c0 8010 31d3 fe28 0000 0101 080a  ......1..(......0x0030:  1c32 3f2e 1c32 3f2e                      .2?..2?.
19:21:38.915037 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 67, bad cksum 0 (->3cb3)!)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"0x0000:  0200 0000 4500 0043 0000 4000 4006 0000  ....E..C..@.@...0x0010:  7f00 0001 7f00 0001 18eb f9cd ba78 0456  .............x.V0x0020:  edd9 81c0 8018 31d3 fe37 0000 0101 080a  ......1..7......0x0030:  1c32 3f2e 1c32 3f2e 2b4f 4b0d 0a2b 4f4b  .2?..2?.+OK..+OK0x0040:  0d0a 2b4f 4b0d 0a                        ..+OK..                (一次性,返回结果)
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

性能:

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

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

转载于:https://www.cnblogs.com/sunsky303/p/9164013.html

Redis的Multi的内幕真相相关推荐

  1. redis的MULTI与PIPELINE

    1. 问题 redis的multi相信很多同学用过,先看下面的代码. <?php $redis = new Redis(); $host = "10.136.30.144"; ...

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

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

  3. Nodejs redis客户端multi命令批量操作

    multi支持所有client支持的命令,如multi.set将set命令参数(包括回调函数)加入命令队列,由multi.exec最终执行命令队列并依次调用队列中的回调函数,最后再调用multi.ex ...

  4. Redis事务Multi介绍

    Redis事务 Redis的事务是通过multi.exec.discard和watch这四个命令来完成的. Redis的单个命令都是原子性的,所以这里需要确保事务性的对象是命令集合. Redis将命令 ...

  5. redis watch multi exec 关系

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

  6. 李娜大骂老公姜山的内幕真相(图)

    李娜方言大吼老公遭直播 WTA顶级巡回赛阿联酋迪拜冠军赛北京时间前天进行了第二轮争夺.首轮轮空的中国金花李娜遭遇比利时新星维克梅耶尔的顽强阻击,李娜在第二盘浪费了4个赛点后,以7比6(6).6比7(6 ...

  7. redis在python下保持代码原子性(multi)

    需求: 从redis的字典A中查找获取结果a,将结果a作为条件查找字典B获取结果b,再将b添加到字典C中,将字典B中的记录b删除.保证整个过程的原子性. 查询redis的官方文档,给上述需求中的代码过 ...

  8. 报告!活捉Redis线上故障一枚!

    一.前言 最近项目的生产环境遇到一个奇怪的问题: 现象:每天早上客服人员在后台创建客服事件时,都会创建失败.当我们重启这个微服务后,后台就可以正常创建了客服事件了.到第二天早上又会创建失败,又得重启这 ...

  9. 用 Redis 搞定游戏中的实时排行榜,附源码!

    原文:segmentfault.com/a/1190000019139010 1. 前言 前段时间刚为项目(手游)实现了一个实时排行榜功能, 主要特性: 实时全服排名 可查询单个玩家排名 支持双维排序 ...

最新文章

  1. python 打印数组变量_使用Python将数组的元素导出到变量中(unpacking)
  2. 学习进度条(第六周)
  3. python执行系统命令的方法
  4. android shell hello world,Android Framework 之HelloWorld(三)
  5. 【代码收集】提前载入贴图
  6. C#中BackgroundWorker的介绍
  7. 算法应用-斐波那契数列
  8. vue如何让自定义函数挂到全局
  9. 计算机网络误区——VLAN中Access和Trunk原理详解
  10. 【笔记】k-Nearest Neighbors(KNN/k近邻)原理
  11. Mysql 查询某个字段最长的记录
  12. Textview属性Kotlin.Android
  13. 【软件需求工程】北理的恶龙们——软件需求规格说明
  14. 一款开源的微信小程序商城项目,接外包直接拿去改改,就能用。。。
  15. 计算机视觉教程3-1:全面详解图像边缘检测算法(附Python实战)
  16. html爱情意思,1一9爱情数字什么意思 1到9数字爱情含义
  17. 修改Linux的分辨率
  18. 典藏推荐:《乱世佳人》(《飘》)
  19. 如何将静图制作成动图?教你一招快速合成GIF动图的方法
  20. win10密码忘了怎么办_笔记本密码忘了怎么办

热门文章

  1. Linux下常用的压缩与解压工具
  2. 计算机网络 | 网络概述(什么是互联网 互联网结构)
  3. 88的89次方+89的88次方,结果的个位数是多少?
  4. 29.97帧/秒 与 30帧/秒 的区别
  5. UGC、PGC、OGC概念、例子和关联和区别
  6. python26种基本设计_二十三种设计模式及其python实现
  7. Python电网静态和准静态分析及自动化优化
  8. 获取指定时间的那天 23:59:59.999 的时间
  9. 学会用时间赚快钱是对的
  10. 支原体检测,假阳性?假阴性?