background

Due to the batch operation of redis cache data, such as preheating cache data, or batch obtaining cache data in the list page, after using multi batch submission transaction, I found that redis pressure is high, but after using pipeline pressure fell back to normal, also because of this case, I write an analysis and note here.

multi

Introduction to the

Marks the start of a transaction block. Multiple commands in a transaction block are placed in a queue in sequence and executed atomically by the EXEC command.

Realize the principle of

I use PHP extension to call up the Redis service and execute the code as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);$handle = $redis->multi();
$handle->incr('a');
$handle->incr('b');
$handle->exec(a);Copy the code

To view the specific connection process during this period, wireshark is used to listen on the loopback address port 6379, and the packet capture request is as shown in the following figure:

After the Redis client establishes a connection with the server, the multi flag transaction begins, and each time after that, the server returns the Queued queue flag. Redis SRC/multi-. c

void queueMultiCommand(client *c) {
    multiCmd *mc;
    int j;

    c->mstate.commands = zrealloc(c->mstate.commands,
            sizeof(multiCmd)*(c->mstate.count+1));
    mc = c->mstate.commands+c->mstate.count;
    mc->cmd = c->cmd;
    mc->argc = c->argc;
    mc->argv = zmalloc(sizeof(robj*)*c->argc);
    memcpy(mc->argv,c->argv,sizeof(robj*)*c->argc);
    for (j = 0; j < c->argc; j++)
        incrRefCount(mc->argv[j]);
    c->mstate.count++;
}
Copy the code

In the above source code, you can see that the Redis server will save the commands in the transaction block to memory each time, which has been explained in the above introduction. Finally, the exec command is executed.

pipeline

Introduction to the

The client writes the executed command to the buffer, and the exec command sends it back to redis once.

Realize the principle of

Similarly, call Redis packet capture with the relevant code;

$redis = new redis();
$redis->connect('127.0.0.1', 6379);$handle = $redis->pipeline();
$handle->incr('a');
$handle->incr('b');
$handle->exec(a);Copy the code

Continue to capture packets using Wireshark, as shown in the following figure

  • Sample diagram of pipeline client request package

  • The Pipeline server returns a sample package diagram

The client will write the command to the buffer, and finally send it to the server through exec command. The server will split the command and return the result one by one.

The difference between the two

As can be seen from the above request, the most obvious difference between the two is that the client sends requests in different ways. The specific differences are as follows:

  • Pipeline select client buffer, Multi select server buffer;
  • Multi requires each command to be sent to the server once. Pipeline sends the command to the server at the last time. The number of requests is reduced compared with multi
  • Multi /exec guarantees atomicity, whereas pipeline does not

Personal blog address: blog.walkerx.cn