How can I execute commands in redis without gettin

2019-07-10 23:32发布

问题:

I try to execute commands on redis but don't care for any response and don't even want any to minimize network traffic. One answer on stackoverflow said a Lua scripts that doesn't return anything could help to achieve that, but when I try it on the redis-cli and sniff my packages I still get the same number of packages transfered between client and server whether I have a script returning nothing or one that returns Integer 1.

The example Queries are:

  • EVAL "" 0
  • EVAL "return 1" 0

In both cases wireshark shows 4 packages exchanged. One [PSH, ACK] client to server, [ACK] from the server to the client, [PSH, ACK] from the server to the client and [ACK] back from the client to the server. In the first case the [PSH, ACK] package that I expect to be the response from redis contains the following data:

0000  02 00 00 00 45 00 00 39  bc a8 40 00 40 06 00 00   ....E..9 ..@.@...
0010  7f 00 00 01 7f 00 00 01  18 eb e6 bb 03 4d 7c 9c   ........ .....M|.
0020  e2 97 bf 53 80 18 23 df  fe 2d 00 00 01 01 08 0a   ...S..#. .-......
0030  11 cd c0 31 11 cd c0 31  24 2d 31 0d 0a            ...1...1 $-1..   

In the second case this package contains:

0000  02 00 00 00 45 00 00 38  fa 9f 40 00 40 06 00 00   ....E..8 ..@.@...
0010  7f 00 00 01 7f 00 00 01  18 eb e6 bb 03 4d 7c a1   ........ .....M|.
0020  e2 97 bf 76 80 18 23 dd  fe 2c 00 00 01 01 08 0a   ...v..#. .,......
0030  11 ce be 46 11 ce be 46  3a 31 0d 0a               ...F...F :1..    

For the second case the point is clear. :1 is the integer reply for 1. But for the first case I'm not sure. $ is the indicator for bulk reply and - the indicator for error. Does this mean that $-1 is the data for the (nil) that is shown in the redis-cli? Or am I completely wrong there? And if I am right, is there a possibility to tell redis that I don't want any response at all (except the ACK for the command)? Or would I have to fork the redis code and implement this myself?

I really appreciate any hints on how to achieve getting no response at all without digging into the redis source code.

回答1:

EVAL "" 0             returns $-1\r\n
EVAL "return 1" 0     returns :1\r\n

In the first case, $-1 is a specific bulk-reply to be used to represent the nil value (as described in the protocol specification)

AFAIK, there is no possibility to tell Redis it does not have to send a reply (even for an empty answer).

As explained by Marc Gravell, you can use Lua to bundle several operations and reduce the volume of the reply data. However, you will not avoid the minimal reply packet.

For instance you could run 100 operations in one Lua script and have one single minimal packet as a reply. However, this packet cannot be avoided IMO, except by altering Redis source code.



标签: lua redis